Created
May 18, 2020 14:33
-
-
Save marcelkohl/509e4aeb38c80d5b8593d6c54c4280a3 to your computer and use it in GitHub Desktop.
php collection that handles countable, iterator array access and indexing with/without key
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
<?php | |
abstract class AbstractCollection implements Countable, Iterator, ArrayAccess | |
{ | |
private $indexes = []; | |
private $values = []; | |
private $position = 0; | |
/** | |
* This constructor is there in order to be able to create a collection with | |
* its values already added | |
* @param array $values | |
*/ | |
public function __construct(array $values = []) | |
{ | |
foreach ($values as $value) { | |
$this->offsetSet('', $value); | |
} | |
} | |
/** | |
* Implementation of method declared in \Countable. | |
* Provides support for count() | |
*/ | |
public function count() | |
{ | |
return count($this->values); | |
} | |
/** | |
* Implementation of method declared in \Iterator | |
* Resets the internal cursor to the beginning of the array | |
*/ | |
public function rewind() | |
{ | |
$this->position = 0; | |
} | |
/** | |
* Implementation of method declared in \Iterator | |
* Used to get the current key (as for instance in a foreach()-structure | |
*/ | |
public function key() | |
{ | |
return $this->position; | |
} | |
/** | |
* Implementation of method declared in \Iterator | |
* Used to get the value at the current cursor position | |
*/ | |
public function current() | |
{ | |
return $this->values[$this->indexes[$this->position]]; | |
} | |
/** | |
* Implementation of method declared in \Iterator | |
* Used to move the cursor to the next position | |
*/ | |
public function next() | |
{ | |
$this->position++; | |
} | |
/** | |
* Implementation of method declared in \Iterator | |
* Checks if the current cursor position is valid | |
*/ | |
public function valid() | |
{ | |
return isset($this->values[$this->indexes[$this->position]]); | |
} | |
/** | |
* Implementation of method declared in \ArrayAccess | |
* Used to be able to use functions like isset() | |
* @param $offset | |
* @return bool | |
*/ | |
public function offsetExists($offset): ?bool | |
{ | |
return isset($this->values[$offset]); | |
} | |
/** | |
* Implementation of method declared in \ArrayAccess | |
* Used for direct access array-like ($collection[$offset]); | |
* @param $offset | |
* @return mixed | |
*/ | |
public function offsetGet($offset) | |
{ | |
return $this->values[$offset]; | |
} | |
/** | |
* Implementation of method declared in \ArrayAccess | |
* Used for direct setting of values | |
* @param $offset | |
* @param $value | |
*/ | |
public function offsetSet($offset, $value) | |
{ | |
if (empty($offset)) { | |
$this->values[] = $value; | |
$this->indexes[] = count($this->values) - 1; | |
} else { | |
$this->values[$offset] = $value; | |
$key = array_search($offset, array_keys($this->values), true); | |
$this->indexes[$key] = $offset; | |
} | |
} | |
/** | |
* Implementation of method declared in \ArrayAccess | |
* Used for unset() | |
* @param $offset | |
*/ | |
public function offsetUnset($offset) | |
{ | |
unset($this->values[$offset]); | |
} | |
/** | |
* @return array | |
*/ | |
public function getIndexes(): array | |
{ | |
return $this->indexes; | |
} | |
/** | |
* @param object $object | |
* @return bool | |
*/ | |
public function contains(object $object) : bool | |
{ | |
return in_array($object, $this->values, TRUE); | |
} | |
/** | |
* @param $key | |
* @return bool | |
*/ | |
public function hasKey($key) : bool | |
{ | |
return in_array($key, $this->indexes, FALSE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment