|
|
|
@ -20,6 +20,18 @@ use JsonSerializable;
|
|
|
|
|
*/
|
|
|
|
|
class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* This is to allow for reset() to work properly.
|
|
|
|
|
*
|
|
|
|
|
* WARNING! This MUST be the first variable in this class!!!
|
|
|
|
|
*
|
|
|
|
|
* PHPStan is ignoring this because we don't need actually to read the property
|
|
|
|
|
*
|
|
|
|
|
* @var mixed
|
|
|
|
|
* @phpstan-ignore-next-line
|
|
|
|
|
*/
|
|
|
|
|
private $first_property = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Collection data.
|
|
|
|
|
*
|
|
|
|
@ -35,6 +47,7 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
|
|
|
|
|
public function __construct(array $data = [])
|
|
|
|
|
{
|
|
|
|
|
$this->data = $data;
|
|
|
|
|
$this->handleReset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -55,6 +68,7 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
|
|
|
|
|
public function __set(string $key, $value): void
|
|
|
|
|
{
|
|
|
|
|
$this->data[$key] = $value;
|
|
|
|
|
$this->handleReset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -71,6 +85,7 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
|
|
|
|
|
public function __unset(string $key): void
|
|
|
|
|
{
|
|
|
|
|
unset($this->data[$key]);
|
|
|
|
|
$this->handleReset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -100,6 +115,7 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
|
|
|
|
|
} else {
|
|
|
|
|
$this->data[$offset] = $value;
|
|
|
|
|
}
|
|
|
|
|
$this->handleReset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -120,6 +136,17 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
|
|
|
|
|
public function offsetUnset($offset): void
|
|
|
|
|
{
|
|
|
|
|
unset($this->data[$offset]);
|
|
|
|
|
$this->handleReset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is to allow for reset() of a Collection to work properly.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function handleReset()
|
|
|
|
|
{
|
|
|
|
|
$this->first_property = reset($this->data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -207,6 +234,7 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
|
|
|
|
|
public function setData(array $data): void
|
|
|
|
|
{
|
|
|
|
|
$this->data = $data;
|
|
|
|
|
$this->handleReset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[\ReturnTypeWillChange]
|
|
|
|
|