| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * File containing the {@see AppUtils\Traits_Optionable} trait, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * and the matching interface. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @package Application Utils | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @subpackage Traits | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @see Traits_Optionable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @see Interface_Optionable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace AppUtils; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Trait for adding options to a class: allows setting | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * and getting options of all types. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * NOTE: To add this to a class, it must use the trait,  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * but also implement the interface. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @package Application Utils | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @subpackage Traits | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @author Sebastian Mordziol <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @see Interface_Optionable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | trait Traits_Optionable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     * Sets an option to the specified value. This can be any | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     * kind of variable type, including objects, as needed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     * @param mixed $default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function setOption(string $name, $value) : Interface_Optionable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if(!isset($this->options)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $this->options = $this->getDefaultOptions(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->options[$name] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         return $this; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     * Sets a collection of options at once, from an | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     * associative array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     * @return Interface_Optionable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function setOptions(array $options) : Interface_Optionable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         foreach($options as $name => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $this->setOption($name, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         return $this; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     * Retrieves an option's value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     * NOTE: Use the specialized type getters to ensure an option | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     * contains the expected type (for ex. getArrayOption()).  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     * @param mixed $default The default value to return if the option does not exist. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     public function getOption(string $name, $default=null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         if(!isset($this->options)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $this->options = $this->getDefaultOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         if(isset($this->options[$name])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             return $this->options[$name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     * Enforces that the option value is a string. Numbers are converted | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     * to string, strings are passed through, and all other types will  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     * return the default value. The default value is also returned if | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     * the string is empty. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     * @param string $default Used if the option does not exist, is invalid, or empty. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  |     */ | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     public function getStringOption(string $name, string $default='') : string | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         $value = $this->getOption($name, false); | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         if((is_string($value) || is_numeric($value)) && !empty($value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |             return (string)$value; | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |         return $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     * Treats the option value as a boolean value: will return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     * true if the value actually is a boolean true. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     * NOTE: boolean string representations are not accepted. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     public function getBoolOption(string $name, bool $default=false) : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         if($this->getOption($name) === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         return $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     * Treats the option value as an integer value: will return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     * valid integer values (also from integer strings), or the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     * default value otherwise. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     * @param int $default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     public function getIntOption(string $name, int $default=0) : int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $value = $this->getOption($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         if(ConvertHelper::isInteger($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             return (int)$value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         return $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     * Treats an option as an array, and returns its value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     * only if it contains an array - otherwise, an empty | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     * array is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     public function getArrayOption(string $name) : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         $val = $this->getOption($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         if(is_array($val)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             return $val; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         return array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     * Checks whether the specified option exists - even | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     * if it has a NULL value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     public function hasOption(string $name) : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         if(!isset($this->options)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             $this->options = $this->getDefaultOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         return array_key_exists($name, $this->options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     * Returns all options in one associative array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |     * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     public function getOptions() : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         if(!isset($this->options)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |             $this->options = $this->getDefaultOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         return $this->options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     * Checks whether the option's value is the one specified. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |     * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     public function isOption(string $name, $value) : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |         return $this->getOption($name) === $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |  * Interface for classes that use the optionable trait. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |  * The trait itself fulfills most of the interface, but | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |  * it is used to guarantee internal type checks will work, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |  * as well as ensure the abstract methods are implemented. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |  * @package Application Utils | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |  * @subpackage Traits | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |  * @author Sebastian Mordziol <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |  * @see Traits_Optionable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  | interface Interface_Optionable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |     function setOption(string $name, $value) : Interface_Optionable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |     function getOption(string $name, $default=null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |     function setOptions(array $options) : Interface_Optionable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |     function getOptions() : array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |     function isOption(string $name, $value) : bool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |     function hasOption(string $name) : bool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |     function getDefaultOptions() : array; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 230 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 231 |  |  |  |