Skip to content

Commit

Permalink
support PropertyCollection access items as class properties
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Aug 27, 2024
1 parent d0b235a commit c60ab0e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ $this->group('a')->each(fn() => /* */);

// Get all grouped properties, excluding non grouped
$this->group();

// Access group as an array or an object
$groupA = $this->group('a');
$groupA['foo'];
$groupA->foo;
```

### Proxying Livewire Methods
Expand Down
5 changes: 5 additions & 0 deletions src/PropertyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public static function make(Component $component, $items = [])
return new static($component, (array) $items);
}

public function __get($key)
{
return $this->items[$key] ?? null;
}

/*
|--------------------------------------------------------------------------
| Collection methods
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/MacroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@
->toContain(1);
});

it('can access property collection like a object', function () {
$component = new class extends TestComponent
{
#[Group('a')]
public $foo = 1;
};

expect(group($component, 'a'))
->foo->toBe(1)
->bar->toBe(null);
});

it('returns array keys when `keys` method was chained', function () {
$component = new class extends TestComponent
{
Expand Down

0 comments on commit c60ab0e

Please sign in to comment.