1010use Ltaooo \Data \Attribute \DataAttribute ;
1111use Ltaooo \Data \Contract \ArrayAble ;
1212use Ltaooo \Data \Traits \ArrayAccessTrait ;
13+ use Ltaooo \Data \Util \Str ;
1314use ReflectionClass ;
1415use ReflectionProperty ;
15- use function Symfony \Component \String \u ;
1616
1717class Data implements ArrayAble, ArrayAccess, JsonSerializable
1818{
1919 use ArrayAccessTrait;
20- protected ?ReflectionClass $ _staticReflection = null ;
2120
22- protected array $ _strCache = [] ;
21+ protected ? ReflectionClass $ _staticReflection = null ;
2322
23+ /**
24+ * @param array|ArrayAble $data
25+ * @throws
26+ */
2427 public function __construct (array |Arrayable $ data = [])
2528 {
2629 $ this ->fill ($ data instanceof Arrayable ? $ data ->toArray () : $ data );
@@ -66,8 +69,8 @@ public function fill(array $data): static
6669 }
6770 foreach ($ this ->getStaticReflection ()->getProperties () as $ property ) {
6871 $ propertyName = $ property ->getName ();
69- $ camelCasePropertyName = $ this -> strCamel ($ propertyName );
70- $ snakePropertyName = $ this -> strSnake ($ propertyName );
72+ $ camelCasePropertyName = Str:: camel ($ propertyName );
73+ $ snakePropertyName = Str:: snake ($ propertyName );
7174 if (
7275 !array_key_exists ($ camelCasePropertyName , $ data )
7376 && !array_key_exists ($ snakePropertyName , $ data )
@@ -117,7 +120,7 @@ protected function propertyToArray(object $object, bool $toSnake, ReflectionProp
117120 if ($ this ->isInsideProperty ($ property )) {
118121 continue ;
119122 }
120- $ name = $ toSnake ? $ this -> strSnake ($ property ->getName ()) : $ property ->getName ();
123+ $ name = $ toSnake ? Str:: snake ($ property ->getName ()) : $ property ->getName ();
121124 $ result [$ name ] = $ this ->forValue ($ property ->getValue ($ object ), $ toSnake );
122125 }
123126 return $ result ;
@@ -156,28 +159,9 @@ protected function getStaticReflection(): ReflectionClass
156159 return $ this ->_staticReflection ?? $ this ->getReflectionClass ($ this );
157160 }
158161
159- protected function strCamel (string $ str )
160- {
161- if (isset ($ this ->_strCache ['camel ' ][$ str ])) {
162- return $ this ->_strCache ['camel ' ][$ str ];
163- }
164- return $ this ->_strCache ['camel ' ][$ str ] = u ($ str )->camel ()->toString ();
165- }
166-
167- protected function strSnake (string $ str )
168- {
169- if (isset ($ this ->_strCache ['snake ' ][$ str ])) {
170- return $ this ->_strCache ['snake ' ][$ str ];
171- }
172- return $ this ->_strCache ['snake ' ][$ str ] = u ($ str )->snake ()->toString ();
173- }
174-
175162 protected function isInsideProperty (ReflectionProperty $ property ): bool
176163 {
177- if (isset ($ this ->_strCache ['inside ' ][$ property ->getName ()])) {
178- return $ this ->_strCache ['inside ' ][$ property ->getName ()];
179- }
180- return $ this ->_strCache ['inside ' ][$ property ->getName ()] = u ($ property ->getName ())->startsWith ('_ ' );
164+ return Str::startsWith ($ property ->getName (), '_ ' );
181165 }
182166
183167}
0 commit comments