. */ 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 * * @type array */ protected $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() { 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) { 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($key) { // 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) { 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($key, $default_val = null) { if (isset($this->attributes[$key])) { return $this->attributes[$key]; } return $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) { $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($key, $value) { $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) { $this->remove($offset); } /** * Remove an attribute from the collection * * @param string $key The name of the parameter * * @return void */ public function remove($key) { 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() { return count($this->attributes); } /** * Clear the collection's contents * * Semantic alias of a no-argument `$this->replace` call * * @return DataCollection */ public function clear() { 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()) { $this->attributes = $attributes; return $this; } /** * Check if the collection is empty * * @return boolean */ public function isEmpty() { return empty($this->attributes); } /** * 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($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($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($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($key) { $this->remove($key); } }