diff --git a/src/Model/Collection/AbstractSet.php b/src/Model/Collection/AbstractSet.php new file mode 100644 index 0000000..007fb1c --- /dev/null +++ b/src/Model/Collection/AbstractSet.php @@ -0,0 +1,63 @@ + + * + * For the full copyright and license information, please view the + * LICENSE.md file that was distributed with this source code. + * + * @package spaark/composite-utils + * @author Emily Shepherd + * @license MIT + */ + +namespace Spaark\CompositeUtils\Model\Collection; + +/** + * Represents an abstract collection which acts as a list of items + */ +abstract class AbstractSet + extends AbstractCollection + implements SetInterface +{ + /** + * {@inheritDoc} + */ + public function offsetSet($offset, $value) + { + if ($offset === null) + { + $this->push($value); + } + else + { + throw new \Exception(); + } + } + + /** + * {@inheritDoc} + */ + public function offsetUnset($index) + { + throw new \Exception(); + } + + /** + * {@inheritDoc} + */ + public function offsetExists($index) + { + throw new \Exception(); + } + + /** + * {@inheritDoc} + */ + public function offsetGet($index) + { + throw new \Exception(); + } +} + diff --git a/src/Model/Collection/HashSet.php b/src/Model/Collection/HashSet.php new file mode 100644 index 0000000..6e89864 --- /dev/null +++ b/src/Model/Collection/HashSet.php @@ -0,0 +1,73 @@ + + * + * For the full copyright and license information, please view the + * LICENSE.md file that was distributed with this source code. + * + * @package spaark/composite-utils + * @author Emily Shepherd + * @license MIT + */ + +namespace Spaark\CompositeUtils\Model\Collection; + +use ArrayIterator; +use Spaark\CompositeUtils\Service\HashProducer; + +/** + * Represents an List stored in a PHP array + */ +class HashSet extends AbstractSet +{ + /** + * @var ValueType[] + */ + protected $data = []; + + /** + * {@inheritDoc} + */ + public function add($item) + { + $this->data[HashProducer::getHash($item)] = $item; + } + + /** + * Checks if an element exists + * + * @param ValueType The item to search for + * @return boolean If the item exists + */ + public function contains($item) : bool + { + return isset($this->data[HashProducer::getHash($item)]); + } + + /** + * {@inheritDoc} + */ + public function remove($item) + { + unset($this->data[HashProducer::getHash($item)]); + } + + /** + * {@inheritDoc} + */ + public function getIterator() + { + return new ArrayIterator($this->data); + } + + /** + * {@inheritDoc} + */ + public function size() : int + { + return count($this->data); + } +} + diff --git a/src/Model/Collection/SetInterface.php b/src/Model/Collection/SetInterface.php new file mode 100644 index 0000000..c1873c9 --- /dev/null +++ b/src/Model/Collection/SetInterface.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the + * LICENSE.md file that was distributed with this source code. + * + * @package spaark/composite-utils + * @author Emily Shepherd + * @license MIT + */ + +namespace Spaark\CompositeUtils\Model\Collection; + +/** + * Represents an abstract collection which acts as a list of items + */ +interface SetInterface extends CollectionInterface +{ + /** + * Adds a new item to the end of the list + * + * @param ValueType $item The item to add + */ + public function add($item) : bool; + + /** + * Removes an item from the list, specified by its index + * + * @parami ValueType $item The item to remove + */ + public function remove($item); +}