1010use Respect \Validation \Exceptions \NestedValidationException ;
1111use Respect \Validation \Rules \AllOf ;
1212use Respect \Validation \Rules \Key as ValidationKey ;
13- use Respect \Validation \Rules \KeySet ;
1413
1514abstract class Struct implements \Iterator, \Countable, \JsonSerializable
1615{
@@ -120,7 +119,7 @@ public function get(string $key, $default = null)
120119 */
121120 public function set (string $ key , $ value )
122121 {
123- $ data = $ this -> toArray ( );
122+ $ data = iterator_to_array ( $ this );
124123 $ data [$ key ] = $ value ;
125124 return new static ($ data );
126125 }
@@ -133,35 +132,24 @@ public function set(string $key, $value)
133132 */
134133 public function delete (string $ key )
135134 {
136- $ data = $ this -> toArray ( );
135+ $ data = iterator_to_array ( $ this );
137136 unset($ data [$ key ]);
138137 return new static ($ data );
139138 }
140139
141140 /**
142- * @return array
143- */
144- public function toArray (): array
145- {
146- return array_reduce ($ this ->__defined_props , function ($ carry , $ prop ) {
147- $ carry [$ prop ] = $ this ->{$ prop };
148- return $ carry ;
149- }, []);
150- }
151-
152- /**
153- * @return array
141+ * @return object
154142 */
155143 public function jsonSerialize ()
156144 {
157- $ data = [] ;
145+ $ data = new \ stdClass () ;
158146 foreach (static ::getSchema ()->getProps () as $ prop ) {
159147 if (!$ this ->has ($ prop ->getName ())) {
160148 continue ;
161149 }
162150
163151 $ value = $ this ->get ($ prop ->getName ());
164- $ data[ $ prop ->getName ()] = static ::jsonSerializeValue ($ value );
152+ $ data->{ $ prop ->getName ()} = static ::jsonSerializeValue ($ value );
165153 }
166154
167155 return $ data ;
0 commit comments