From fcafeba6dd94f4f3bff25216a4040c55452c06f9 Mon Sep 17 00:00:00 2001 From: gwleuverink Date: Mon, 26 Aug 2024 14:41:27 +0200 Subject: [PATCH] add PropertyCollection --- src/PropertyCollection.php | 89 ++++++++++++++++++++++++++++++++++++++ src/ServiceProvider.php | 2 +- src/helpers.php | 4 +- tests/Unit/MacroTest.php | 10 +++-- 4 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 src/PropertyCollection.php diff --git a/src/PropertyCollection.php b/src/PropertyCollection.php new file mode 100644 index 0000000..7d2c2f4 --- /dev/null +++ b/src/PropertyCollection.php @@ -0,0 +1,89 @@ +items; + } + + public function keys(): array + { + return array_keys($this->items); + } + + public function values(): array + { + return array_values($this->items); + } + + public function each(callable $callback): self + { + foreach ($this as $key => $item) { + if ($callback($item, $key) === false) { + break; + } + } + + return $this; + } + + /* + |-------------------------------------------------------------------------- + | ArrayAccess/Iterator methods + |-------------------------------------------------------------------------- + */ + public function offsetExists($offset): bool + { + return isset($this->items[$offset]); + } + + public function offsetGet($offset): mixed + { + return $this->items[$offset]; + } + + public function offsetSet($offset, $value): void + { + if (is_null($offset)) { + $this->items[] = $value; + + return; + } + + $this->items[$offset] = $value; + } + + public function offsetUnset($offset): void + { + unset($this->items[$offset]); + } + + public function getIterator(): ArrayIterator + { + return new ArrayIterator($this->items); + } +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index c105b21..9d5db6f 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -19,7 +19,7 @@ public function register() protected function registerGroupMacro() { - Component::macro('group', function (string|array $groups) { + Component::macro('group', function (string|array $groups): PropertyCollection { /** @var Component $this */ return group($this, $groups); }); diff --git a/src/helpers.php b/src/helpers.php index fb5d047..ff8cabd 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -5,7 +5,7 @@ use ReflectionClass; use Livewire\Component; -function group(Component $component, string|array $groups) +function group(Component $component, string|array $groups): PropertyCollection { $groups = (array) $groups; @@ -27,5 +27,5 @@ function group(Component $component, string|array $groups) } } - return $result; + return new PropertyCollection($component, $result); } diff --git a/tests/Unit/MacroTest.php b/tests/Unit/MacroTest.php index a3fffbe..1439619 100644 --- a/tests/Unit/MacroTest.php +++ b/tests/Unit/MacroTest.php @@ -55,7 +55,9 @@ $iterations = 0; - group($component, 'a')->each(fn () => $iterations++); + group($component, 'a')->each(function () use (&$iterations) { + $iterations++; + }); expect($iterations)->toBe(2); }); @@ -70,10 +72,12 @@ public $bar = 2; }; - expect(group($component, 'a')) + $result = group($component, 'a') ->each(function () { // Don't have to do anything here - }) + }); + + expect($result) ->values() ->toBeArray() ->toContain(1, 2);