| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of Properties package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author Serafim <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @date 14.04.2016 13:11 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Serafim\Properties; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Serafim\Properties\Support\Registry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Serafim\Properties\Support\Strings as Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Class Getters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @package Serafim\Properties | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | trait Getters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @return mixed|void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @throws \LogicException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public function __get($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         // Try to call `getProperty` method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $getter = Str::getGetter($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         if (method_exists($this, $getter)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             return $this->{$getter}(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $parser = Registry::get($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         if ($parser->has($name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             $property  = $parser->get($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             $isBoolean = $property->typeOf('bool') || $property->typeOf('boolean'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             $getter    = Str::getBooleanGetter($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             // If boolean - try to call `isProperty` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             if ($isBoolean && method_exists($this, $getter)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 return $this->{$getter}(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             if ($parser->isReadable($name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 // Return declared value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 return $this->getPropertyValue($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $exception = sprintf('Can not read write only property %s::$%s', get_class($this), $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             throw new \LogicException($exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     public function __isset($name) | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         // Try to call `getProperty` method | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         $getter = Str::getGetter($name); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         if (method_exists($this, $getter)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         return Registry::get($this)->has($name); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     private function getPropertyValue($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $reflection = new \ReflectionProperty($this, $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $reflection->setAccessible(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $reflection->getValue($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  |