| Web Commons | ||||
|
||||
| Coverage | ||||||||||||
| Classes | Functions / Methods | Lines | ||||||||||
| Total |
|
100.00% | 1 / 1 |
|
100.00% | 6 / 6 |
|
100.00% | 16 / 16 | |||
| Entity |
|
100.00% | 1 / 1 |
|
100.00% | 6 / 6 |
|
100.00% | 16 / 16 | |||
| public function __construct($attrs) |
|
100.00% | 1 / 1 |
|
100.00% | 2 / 2 | ||||||
| public function __get($attr) |
|
100.00% | 1 / 1 |
|
100.00% | 3 / 3 | ||||||
| public function __set($attr, $value) |
|
100.00% | 1 / 1 |
|
100.00% | 3 / 3 | ||||||
| public function toArray() |
|
100.00% | 1 / 1 |
|
100.00% | 1 / 1 | ||||||
| public function fromArray($array) |
|
100.00% | 1 / 1 |
|
100.00% | 4 / 4 | ||||||
| private function triggerAttributeError($attr) |
|
100.00% | 1 / 1 |
|
100.00% | 3 / 3 | ||||||
1 : <?php 2 : /** 3 : * This file groups classes pertaining to the "model" part of MVC. 4 : * 5 : * @author Antoine d'Otreppe de Bouvette <a.dotreppe@aspyct.org> 6 : * @license http://www.opensource.org/licenses/mit-license.php 7 : * @version 0.1dev 8 : */ 9 : 10 : /** 11 : * Generic attribute container class. 12 : * Give an associative array to its constructor to define the existing fields. 13 : * @since 0.1 14 : */ 15 : class Entity { 16 : /** 17 : * Defines and contains the existing fields. 18 : * @var mixed[string] 19 : * @since 0.1 20 : */ 21 : private $attrs; 22 : 23 : public function __construct($attrs) { 24 11 : $this->attrs = $attrs; 25 11 : } 26 : 27 : /** 28 : * Returns the value of the attribute $attr. 29 : * @param string $attr 30 : * @throws DomainException if $attr does not exist. 31 : * @return mixed 32 : * @since 0.1 33 : */ 34 : public function __get($attr) { 35 5 : if (array_key_exists($attr, $this->attrs)) 36 5 : return $this->attrs[$attr]; 37 : else 38 2 : return $this->triggerAttributeError($attr); 39 : } 40 : 41 : /** 42 : * Returns the value of the attribute $attr. 43 : * @param string $attr 44 : * @param mixed $value 45 : * @throws DomainException if $attr does not exist. 46 : * @since 0.1 47 : */ 48 : public function __set($attr, $value) { 49 2 : if (array_key_exists($attr, $this->attrs)) 50 2 : return $this->attrs[$attr] = $value; 51 : else 52 1 : return $this->triggerAttributeError($attr); 53 : } 54 : 55 : /** 56 : * Returns the attributes of this class in an associative array. 57 : * @return array 58 : * @since 0.1 59 : */ 60 : public function toArray() { 61 4 : return $this->attrs; 62 : } 63 : 64 : /** 65 : * Sets the value of attributes according to an associative array. 66 : * No attribute will be added or deleted. 67 : * Attributes not present in the array will be set to Null. 68 : * 69 : * @param array $array 70 : */ 71 : public function fromArray($array) { 72 3 : foreach (array_keys($this->attrs) as $attr) { 73 3 : $this->attrs[$attr] = array_get_default($array, $attr, Null); 74 3 : } 75 3 : } 76 : 77 : /** 78 : * Throws an exception stating that no such attribute exists. 79 : * @param string $attr 80 : * @throws DomainException 81 : * @since 0.1 82 : */ 83 : private function triggerAttributeError($attr) { 84 3 : $cls = new ReflectionClass($this); 85 3 : throw new DomainException( 86 3 : 'No attribute "' .$attr. '" in class ' . $cls->getName()); 87 : } 88 : } |
| Generated by PHPUnit 3.4.5 and Xdebug 2.1.0 at Fri Jul 16 0:48:38 CEST 2010. |