Erebot  latest
A modular IRC bot for PHP 7.0+
Module.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\Config;
22 
30 class Module implements \Erebot\Interfaces\Config\Module
31 {
33  protected $params;
34 
36  protected $active;
37 
39  protected $name;
40 
48  public function __construct(\SimpleXMLElement $xml)
49  {
50  $this->name = (string) $xml['name'];
51  $this->params = array();
52  $active = strtolower(
53  isset($xml['active']) ?
54  (string) $xml['active'] :
55  'true'
56  );
57  $this->active = in_array($active, array('1', 'true', 'on', 'yes'));
58 
59  foreach ($xml->param as $param) {
60  $prm = (string) $param['name'];
61  $val = (string) $param['value'];
62  $this->params[$prm] = $val;
63  }
64  }
65 
69  public function __destruct()
70  {
71  }
72 
74  public function isActive($active = null)
75  {
76  $res = $this->active;
77  if ($active !== null) {
78  if (!is_bool($active)) {
79  throw new \Erebot\InvalidValueException(
80  'Invalid activation value'
81  );
82  }
83  $this->active = $active;
84  }
85  return $res;
86  }
87 
89  public function getName()
90  {
91  return $this->name;
92  }
93 
95  public function getParam($param)
96  {
97  if (!is_string($param)) {
98  throw new \Erebot\InvalidValueException('Bad parameter name');
99  }
100  if (!isset($this->params[$param])) {
101  throw new \Erebot\NotFoundException('No such parameter');
102  }
103  return $this->params[$param];
104  }
105 
107  public function getParamsNames()
108  {
109  return array_keys($this->params);
110  }
111 }
__construct(\SimpleXMLElement $xml)
Definition: Module.php:48
isActive($active=null)
Definition: Module.php:74
getParam($param)
Definition: Module.php:95
$params
A dictionary mapping parameter names to their textual value.
Definition: Module.php:33
This class stores configuration data about modules.
Definition: Module.php:30
$name
The name of the module.
Definition: Module.php:39
$active
A boolean indicating whether the module is active or not.
Definition: Module.php:36