Support wildcards when turning into array

This commit is contained in:
Andrea Marco Sartori 2023-08-06 17:29:27 +02:00
parent a752b191af
commit d755ff1f73
4 changed files with 312 additions and 51 deletions

View File

@ -340,6 +340,8 @@ foreach ($json as $key => $value) {
}
```
> If your wrapper class implements the method `toArray()`, such method will be called when eager loading sub-trees into an array.
Lazy pointers also have all the other functionalities of normal pointers: they accept callbacks, can be set one by one or all together, can be eager loaded into an array and can be mixed with normal pointers as well:
```php

View File

@ -113,13 +113,21 @@ final class Parser implements IteratorAggregate
*/
public function toArray(): array
{
$index = 0;
$array = [];
$hasWildcards = false;
foreach ($this as $key => $value) {
$array[$key] = $value instanceof self ? $value->toArray() : $value;
if (isset($array[$index][$key])) {
$index++;
$hasWildcards = true;
}
$turnsIntoArray = is_object($value) && method_exists($value, 'toArray');
$array[$index][$key] = $turnsIntoArray ? $value->toArray() : $value;
}
return $array;
return $hasWildcards || empty($array) ? $array : $array[0];
}
/**

View File

@ -4,44 +4,168 @@ return [
'complex_array' => [
'/-1,/-2' => [],
'/-/id,/-/batters/batter/-/type' => [
'id' => '0003',
'type' => 'Chocolate',
[
'id' => '0001',
'type' => 'Regular',
],
[
'type' => 'Chocolate',
],
[
'type' => 'Blueberry',
],
[
'type' => 'Devil\'s Food',
'id' => '0002',
],
[
'type' => 'Regular',
'id' => '0003',
],
[
'type' => 'Regular',
],
[
'type' => 'Chocolate',
],
],
'/-/name,/-/topping/-/type,/-/id' => [
'id' => '0003',
'name' => 'Old Fashioned',
'type' => 'Maple',
[
'id' => '0001',
'name' => 'Cake',
'type' => 'None',
],
[
'type' => 'Glazed',
],
[
'type' => 'Sugar',
],
[
'type' => 'Powdered Sugar',
],
[
'type' => 'Chocolate with Sprinkles',
],
[
'type' => 'Chocolate',
],
[
'type' => 'Maple',
'id' => '0002',
'name' => 'Raised',
],
[
'type' => 'None',
],
[
'type' => 'Glazed',
],
[
'type' => 'Sugar',
],
[
'type' => 'Chocolate',
],
[
'type' => 'Maple',
'id' => '0003',
'name' => 'Old Fashioned',
],
[
'type' => 'None',
],
[
'type' => 'Glazed',
],
[
'type' => 'Chocolate',
],
[
'type' => 'Maple',
],
],
'/-/batters/batter/-,/-/name' => [
'name' => 'Old Fashioned',
[
"id" => "1001",
"type" => "Regular",
'name' => 'Cake',
[
'id' => '1001',
'type' => 'Regular',
],
[
'id' => '1002',
'type' => 'Chocolate',
],
[
'id' => '1003',
'type' => 'Blueberry',
],
[
'id' => '1004',
'type' => 'Devil\'s Food',
],
],
[
"id" => "1002",
"type" => "Chocolate",
'name' => 'Raised',
[
'id' => '1001',
'type' => 'Regular',
],
],
[
"id" => "1003",
"type" => "Blueberry",
],
[
"id" => "1004",
"type" => "Devil's Food",
'name' => 'Old Fashioned',
[
'id' => '1001',
'type' => 'Regular',
],
[
'id' => '1002',
'type' => 'Chocolate',
],
],
],
],
'complex_object' => [
'/-1,/-2' => [],
'/id,/batters/batter/-/type' => [
'id' => '0001',
'type' => "Devil's Food",
[
'id' => '0001',
'type' => 'Regular',
],
[
'type' => 'Chocolate',
],
[
'type' => 'Blueberry',
],
[
'type' => 'Devil\'s Food',
],
],
'/name,/topping/-/type,/id' => [
'id' => '0001',
'name' => 'Cake',
'type' => 'Maple',
[
'id' => '0001',
'name' => 'Cake',
'type' => 'None',
],
[
'type' => 'Glazed',
],
[
'type' => 'Sugar',
],
[
'type' => 'Powdered Sugar',
],
[
'type' => 'Chocolate with Sprinkles',
],
[
'type' => 'Chocolate',
],
[
'type' => 'Maple',
],
],
'/batters/batter/-,/type' => [
'type' => 'donut',

View File

@ -4,52 +4,166 @@ return [
'complex_array' => [
'' => $complexArray = require __DIR__ . '/../parsing/complex_array.php',
'/-' => $complexArray,
'/-/id' => ['id' => '0003'],
'/-/id' => [
[
'id' => '0001',
],
[
'id' => '0002',
],
[
'id' => '0003',
],
],
'/-/batters' => [
'batters' => [
'batter' => [
[
"id" => "1001",
"type" => "Regular",
[
'batters' => [
'batter' => [
[
'id' => '1001',
'type' => 'Regular',
],
[
'id' => '1002',
'type' => 'Chocolate',
],
[
'id' => '1003',
'type' => 'Blueberry',
],
[
'id' => '1004',
'type' => 'Devil\'s Food',
],
],
[
"id" => "1002",
"type" => "Chocolate",
],
],
[
'batters' => [
'batter' => [
[
'id' => '1001',
'type' => 'Regular',
],
],
],
],
[
'batters' => [
'batter' => [
[
'id' => '1001',
'type' => 'Regular',
],
[
'id' => '1002',
'type' => 'Chocolate',
],
],
],
],
],
'/-/batters/batter' => [
'batter' => [
[
"id" => "1001",
"type" => "Regular",
[
'batter' => [
[
'id' => '1001',
'type' => 'Regular',
],
[
'id' => '1002',
'type' => 'Chocolate',
],
[
'id' => '1003',
'type' => 'Blueberry',
],
[
'id' => '1004',
'type' => 'Devil\'s Food',
],
],
[
"id" => "1002",
"type" => "Chocolate",
],
[
'batter' => [
[
'id' => '1001',
'type' => 'Regular',
],
],
],
[
'batter' => [
[
'id' => '1001',
'type' => 'Regular',
],
[
'id' => '1002',
'type' => 'Chocolate',
],
],
],
],
'/-/batters/batter/-' => [
[
"id" => "1001",
"type" => "Regular",
[
'id' => '1001',
'type' => 'Regular',
],
[
'id' => '1002',
'type' => 'Chocolate',
],
[
'id' => '1003',
'type' => 'Blueberry',
],
[
'id' => '1004',
'type' => 'Devil\'s Food',
],
],
[
"id" => "1002",
"type" => "Chocolate",
[
'id' => '1001',
'type' => 'Regular',
],
],
[
"id" => "1003",
"type" => "Blueberry",
],
[
"id" => "1004",
"type" => "Devil's Food",
[
'id' => '1001',
'type' => 'Regular',
],
[
'id' => '1002',
'type' => 'Chocolate',
],
],
],
'/-/batters/batter/-/id' => [
[
'id' => '1001',
],
[
'id' => '1002',
],
[
'id' => '1003',
],
[
'id' => '1004',
],
[
'id' => '1001',
],
[
'id' => '1001',
],
[
'id' => '1002',
],
],
'/-/batters/batter/-/id' => ['id' => "1002"],
],
'complex_object' => [
'' => require __DIR__ . '/../parsing/complex_object.php',
@ -115,7 +229,20 @@ return [
"type" => "Devil's Food",
],
],
'/batters/batter/-/id' => ['id' => "1004"],
'/batters/batter/-/id' => [
[
'id' => '1001',
],
[
'id' => '1002',
],
[
'id' => '1003',
],
[
'id' => '1004',
],
],
],
'empty_array' => [
'' => [],