@@ -22,13 +22,25 @@ abstract class Base
2222 'array ' => true ,
2323 ];
2424
25- public function __construct ()
25+ public function __construct (array $ initialValues = [] )
2626 {
2727 foreach (static ::$ fields as $ field => $ type )
2828 {
29- if (! \is_array ( $ type ) && ! isset (self :: $ scalars [ $ type ]))
29+ if (isset ($ initialValues [ $ field ]))
3030 {
31- $ this ->data [$ field ] = new $ type ();
31+ $ this ->setFields [$ field ] = true ;
32+ $ this ->data [$ field ] = $ initialValues [$ field ];
33+ }
34+ elseif (! \is_array ($ type ) && ! isset (self ::$ scalars [$ type ]))
35+ {
36+ if (str_starts_with ($ type , 'array ' ))
37+ {
38+ $ this ->data [$ field ] = [];
39+ }
40+ else
41+ {
42+ $ this ->data [$ field ] = new $ type ();
43+ }
3244 }
3345 }
3446 }
@@ -56,6 +68,7 @@ public function __set(string $field, $value)
5668 {
5769 throw new \PHPFUI \ConstantContact \Exception \InvalidField (static ::class . ":: {$ field } is not a valid field " );
5870 }
71+
5972 $ type = \get_debug_type ($ value );
6073
6174 if (\is_array ($ expectedType ))
@@ -65,9 +78,42 @@ public function __set(string $field, $value)
6578 throw new \PHPFUI \ConstantContact \Exception \InvalidValue (static ::class . ":: {$ field } is {$ value } but must be one of " . \implode (', ' , $ expectedType ));
6679 }
6780 }
68- elseif ( $ expectedType != $ type )
81+ else
6982 {
70- throw new \PHPFUI \ConstantContact \Exception \InvalidType (static ::class . ":: {$ field } is of type {$ type } but should be type {$ expectedType }" );
83+ $ expectedType = trim ($ expectedType , '\\' );
84+ if ($ type == 'array ' && str_starts_with ($ expectedType , 'array ' ))
85+ {
86+ $ arrayStart = strpos ($ expectedType , '[ ' );
87+ if ($ arrayStart )
88+ {
89+ $ arrayEnd = strpos ($ expectedType , '] ' );
90+ if (strlen ($ expectedType ) > $ arrayEnd + 1 )
91+ {
92+ $ maxSize = (int )trim (substr ($ expectedType , $ arrayEnd ), '[] ' );
93+ if (count ($ value ) > $ maxSize )
94+ {
95+ throw new \PHPFUI \ConstantContact \Exception \InvalidValue (static ::class . ":: {$ field } array has a limit of {$ maxSize } values " );
96+ }
97+ }
98+ $ expectedType = trim (substr ($ expectedType , $ arrayStart + 2 , $ arrayEnd - $ arrayStart - 2 ), '\\' );
99+ }
100+ else
101+ {
102+ $ expectedType = 'string ' ;
103+ }
104+ foreach ($ value as $ index => $ element )
105+ {
106+ $ elementType = \get_debug_type ($ element );
107+ if ($ expectedType != $ elementType )
108+ {
109+ throw new \PHPFUI \ConstantContact \Exception \InvalidType (static ::class . ":: {$ field } should be an array[ {$ expectedType }] but index {$ index } is of type {$ elementType }" );
110+ }
111+ }
112+ }
113+ elseif ($ expectedType != $ type )
114+ {
115+ throw new \PHPFUI \ConstantContact \Exception \InvalidType (static ::class . ":: {$ field } is of type {$ type } but should be type {$ expectedType }" );
116+ }
71117 }
72118
73119 if (isset (static ::$ minLength [$ field ]))
@@ -114,9 +160,32 @@ public function getData() : array
114160 {
115161 if ($ value instanceof self)
116162 {
117- $ value = $ value ->getData ();
163+ $ result [$ field ] = $ value ->getData ();
164+ }
165+ elseif (is_array ($ value ))
166+ {
167+ if (! count ($ value ))
168+ {
169+ continue ;
170+ }
171+ $ result [$ field ] = [];
172+ foreach ($ value as $ item )
173+ {
174+ if ($ item instanceof self)
175+ {
176+ $ item = $ item ->getData ();
177+ }
178+ elseif (is_object ($ item ))
179+ {
180+ $ item = (string )$ item ;
181+ }
182+ $ result [$ field ][] = $ item ;
183+ }
184+ }
185+ else
186+ {
187+ $ result [$ field ] = is_object ($ value ) ? (string )$ value : $ value ;
118188 }
119- $ result [$ field ] = $ value ;
120189 }
121190 }
122191
@@ -136,3 +205,4 @@ public function getfields() : array
136205 return static ::$ fields ;
137206 }
138207 }
208+
0 commit comments