-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
703b67f
commit 2ceb32b
Showing
3 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
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
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 <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
* @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(); | ||
} | ||
} | ||
|
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
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 <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
* @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); | ||
} | ||
} | ||
|
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
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 <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
* @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); | ||
} |