Erebot  latest
A modular IRC bot for PHP 5.3+
CollectionAbstract.php
1 <?php
2 /*
3  This file is part of Erebot, a modular IRC bot written in PHP.
4 
5  Copyright © 2010 François Poirotte
6 
7  Erebot 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  Erebot 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 Erebot. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 namespace Erebot\Event\Match;
22 
33 abstract class CollectionAbstract implements
37 {
39  protected $submatchers;
40 
49  public function __construct()
50  {
51  $args = func_get_args();
52  foreach ($args as $arg) {
53  if (!($arg instanceof \Erebot\Interfaces\Event\Match)) {
54  throw new \Erebot\InvalidValueException('Not a valid matcher');
55  }
56  }
57  $this->submatchers = $args;
58  }
59 
61  public function count()
62  {
63  return count($this->submatchers);
64  }
65 
67  public function offsetExists($offset)
68  {
69  return isset($this->submatchers[$offset]);
70  }
71 
73  public function offsetGet($offset)
74  {
75  return $this->submatchers[$offset];
76  }
77 
79  public function offsetSet($offset, $value)
80  {
81  if (!($value instanceof \Erebot\Interfaces\Event\Match)) {
82  throw new \Erebot\InvalidValueException('Not a valid matcher');
83  }
84  $this->submatchers[$offset] = $value;
85  }
86 
88  public function offsetUnset($offset)
89  {
90  unset($this->submatchers[$offset]);
91  }
92 
105  public function & add($filter)
106  {
107  $filters = func_get_args();
108  foreach ($filters as $filter) {
109  if (!($filter instanceof \Erebot\Interfaces\Event\Match)) {
110  throw new \Erebot\InvalidValueException('Not a valid matcher');
111  }
112  if (!in_array($filter, $this->submatchers, true)) {
113  $this->submatchers[] = $filter;
114  }
115  }
116  return $this;
117  }
118 
132  public function & remove($filter)
133  {
134  $filters = func_get_args();
135  foreach ($filters as $filter) {
136  if (!($filter instanceof \Erebot\Interfaces\Event\Match)) {
137  throw new \Erebot\InvalidValueException('Not a valid matcher');
138  }
139  $key = array_search($filter, $this->submatchers, true);
140  if ($key !== false) {
141  unset($this->submatchers[$key]);
142  }
143  }
144  return $this;
145  }
146 }
Interface to provide accessing objects as arrays.
Definition: ArrayAccess.php:10
$submatchers
Subfilters of this filter.
Definition: CLI.php:21
Interface for an event filter.
Definition: Match.php:30
Abstract class for a filter which groups several (sub-)filters together.
Classes implementing Countable can be used with the count() function.
Definition: Countable.php:11