-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
44a254c
commit fcafeba
Showing
4 changed files
with
99 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters