Skip to content

Commit

Permalink
add PropertyCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Aug 26, 2024
1 parent 44a254c commit fcafeba
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 6 deletions.
89 changes: 89 additions & 0 deletions src/PropertyCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Leuverink\PropertyAttribute;

use Iterator;
use ArrayAccess;
use ArrayIterator;
use IteratorAggregate;
use Livewire\Component;

class PropertyCollection implements ArrayAccess, IteratorAggregate
{
final public function __construct(
private readonly Component $component,

Check failure on line 14 in src/PropertyCollection.php

View workflow job for this annotation

GitHub Actions / duster-lint

Property Leuverink\PropertyAttribute\PropertyCollection::$component is never read, only written.
private array $items = []
) {}

public static function make(Component $component, $items = [])
{
return new static($component, (array) $items);
}

/*
|--------------------------------------------------------------------------
| Collection methods
|--------------------------------------------------------------------------
*/
public function toArray(): array
{
return $this->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);
}
}
2 changes: 1 addition & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
4 changes: 2 additions & 2 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,5 +27,5 @@ function group(Component $component, string|array $groups)
}
}

return $result;
return new PropertyCollection($component, $result);
}
10 changes: 7 additions & 3 deletions tests/Unit/MacroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@

$iterations = 0;

group($component, 'a')->each(fn () => $iterations++);
group($component, 'a')->each(function () use (&$iterations) {
$iterations++;
});

expect($iterations)->toBe(2);
});
Expand All @@ -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);
Expand Down

0 comments on commit fcafeba

Please sign in to comment.