Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PropertyAccess] Allow usage of wildcard [*] #52723

Open
wants to merge 12 commits into
base: 7.1
Choose a base branch
from

Conversation

Brajk19
Copy link
Contributor

@Brajk19 Brajk19 commented Nov 24, 2023

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? yes
Issues
License MIT

This feature allows usage of wildcards to read values from multiple arrays/objects.

Examples:

$persons = [
    [
        'first_name' => 'Wouter',
    ],
    [
        'first_name' => 'Ryan',
    ],
];

var_dump($propertyAccessor->getValue($persons, '[*][first_name]')); // ['Wouter', 'Ryan']
$persons = [
    [
        'first_name' => 'Wouter',
        'pets' => ['Tom', 'Jerry']
    ],
    [
        'first_name' => 'Ryan',
        'pets' => ['Garfield']
    ],
];

var_dump($propertyAccessor->getValue($persons, '[*][pets]')); // ['Tom', 'Jerry', 'Garfield']
$persons = [
    [
        'first_name' => 'Wouter',
        'pets' => [
            'cats' => ['Tom', 'Garfield'],
            'mouses' => ['Jerry']
        ]
    ],
    [
        'first_name' => 'Ryan',
        'pets' => [
            'cats' => ['Sylvester', 'Felix'],
            'mouses' => ['Speedy Gonzales']
        ]
    ],
];

var_dump($propertyAccessor->getValue($persons, '[*][pets][mouses]')); // ['Jerry', 'Speedy Gonzales']
$persons = [
    [
        'first_name' => 'Wouter',
        'pets' => [
            [
                'name' => 'Tom',
                'type' => 'cat',
            ],
            [
                'name' => 'Garfield',
                'type' => 'cat',
            ],
            [
                'name' => 'Jerry',
                'type' => 'mouse',
            ]
        ]
    ],
    [
        'first_name' => 'Ryan',
        'pets' => [
            [
                'name' => 'Sylvester',
                'type' => 'cat',
            ],
            [
                'name' => 'Felix',
                'type' => 'cat',
            ],
            [
                'name' => 'Speedy Gonzales',
                'type' => 'mouse',
            ]
        ]
    ],
];
var_dump($propertyAccessor->getValue($persons, '[*][pets][*][name]')); // ['Tom', 'Garfield', 'Jerry', 'Sylvester', 'Felix', 'Speedy Gonzales']
var_dump($propertyAccessor->getValue($persons, '[*][pets][*][type]')); // ['cat', 'cat', 'mouse', 'cat', 'cat', 'mouse']
var_dump($propertyAccessor->getValue($persons, '[0][pets][*][*]')); // ['Tom', 'cat', 'Garfield', 'cat', 'Jerry', 'mouse']

More examples in PropertyAccessorWildcardTest.

If there is * key in array it should be accesed with [\*]

How does it work?
Whenever [*] is detected, it is replaced with all possible values:

  • 1st example: [*][first_name] becomes [0][first_name] and [1][first_name]
  • readPropertiesUntil is then recursively called for each of them whenever wildcard is detected
  • returned paths from each of them are merged together

Notes:

  • public function isWildcard(int $index): bool should be added to PropertyPathInterface in next major version
  • currently deprecation is triggered when method does not exists in class implementing that interface

welcoMattic and others added 6 commits November 24, 2023 20:47
New property `$isWildcard` is added which denotes whether certain
element is wildcard.
Element is registered as wildcard if it's `[*]`.
Method isWildcard should be added to PropertyPathInterface in next major
release.
Whenever wildcard is in property path, that wildcard is replaced with
all possible values and same method `readPropertyUntil` is recursively
called.
All returned subpaths are then merged together.
@carsonbot carsonbot added this to the 7.1 milestone Nov 24, 2023
@Brajk19 Brajk19 changed the title [PropertyAccess] Add option to use [PropertyAccess] Allow usage of wildcard [*] Nov 24, 2023
@OskarStark OskarStark changed the title [PropertyAccess] Allow usage of wildcard [*] [PropertyAccess] Allow usage of wildcard [*] Nov 24, 2023
Co-authored-by: Oskar Stark <oskarstark@googlemail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants