2020 * @implements ArrayAccess<int|string, mixed>
2121 * @implements Iterator<int|string, mixed>
2222 */
23- abstract class BaseData implements StoryblokDataInterface, Iterator, ArrayAccess, Countable
23+ abstract class BaseData implements
24+ StoryblokDataInterface,
25+ Iterator,
26+ ArrayAccess,
27+ Countable
2428{
2529 use IterableDataTrait;
2630
@@ -47,8 +51,11 @@ public function setData(array $data): void
4751 $ this ->data = $ data ;
4852 }
4953
50- public function getInt (int |string $ key , int |null $ defaultValue = null , string $ charNestedKey = ". " ): int |null
51- {
54+ public function getInt (
55+ int |string $ key ,
56+ int |null $ defaultValue = null ,
57+ string $ charNestedKey = ". " ,
58+ ): int |null {
5259 $ returnValue = $ this ->get ($ key , null , $ charNestedKey );
5360
5461 if (is_scalar ($ returnValue )) {
@@ -58,8 +65,11 @@ public function getInt(int|string $key, int|null $defaultValue = null, string $c
5865 return $ defaultValue ;
5966 }
6067
61- public function getBoolean (int |string $ key , bool $ defaultValue = false , string $ charNestedKey = ". " ): bool
62- {
68+ public function getBoolean (
69+ int |string $ key ,
70+ bool $ defaultValue = false ,
71+ string $ charNestedKey = ". " ,
72+ ): bool {
6373 $ returnValue = $ this ->get ($ key , $ defaultValue , $ charNestedKey );
6474
6575 if (is_scalar ($ returnValue )) {
@@ -73,12 +83,15 @@ public function getBoolean(int|string $key, bool $defaultValue = false, string $
7383 * @param mixed[] $defaultValue
7484 * @return mixed[]
7585 */
76- public function getArray (int |string $ key , array $ defaultValue = [], string $ charNestedKey = ". " ): array
77- {
86+ public function getArray (
87+ int |string $ key ,
88+ array $ defaultValue = [],
89+ string $ charNestedKey = ". " ,
90+ ): array {
7891 $ returnValue = $ this ->get ($ key , $ defaultValue , $ charNestedKey );
7992
8093 if (is_scalar ($ returnValue )) {
81- return [strval ($ returnValue ) ];
94+ return [strval ($ returnValue )];
8295 }
8396
8497 if ($ returnValue instanceof StoryblokData) {
@@ -118,8 +131,11 @@ public function getFormattedDateTime(
118131 * @param string $charNestedKey The character used for separating nested keys (default: ".").
119132 * @return $this The current instance for method chaining.
120133 */
121- public function set (int |string $ key , mixed $ value , string $ charNestedKey = ". " ): self
122- {
134+ public function set (
135+ int |string $ key ,
136+ mixed $ value ,
137+ string $ charNestedKey = ". " ,
138+ ): self {
123139 if (is_string ($ key )) {
124140 $ array = &$ this ->data ;
125141 if ($ charNestedKey === "" ) {
@@ -180,7 +196,6 @@ protected function returnData(mixed $value, bool $raw = false): mixed
180196 }
181197
182198 return new StoryblokData ([]);
183-
184199 }
185200
186201 /**
@@ -262,8 +277,12 @@ public function dump(): void
262277 * @param bool $raw Whether to return raw data or cast it into StoryblokData if applicable.
263278 * @return mixed The value associated with the key, or the default value if the key does not exist.
264279 */
265- public function get (int |string $ key , mixed $ defaultValue = null , string $ charNestedKey = ". " , bool $ raw = false ): mixed
266- {
280+ public function get (
281+ int |string $ key ,
282+ mixed $ defaultValue = null ,
283+ string $ charNestedKey = ". " ,
284+ bool $ raw = false ,
285+ ): mixed {
267286 if (is_string ($ key )) {
268287 if ($ charNestedKey === "" ) {
269288 $ charNestedKey = ". " ;
@@ -273,7 +292,10 @@ public function get(int|string $key, mixed $defaultValue = null, string $charNes
273292 if (str_contains ($ keyString , $ charNestedKey )) {
274293 $ nestedValue = $ this ->data ;
275294 foreach (explode ($ charNestedKey , $ keyString ) as $ nestedKey ) {
276- if (is_array ($ nestedValue ) && array_key_exists ($ nestedKey , $ nestedValue )) {
295+ if (
296+ is_array ($ nestedValue ) &&
297+ array_key_exists ($ nestedKey , $ nestedValue )
298+ ) {
277299 $ nestedValue = $ nestedValue [$ nestedKey ];
278300 } elseif ($ nestedValue instanceof StoryblokData) {
279301 $ nestedValue = $ nestedValue ->get ($ nestedKey );
@@ -285,18 +307,23 @@ public function get(int|string $key, mixed $defaultValue = null, string $charNes
285307 return $ this ->returnData ($ nestedValue , $ raw );
286308 }
287309
288- if (! array_key_exists ($ key , $ this ->data )) {
310+ if (!array_key_exists ($ key , $ this ->data )) {
289311 return $ defaultValue ;
290312 }
313+ }
291314
315+ if (is_numeric ($ key ) && !array_key_exists ($ key , $ this ->data )) {
316+ return $ defaultValue ;
292317 }
293318
294319 return $ this ->returnData ($ this ->data [$ key ], $ raw ) ?? $ defaultValue ;
295-
296320 }
297321
298- public function getString (int |string $ key , string $ defaultValue = "" , string $ charNestedKey = ". " ): string
299- {
322+ public function getString (
323+ int |string $ key ,
324+ string $ defaultValue = "" ,
325+ string $ charNestedKey = ". " ,
326+ ): string {
300327 $ returnValue = $ this ->get ($ key , "" , $ charNestedKey );
301328
302329 if (is_scalar ($ returnValue )) {
@@ -306,8 +333,11 @@ public function getString(int|string $key, string $defaultValue = "", string $ch
306333 return $ defaultValue ;
307334 }
308335
309- public function getStringNullable (int |string $ key , string |null $ defaultValue = null , string $ charNestedKey = ". " ): string |null
310- {
336+ public function getStringNullable (
337+ int |string $ key ,
338+ string |null $ defaultValue = null ,
339+ string $ charNestedKey = ". " ,
340+ ): string |null {
311341 $ returnValue = $ this ->get ($ key , $ defaultValue , $ charNestedKey );
312342
313343 if (is_scalar ($ returnValue )) {
0 commit comments