. */ namespace SP\Core; use ArrayAccess; use ArrayIterator; use Countable; use IteratorAggregate; use Traversable; /** * Class DataCollection * * @package SP\Core\Context */ abstract class DataCollection implements IteratorAggregate, ArrayAccess, Countable { /** * Collection of data attributes */ protected array $attributes = []; /** * Retrieve an external iterator * * @link http://php.net/manual/en/iteratoraggregate.getiterator.php * @return Traversable An instance of an object implementing Iterator or * Traversable * @since 5.0.0 */ public function getIterator(): Traversable { return new ArrayIterator($this->attributes); } /** * Whether a offset exists * * @link http://php.net/manual/en/arrayaccess.offsetexists.php * * @param mixed $offset
* An offset to check for. *
* * @return boolean true on success or false on failure. * ** The return value will be casted to boolean if non-boolean was returned. * @since 5.0.0 */ public function offsetExists($offset): bool { return $this->exists($offset); } /** * See if an attribute exists in the collection * * @param string $key The name of the parameter * * @return boolean */ public function exists(string $key): bool { // Don't use "isset", since it returns false for null values return array_key_exists($key, $this->attributes); } /** * Offset to retrieve * * @link http://php.net/manual/en/arrayaccess.offsetget.php * * @param mixed $offset
* The offset to retrieve. *
* * @return mixed Can return all value types. * @since 5.0.0 */ public function offsetGet($offset): mixed { return $this->get($offset); } /** * Return an attribute of the collection * * Return a default value if the key doesn't exist * * @param string $key The name of the parameter to return * @param mixed $default_val The default value of the parameter if it contains no value * * @return mixed */ public function get(string $key, $default_val = null) { return $this->attributes[$key] ?? $default_val; } /** * Offset to set * * @link http://php.net/manual/en/arrayaccess.offsetset.php * * @param mixed $offset* The offset to assign the value to. *
* @param mixed $value* The value to set. *
* * @return void * @since 5.0.0 */ public function offsetSet($offset, $value): void { $this->set($offset, $value); } /** * Set an attribute of the collection * * @param string $key The name of the parameter to set * @param mixed $value The value of the parameter to set * * @return DataCollection */ public function set(string $key, $value): DataCollection { $this->attributes[$key] = $value; return $this; } /** * Offset to unset * * @link http://php.net/manual/en/arrayaccess.offsetunset.php * * @param mixed $offset* The offset to unset. *
* * @return void * @since 5.0.0 */ public function offsetUnset($offset): void { $this->remove($offset); } /** * Remove an attribute from the collection * * @param string $key The name of the parameter * * @return void */ public function remove(string $key): void { unset($this->attributes[$key]); } /** * Count elements of an object * * @link http://php.net/manual/en/countable.count.php * @return int The custom count as an integer. * ** The return value is cast to an integer. * @since 5.1.0 */ public function count(): int { return count($this->attributes); } /** * Clear the collection's contents * * Semantic alias of a no-argument `$this->replace` call * * @return DataCollection */ public function clear(): DataCollection { return $this->replace(); } /** * Replace the collection's attributes * * @param array $attributes The attributes to replace the collection's with * * @return DataCollection */ public function replace(array $attributes = array()): DataCollection { $this->attributes = $attributes; return $this; } /** * Check if the collection is empty * * @return boolean */ public function isEmpty(): bool { return count($this->attributes) === 0; } /** * Magic "__get" method * * Allows the ability to arbitrarily request an attribute from * this instance while treating it as an instance property * * @param string $key The name of the parameter to return * * @return mixed * @see get() */ public function __get(string $key) { return $this->get($key); } /** * Magic "__set" method * * Allows the ability to arbitrarily set an attribute from * this instance while treating it as an instance property * * @param string $key The name of the parameter to set * @param mixed $value The value of the parameter to set * * @return void * @see set() */ public function __set(string $key, $value) { $this->set($key, $value); } /** * Magic "__isset" method * * Allows the ability to arbitrarily check the existence of an attribute * from this instance while treating it as an instance property * * @param string $key The name of the parameter * * @return boolean * @see exists() */ public function __isset(string $key) { return $this->exists($key); } /** * Magic "__unset" method * * Allows the ability to arbitrarily remove an attribute from * this instance while treating it as an instance property * * @param string $key The name of the parameter * * @return void * @see remove() * */ public function __unset(string $key) { $this->remove($key); } }