Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/Model/Collection/AbstractSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* This file is part of the Composite Utils package.
*
* (c) Emily Shepherd <emily@emilyshepherd.me>
*
* 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 <emily@emilyshepherd.me>
* @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();
}
}

73 changes: 73 additions & 0 deletions src/Model/Collection/HashSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* This file is part of the Composite Utils package.
*
* (c) Emily Shepherd <emily@emilyshepherd.me>
*
* 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 <emily@emilyshepherd.me>
* @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);
}
}

35 changes: 35 additions & 0 deletions src/Model/Collection/SetInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* This file is part of the Composite Utils package.
*
* (c) Emily Shepherd <emily@emilyshepherd.me>
*
* 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 <emily@emilyshepherd.me>
* @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);
}