Skip to content

Commit 86adffa

Browse files
committed
Add Set implementation
1 parent f51a8fa commit 86adffa

3 files changed

Lines changed: 171 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* This file is part of the Composite Utils package.
4+
*
5+
* (c) Emily Shepherd <emily@emilyshepherd.me>
6+
*
7+
* For the full copyright and license information, please view the
8+
* LICENSE.md file that was distributed with this source code.
9+
*
10+
* @package spaark/composite-utils
11+
* @author Emily Shepherd <emily@emilyshepherd.me>
12+
* @license MIT
13+
*/
14+
15+
namespace Spaark\CompositeUtils\Model\Collection;
16+
17+
/**
18+
* Represents an abstract collection which acts as a list of items
19+
*/
20+
abstract class AbstractSet
21+
extends AbstractCollection
22+
implements SetInterface
23+
{
24+
/**
25+
* {@inheritDoc}
26+
*/
27+
public function offsetSet($offset, $value)
28+
{
29+
if ($offset === null)
30+
{
31+
$this->push($value);
32+
}
33+
else
34+
{
35+
throw new \Exception();
36+
}
37+
}
38+
39+
/**
40+
* {@inheritDoc}
41+
*/
42+
public function offsetUnset($index)
43+
{
44+
throw new \Exception();
45+
}
46+
47+
/**
48+
* {@inheritDoc}
49+
*/
50+
public function offsetExists($index)
51+
{
52+
throw new \Exception();
53+
}
54+
55+
/**
56+
* {@inheritDoc}
57+
*/
58+
public function offsetGet($index)
59+
{
60+
throw new \Exception();
61+
}
62+
}
63+

src/Model/Collection/HashSet.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* This file is part of the Composite Utils package.
4+
*
5+
* (c) Emily Shepherd <emily@emilyshepherd.me>
6+
*
7+
* For the full copyright and license information, please view the
8+
* LICENSE.md file that was distributed with this source code.
9+
*
10+
* @package spaark/composite-utils
11+
* @author Emily Shepherd <emily@emilyshepherd.me>
12+
* @license MIT
13+
*/
14+
15+
namespace Spaark\CompositeUtils\Model\Collection;
16+
17+
use ArrayIterator;
18+
use Spaark\CompositeUtils\Service\HashProducer;
19+
20+
/**
21+
* Represents an List stored in a PHP array
22+
*/
23+
class HashSet extends AbstractSet
24+
{
25+
/**
26+
* @var ValueType[]
27+
*/
28+
protected $data = [];
29+
30+
/**
31+
* {@inheritDoc}
32+
*/
33+
public function add($item)
34+
{
35+
$this->data[HashProducer::getHash($item)] = $item;
36+
}
37+
38+
/**
39+
* Checks if an element exists
40+
*
41+
* @param ValueType The item to search for
42+
* @return boolean If the item exists
43+
*/
44+
public function contains($item) : bool
45+
{
46+
return isset($this->data[HashProducer::getHash($item)]);
47+
}
48+
49+
/**
50+
* {@inheritDoc}
51+
*/
52+
public function remove($item)
53+
{
54+
unset($this->data[HashProducer::getHash($item)]);
55+
}
56+
57+
/**
58+
* {@inheritDoc}
59+
*/
60+
public function getIterator()
61+
{
62+
return new ArrayIterator($this->data);
63+
}
64+
65+
/**
66+
* {@inheritDoc}
67+
*/
68+
public function size() : int
69+
{
70+
return count($this->data);
71+
}
72+
}
73+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* This file is part of the Composite Utils package.
4+
*
5+
* (c) Emily Shepherd <emily@emilyshepherd.me>
6+
*
7+
* For the full copyright and license information, please view the
8+
* LICENSE.md file that was distributed with this source code.
9+
*
10+
* @package spaark/composite-utils
11+
* @author Emily Shepherd <emily@emilyshepherd.me>
12+
* @license MIT
13+
*/
14+
15+
namespace Spaark\CompositeUtils\Model\Collection;
16+
17+
/**
18+
* Represents an abstract collection which acts as a list of items
19+
*/
20+
interface SetInterface extends CollectionInterface
21+
{
22+
/**
23+
* Adds a new item to the end of the list
24+
*
25+
* @param ValueType $item The item to add
26+
*/
27+
public function add($item) : bool;
28+
29+
/**
30+
* Removes an item from the list, specified by its index
31+
*
32+
* @parami ValueType $item The item to remove
33+
*/
34+
public function remove($item);
35+
}

0 commit comments

Comments
 (0)