Plop
A simple logging library for PHP
Collection.php
1 <?php
2 /*
3  This file is part of Plop, a simple logging library for PHP.
4 
5  Copyright © 2010-2014 François Poirotte
6 
7  Plop is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Plop is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Plop. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 namespace Plop;
22 
28 {
29  const TYPE_HINT = null;
30 
32  protected $items = array();
33 
35  public function count()
36  {
37  return count($this->items);
38  }
39 
41  public function getIterator()
42  {
43  return new ArrayIterator($this->items);
44  }
45 
47  public function offsetGet($offset)
48  {
49  return $this->items[$offset];
50  }
51 
53  public function offsetSet($offset, $value)
54  {
55  $hint = static::TYPE_HINT;
56  if ($hint !== null && (!is_object($value) || !($value instanceof $hint))) {
57  throw new \Plop\Exception('An instance of ' . $hint . ' was expected');
58  }
59 
60  if ($offset === null) {
61  $this->items[] = $value;
62  } else {
63  $this->items[$offset] = $value;
64  }
65  }
66 
68  public function offsetExists($offset)
69  {
70  $res = @isset($this->items[$offset]) ||
71  (array_search($offset, $this->items, true) !== false);
72  return $res;
73  }
74 
76  public function offsetUnset($offset)
77  {
78  if (!is_int($offset)) {
79  $offset = array_search($offset, $this->items, true);
80  }
81  if (isset($this->items[$offset])) {
82  unset($this->items[$offset]);
83  }
84  }
85 }
Interface to provide accessing objects as arrays.
Interface to create an external Iterator.
A class that can be used to handle collections of objects.
Definition: Collection.php:27
offsetSet($offset, $value)
Definition: Collection.php:53
offsetUnset($offset)
Definition: Collection.php:76
offsetGet($offset)
Definition: Collection.php:47
Classes implementing Countable can be used with the count() function.
offsetExists($offset)
Definition: Collection.php:68