Erebot  latest
A modular IRC bot for PHP 7.0+
EventHandler.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;
22 
31 class EventHandler implements \Erebot\Interfaces\EventHandler
32 {
34  protected $callback;
35 
37  protected $filter;
38 
52  public function __construct(
53  callable $callback,
54  \Erebot\Interfaces\Event\Match $filter = null
55  ) {
56  $this->setCallback($callback);
57  $this->setFilter($filter);
58  }
59 
61  public function __destruct()
62  {
63  }
64 
65  public function setCallback(callable $callback)
66  {
67  $this->callback = $callback;
68  return $this;
69  }
70 
71  public function getCallback()
72  {
73  return $this->callback;
74  }
75 
76  public function setFilter(\Erebot\Interfaces\Event\Match $filter = null)
77  {
78  $this->filter = $filter;
79  return $this;
80  }
81 
82  public function getFilter()
83  {
84  return $this->filter;
85  }
86 
87  public function handleEvent(\Erebot\Interfaces\Event\Base\Generic $event)
88  {
89  $matched = true;
90 
91  if ($this->filter !== null) {
92  $matched = $this->filter->match($event);
93  }
94 
95  $cb = $this->callback;
96  return ($matched ? $cb($this, $event) : null);
97  }
98 }
__construct(callable $callback,\Erebot\Interfaces\Event\Match $filter=null)
$callback
Callable object to use when this handler is triggered.
__destruct()
Destructor.
An event handler which will call a callback function/method whenever a set of conditions are met...
$filter
Filtering object to decide whether the callback must be called or not.