Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Aug 27, 2024
1 parent 9487053 commit 8639e03
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,35 @@ function group(Component $component, string|array|null $groups = null): Property
{
$groups = (array) $groups;

$result = empty($groups)
? all_grouped_properties($component)
: all_named_grouped_properties($component, $groups);

return PropertyCollection::make($component, $result);
}

function all_grouped_properties(Component $component): array
{
$result = [];
$reflection = new ReflectionClass($component);
$componentProperties = $reflection->getProperties();

// No groups given, return all grouped properties
if (empty($groups)) {
foreach ($componentProperties as $property) {
$property->setAccessible(true); // Allows grabbing protected props in php < 8.1
if ($property->getAttributes(Group::class)) {
$result[$property->getName()] = $property->getValue($component);
}
foreach ($componentProperties as $property) {
$property->setAccessible(true); // Allows grabbing protected props in php < 8.1
if ($property->getAttributes(Group::class)) {
$result[$property->getName()] = $property->getValue($component);
}

return PropertyCollection::make($component, $result);
}

// Return properties for given groups
return $result;
}

function all_named_grouped_properties(Component $component, array $groups): array
{
$result = [];
$reflection = new ReflectionClass($component);
$componentProperties = $reflection->getProperties();

foreach ($groups as $group) {
foreach ($componentProperties as $property) {
$property->setAccessible(true); // Allows grabbing protected props in php < 8.1
Expand All @@ -40,5 +52,5 @@ function group(Component $component, string|array|null $groups = null): Property
}
}

return PropertyCollection::make($component, $result);
return $result;
}

0 comments on commit 8639e03

Please sign in to comment.