Skip to content

Commit 6b38e7f

Browse files
committed
Change getArrayGetter to match getArray concepts. Add getNullableArrayGetter
⛑ Made `getArrayGetter` match `getArray` (returns always array instance) and add `getNullableArrayGetter` to return null if data is missing or empty. 🛠 `getArrayGetter` does not return null any more. Use `getNullableArrayGetter` instead to get null if data is missing.
1 parent 5d097a1 commit 6b38e7f

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

docs/content/en/index.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ $value = $data->getRequiredDateTime('key');
146146

147147
> Throws always `NotAnArrayException` exception if value exists but is not an array.
148148
149-
Get always an array event if provided data is missing or if null.
149+
Get always an array even if provided data is missing or if null.
150150

151151
```php
152152
$value = $data->getArray('key');
@@ -155,7 +155,7 @@ $value = $data->getArray('key');
155155
Get nullable array.
156156

157157
```php
158-
$value = $data->getRequiredArray('key');
158+
$value = $data->getNullableArray('key');
159159
```
160160

161161
Get required array that is not empty. Throws `ArrayIsEmptyException` exception if missing.
@@ -168,12 +168,18 @@ $value = $data->getRequiredArray('key');
168168

169169
> Throws always `NotAnArrayException` exception if value exists but is not an array.
170170
171-
Try to get nullable array from data and wrap it in `GetValue` instance.
171+
Get always `GetValue` instance even if provided data is missing or if null.
172172

173173
```php
174174
$value = $data->getArrayGetter('key');
175175
```
176176

177+
Try to get nullable array from data and wrap it in `GetValue` instance.
178+
179+
```php
180+
$value = $data->getNullableArrayGetter('key');
181+
```
182+
177183
Try to get non-empty array from data and wrap it in `GetValue` instance. Throws `ArrayIsEmptyException` exception if
178184
missing.
179185

src/GetValue.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,25 @@ public function getRequiredArray(string $key): array
228228
}
229229

230230
/**
231-
* Ensures that always an array getter will be returned (if missing in $data or if null).
231+
* Get always `GetValue` instance even if provided data is missing or if null.
232232
*/
233-
public function getArrayGetter(string $key): ?self
233+
public function getArrayGetter(string $key): self
234+
{
235+
return new self(new ArrayData($this->getArray($key)), $this->exceptionBuilder, $this->getValidatedValueAction);
236+
}
237+
238+
/**
239+
* Try to get nullable array from data and wrap it in `GetValue` instance.
240+
*/
241+
public function getNullableArrayGetter(string $key): ?self
234242
{
235243
$value = $this->getNullableArray($key);
236244

237245
if ($value === null || $value === []) {
238246
return null;
239247
}
240248

241-
return new self(new ArrayData($value), $this->exceptionBuilder);
249+
return new self(new ArrayData($value), $this->exceptionBuilder, $this->getValidatedValueAction);
242250
}
243251

244252
/**
@@ -248,7 +256,7 @@ public function getRequiredArrayGetter(string $key): self
248256
{
249257
$value = $this->getRequiredArray($key);
250258

251-
return new self(new ArrayData($value), $this->exceptionBuilder);
259+
return new self(new ArrayData($value), $this->exceptionBuilder, $this->getValidatedValueAction);
252260
}
253261

254262
/**

tests/GetValueArrayDataTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,26 @@ public function testGetArrayGetterOnNonArray(): void
5656

5757
public function testGetArrayGetterOnNonExistingItem(): void
5858
{
59-
$this->assertNull($this->data->getArrayGetter(''));
59+
$result = $this->data->getArrayGetter('');
60+
$this->assertInstanceOf(GetValue::class, $result);
61+
$this->assertEmpty($result->data->get());
6062
}
6163

6264
public function testGetArrayGetterOnEmptyArray(): void
6365
{
64-
$this->assertNull($this->data->getArrayGetter(self::KeyItemsEmpty));
66+
$result = $this->data->getArrayGetter(self::KeyItemsEmpty);
67+
$this->assertInstanceOf(GetValue::class, $result);
68+
$this->assertEmpty($result->data->get());
69+
}
70+
71+
public function testGetNullableArrayGetterOnNonExistingItem(): void
72+
{
73+
$this->assertNull($this->data->getNullableArrayGetter(''));
74+
}
75+
76+
public function testGetNullableArrayGetterOnEmptyArray(): void
77+
{
78+
$this->assertNull($this->data->getNullableArrayGetter(self::KeyItemsEmpty));
6579
}
6680

6781
protected function assertItem(

0 commit comments

Comments
 (0)