| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace gotakk\ViewModelBundle\ViewModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | class ViewModelNode implements \ArrayAccess | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     static private $plurals =array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     public static function addPlural($singular, $plural) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |         self::$plurals[$singular] = $plural; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public static function getPlurals() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         return self::$plurals; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function __construct($skel = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         foreach ($skel as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             $this->$key = (is_array($value)) ? new ViewModelNode($value) : $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private function getNewIntIndex() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $vars = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         foreach (get_object_vars($this) as $k => $v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             if (is_numeric($k)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                 $vars[$k] = $v; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         ksort($vars); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         end($vars); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return (count($vars)) ? intval(key($vars)) + 1 : 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function offsetSet($offset, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $index = $this->getNewIntIndex(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         if (is_null($offset)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $this->$index = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $this->$offset = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function offsetExists($offset) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return isset($this->$offset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function offsetUnset($offset) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         unset($this->$offset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     public function offsetGet($offset) | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         return isset($this->$offset) ? $this->$offset : null; | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function add($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $index = $this->getNewIntIndex(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         return $this->$index = (is_array($data)) ? $this->$index = new ViewModelNode($data) : $this->$index = $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public function __call($name, $args) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             preg_match('/^[a-z]*/', $name, $matches); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $action = $matches[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             preg_match('/[A-Z][a-zA-Z]*/', $name, $matches); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $target = lcfirst($matches[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             throw new \BadMethodCallException("Error while parsing method name '$name()'"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         switch ($action) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             case 'get': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 if (isset($this->$target)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                     return $this->$target; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             case 'add': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 $arg = $args[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 $entityName = isset(self::$plurals[$target]) ? self::$plurals[$target] : $target . 's'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 if (isset($this->$entityName) && $this->$entityName instanceof ViewModelNode) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     return $this->{$entityName}->add($arg); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                     $this->$entityName = new ViewModelNode($args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                     return $this->{$entityName}[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             case 'set': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 $arg = $args[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 $this->$target = is_array($arg) ? new ViewModelNode($arg) : $arg; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 throw new \BadMethodCallException("$name(): Undefined method"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     public function toArray() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $arr = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         foreach (get_object_vars($this) as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $arr[$key] = ($value instanceof ViewModelNode) ? $value->toArray() : $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         return $arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  |