Skip to content

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

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

Open
wants to merge 14 commits into
base: 7.4
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

@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
@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
Brajk19 and others added 12 commits January 10, 2025 11:49
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.
Co-authored-by: Oskar Stark <oskarstark@googlemail.com>
Co-authored-by: Oskar Stark <oskarstark@googlemail.com>
@Brajk19 Brajk19 force-pushed the propertyaccess-wildcard branch from 54880c1 to c5a0544 Compare January 10, 2025 11:07
@Brajk19 Brajk19 requested a review from welcoMattic as a code owner January 10, 2025 11:07
@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
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.

6 participants