1 | <?php |
||
63 | class DataBag extends stdClass implements DataContainer |
||
64 | { |
||
65 | /** |
||
66 | * magic method, called when there's an attempt to get a property |
||
67 | * that doesn't actually exist |
||
68 | * |
||
69 | * if $propertyName is a dot.notation.support path, we'll attempt to |
||
70 | * retrieve the property from the data bag's children |
||
71 | * |
||
72 | * @param string $propertyName |
||
73 | * name of the property being read |
||
74 | * @return mixed |
||
75 | * |
||
76 | * @throws E4xx_NoSuchProperty |
||
77 | */ |
||
78 | public function __get($propertyName) |
||
79 | { |
||
80 | // is the user trying to use dot.notation? |
||
81 | if (IsDotNotationPath::checkString($propertyName)) { |
||
82 | return FilterDotNotationPath::fromObject($this, $propertyName); |
||
83 | } |
||
84 | |||
85 | // if we get here, then we have no idea what you are trying to get |
||
86 | throw new E4xx_NoSuchProperty($this, $propertyName); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * magic method, called when there's an attempt to set a property that |
||
91 | * doesn't actually exist |
||
92 | * |
||
93 | * if $propertyName is a dot.notation.support path, we'll attempt to |
||
94 | * set the property using the path |
||
95 | * |
||
96 | * @param string $propertyName |
||
97 | * name of the property to set |
||
98 | * @param mixed $propertyValue |
||
99 | * value of the property to set |
||
100 | * @return void |
||
101 | */ |
||
102 | public function __set($propertyName, $propertyValue) |
||
103 | { |
||
104 | // is the user trying to use dot.notation? |
||
105 | if (IsDotNotationPath::checkString($propertyName)) { |
||
106 | return MergeUsingDotNotationPath::intoObject($this, $propertyName, $propertyValue, DataBag::class); |
||
107 | } |
||
108 | |||
109 | // if we get here, then we simply have a new property to set |
||
110 | // |
||
111 | // I hope that it does not recurse! |
||
112 | $this->$propertyName = $propertyValue; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * magic method, called when we want to know if a fake property exists |
||
117 | * or not |
||
118 | * |
||
119 | * if $propertyName is a dot.notation.support path, we'll attempt to |
||
120 | * follow it to find the stated property |
||
121 | * |
||
122 | * @param string $propertyName |
||
123 | * name of the property to search for |
||
124 | * @return boolean |
||
125 | * TRUE if the property exists (or is emulated) |
||
126 | * FALSE otherwise |
||
127 | */ |
||
128 | public function __isset($propertyName) |
||
129 | { |
||
130 | // is the user trying to use dot.notation? |
||
131 | if (IsDotNotationPath::checkString($propertyName)) { |
||
132 | return HasUsingDotNotationPath::inObject($this, $propertyName); |
||
133 | } |
||
134 | |||
135 | // if we get here, the property does not exist |
||
136 | return false; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * magic method, called when we want to delete a fake property |
||
141 | * |
||
142 | * @param string $propertyName |
||
143 | * the property to remove |
||
144 | */ |
||
145 | public function __unset($propertyName) |
||
146 | { |
||
147 | RemoveUsingDotNotationPath::from($this, $propertyName); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * magic method, called after we have been cloned |
||
152 | * |
||
153 | * we go all-in and perform a deep clone |
||
154 | * |
||
155 | * @return void |
||
156 | */ |
||
157 | public function __clone() |
||
158 | { |
||
159 | $properties = get_object_vars($this); |
||
160 | foreach ($properties as $propertyName => $propertyValue) { |
||
161 | if (is_object($propertyValue)) { |
||
162 | $this->$propertyName = clone $propertyValue; |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * support foreach() loops over our data |
||
169 | * |
||
170 | * @return \Traversable |
||
171 | */ |
||
172 | public function getIterator() |
||
176 | } |
||
177 |