| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Benrowe\Properties; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Closure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Defines a unique property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @package Benrowe\Properties | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @todo add support for validating a property's value when being set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class Property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $type = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $default = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $value = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $setter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $getter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     const TYPES = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             'string', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             'integer', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             'float', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             'boolean', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             'array', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             'object', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 30 |  |             'null', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             'resource', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 30 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 30 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 30 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 30 |  |      * Create a new Property Instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param string $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param string $default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 30 |  |     public function __construct(string $name, string $type = null, $default = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->setName($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 30 |  |         $this->setType($type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 30 |  |         $this->setDefault($default); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * Set the property name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param string $name the name of the property | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 3 |  |     public function setName(string $name) | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 3 |  |         // add name validation | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $this->name = $name; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * Get the property name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 30 |  |     public function getName(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 30 |  |         return $this->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 27 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 27 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * Set the type for the property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param string $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 6 |  |      * @todo add support for interface/class checking! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     public function setType($type) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         if ($type === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             // no type set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $this->type = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 6 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 6 |  |         $type = strtolower($type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         if (!in_array($type, self::TYPES, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             throw new PropertyException('Invalid type'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 6 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 6 |  |         $this->type = $type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * Get the property type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 3 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public function getType() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 3 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         return $this->type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * Set the default value of the property if nothing is explicitly set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param mixed $default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 30 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     public function setDefault($default) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 30 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 30 |  |         $this->default = $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * Get the default value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 3 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     public function getDefault() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 3 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         return $this->default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * Set the value against the property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 9 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     public function setValue($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 9 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 3 |  |         if ($this->setter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $value = call_user_func($this->setter, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 9 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 9 |  |         $this->value = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * Get the currently set value, if no value is set the default is used | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 12 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     public function getValue() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 12 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 6 |  |         if ($this->value === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             return $this->default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 9 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 9 |  |         $value = $this->value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 3 |  |         if ($this->getter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             $value = call_user_func($this->getter, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 9 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * Inject a custom closure to handle the storage of the value when it is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * set into the property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * @param  Closure $setter the custom function to run when the value is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * being set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * @return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 3 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     public function setter(Closure $setter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 3 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $this->setter = $setter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 3 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * Specify a custom closer to handle the retreival of the value stored | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * against this property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * @param  Closure $getter [description] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * @return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 3 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     public function getter(Closure $getter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 3 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         $this->getter = $getter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 3 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 178 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 179 |  |  |  |