-
-
Save teresko/0a564827072887985c6e to your computer and use it in GitHub Desktop.
Superclass for the collections
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 | |
namespace Components; | |
abstract class Collection extends DomainObject implements \Iterator, \ArrayAccess, \Countable | |
{ | |
abstract protected function buildItem(); | |
protected $pool = []; | |
protected $forRemoval = [ | |
'pool' => [], | |
'keys' => [], | |
]; | |
protected $amount = 0; | |
private $position = 0; | |
public function addItem($parameters) | |
{ | |
$instance = $this->buildItem(); | |
foreach ($parameters as $key => $value) { | |
$method = 'set' . str_replace('_', '', $key); | |
if (method_exists($instance, $method)) { | |
$instance->{$method}($value); | |
} | |
} | |
$this->amount += 1; | |
$this->pool[] = $instance; | |
return $instance; | |
} | |
public function removeItem($id) | |
{ | |
if (isset($this->pool[$id])) { | |
$this->forRemoval['pool'][] = $this->pool[$id]; | |
$this->forRemoval['keys'][] = $id; | |
} | |
} | |
public function cleanup() | |
{ | |
foreach ($this->forRemoval['keys'] as $id) { | |
unset($this->pool[$id]); | |
} | |
$temp = []; | |
foreach ($this->pool as $element) { | |
$temp[] = $element; | |
} | |
$this->pool = $temp; | |
$this->amount = count($this->pool); | |
$this->forRemoval = [ | |
'pool' => [], | |
'keys' => [], | |
]; | |
} | |
public function getRemovable() | |
{ | |
return $this->forRemoval; | |
} | |
public function getAmount() | |
{ | |
return $this->amount; | |
} | |
public function __construct() | |
{ | |
$this->position = 0; | |
} | |
public function hasItems() | |
{ | |
return $this->amount > 0; | |
} | |
// implementing Iterator | |
public function rewind() | |
{ | |
$this->position = 0; | |
} | |
public function current() | |
{ | |
return $this->pool[$this->position]; | |
} | |
public function key() | |
{ | |
return $this->position; | |
} | |
public function next() | |
{ | |
++$this->position; | |
} | |
public function valid() | |
{ | |
return isset($this->pool[$this->position]); | |
} | |
// implementing ArrayAccess | |
public function offsetSet($offset, $value) | |
{ | |
if (is_null($offset)) { | |
$this->pool[] = $value; | |
} else { | |
$this->pool[$offset] = $value; | |
} | |
} | |
public function offsetExists($offset) | |
{ | |
return isset($this->pool[$offset]); | |
} | |
public function offsetUnset($offset) | |
{ | |
unset($this->pool[$offset]); | |
} | |
public function offsetGet($offset) | |
{ | |
return isset($this->pool[$offset]) ? $this->pool[$offset] : null; | |
} | |
// implementing Countable | |
public function count($mode = \COUNT_NORMAL) | |
{ | |
return count($this->amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment