| 1 |  |  | <?php namespace Stevenmaguire\Uber; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | trait GetSetTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |      * Attempts to magically handle getter and setter methods. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |      * @param    string   $method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |      * @param    array    $parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @return   mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @throws   Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 | 46 |  |     public function __call($method, $parameters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 | 46 |  |         $property = $this->convertMethodToProperty($method); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 46 |  |         if ($this->isGetMethod($method)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 18 |  |             return $this->getAttribute($property); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 32 |  |         } elseif ($this->isSetMethod($method)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 28 |  |             return $this->updateAttribute($property, $parameters[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         } // @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 4 |  |         throw new Exception($method . ' method not implemented'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * Attempts to parse a method name and format its related property name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param    string   $method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @return   string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 46 |  |     public function convertMethodToProperty($method) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 46 |  |         $property = preg_replace("/[g|s]{1}et(.*)/", "$1", $method); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 46 |  |         return strtolower(preg_replace('/(.)(?=[A-Z])/', '$1'.'_', $property)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Fetches a specific attribute of the current object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param    string   $attribute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return   mixed|null | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 18 |  |     private function getAttribute($attribute) | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 18 |  |         if (property_exists($this, $attribute)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 16 |  |             return $this->$attribute; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 2 |  |         throw new Exception($attribute . ' attribute not defined'); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * Checks if given method name is a valid getter method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param    string   $method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return   boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 46 |  |     public function isGetMethod($method) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 46 |  |         return preg_match("/^get[A-Za-z]+$/", $method); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * Checks if given method name is a valid setter method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param    string   $method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @return   boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 32 |  |     public function isSetMethod($method) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 32 |  |         return preg_match("/^set[A-Za-z]+$/", $method); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * Updates a specific attribute of the current object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param    string                   $attribute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param    string|boolean|integer   $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @return   object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 94 |  |     private function updateAttribute($attribute, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 94 |  |         if (property_exists($this, $attribute)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 94 |  |             $this->$attribute = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 94 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 94 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  |