Skip to content

Commit af11415

Browse files
committed
split ObjectSet into multiple classes depending on use
1 parent 0f37f2a commit af11415

11 files changed

Lines changed: 395 additions & 269 deletions

phpunit.xml

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
<?xml version="1.0" encoding="UTF-8" ?>
2-
<phpunit colors="true">
3-
<testsuites>
4-
<testsuite name="unit">
5-
<directory>tests</directory>
6-
</testsuite>
7-
</testsuites>
8-
<filter>
9-
<whitelist processUncoveredFilesFromWhitelist="true">
10-
<directory suffix=".php">src</directory>
11-
</whitelist>
12-
</filter>
13-
<logging>
14-
<log type="coverage-html" target="build/html" lowUpperBound="50" highLowerBound="90"/>
15-
<log type="coverage-text" target="php://stdout" showOnlySummary="true"/>
16-
<log type="coverage-clover" target="build/coverage.xml"/>
17-
<log type="coverage-php" target="build/coverage.serialized"/>
18-
<log type="junit" target="build/logfile.xml"/>
19-
</logging>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage processUncoveredFiles="true">
4+
<include>
5+
<directory suffix=".php">src</directory>
6+
</include>
7+
<report>
8+
<clover outputFile="build/coverage.xml"/>
9+
<html outputDirectory="build/html" lowUpperBound="50" highLowerBound="90"/>
10+
<php outputFile="build/coverage.serialized"/>
11+
<text outputFile="php://stdout" showOnlySummary="true"/>
12+
</report>
13+
</coverage>
14+
<testsuites>
15+
<testsuite name="unit">
16+
<directory>tests</directory>
17+
</testsuite>
18+
</testsuites>
19+
<logging>
20+
<junit outputFile="build/logfile.xml"/>
21+
</logging>
2022
</phpunit>

src/Utils/BaseSet.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Infinityloop\Utils;
6+
7+
abstract class BaseSet implements \Iterator, \ArrayAccess, \Countable
8+
{
9+
use \Nette\SmartObject;
10+
11+
protected const INNER_CLASS = self::class;
12+
13+
protected array $array = [];
14+
15+
public function merge(self $objectSet, bool $allowReplace = false) : self
16+
{
17+
if (!$objectSet instanceof static) {
18+
throw new \Exception('I can only merge Sets of same type');
19+
}
20+
21+
return $this->mergeImpl($objectSet, $allowReplace);
22+
}
23+
24+
public function toArray() : array
25+
{
26+
return $this->array;
27+
}
28+
29+
public function current() : object
30+
{
31+
return \current($this->array);
32+
}
33+
34+
public function next() : void
35+
{
36+
\next($this->array);
37+
}
38+
39+
public function valid() : bool
40+
{
41+
return \key($this->array) !== null;
42+
}
43+
44+
public function rewind() : void
45+
{
46+
\reset($this->array);
47+
}
48+
49+
public function count() : int
50+
{
51+
return \count($this->array);
52+
}
53+
54+
public function offsetExists($offset) : bool
55+
{
56+
return \array_key_exists($offset, $this->array);
57+
}
58+
59+
public function offsetGet($offset) : object
60+
{
61+
if (!$this->offsetExists($offset)) {
62+
throw new \Exception('Item doesnt exist.');
63+
}
64+
65+
return $this->array[$offset];
66+
}
67+
68+
public function offsetSet($offset, $object) : void
69+
{
70+
if (!\is_a($object, static::INNER_CLASS)) {
71+
throw new \Exception('Invalid input.');
72+
}
73+
74+
$this->offsetSetImpl($offset, $object);
75+
}
76+
77+
public function offsetUnset($offset) : void
78+
{
79+
if (!$this->offsetExists($offset)) {
80+
throw new \Exception('Item already doesnt exist.');
81+
}
82+
83+
unset($this->array[$offset]);
84+
}
85+
86+
abstract protected function mergeImpl(self $objectSet, bool $allowReplace) : self;
87+
88+
abstract protected function offsetSetImpl($offset, object $object) : void;
89+
}

src/Utils/ImplicitObjectMap.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Infinityloop\Utils;
6+
7+
abstract class ImplicitObjectMap extends ObjectMap
8+
{
9+
public function __construct(array $data = [])
10+
{
11+
parent::__construct();
12+
13+
foreach ($data as $key => $object) {
14+
$this->offsetSet(\is_string($key)
15+
? $key
16+
: null , $object);
17+
}
18+
}
19+
20+
abstract protected function getKey(object $object) : string;
21+
22+
protected function offsetSetImpl($offset, object $object) : void
23+
{
24+
$key = $this->getKey($object);
25+
26+
if ($offset === $key) {
27+
parent::offsetSetImpl($key, $object);
28+
29+
return;
30+
}
31+
32+
if ($offset === null) {
33+
if ($this->offsetExists($key)) {
34+
throw new \Exception('Duplicated item');
35+
}
36+
37+
parent::offsetSetImpl($key, $object);
38+
39+
return;
40+
}
41+
42+
throw new \Exception('Offset does not match implicit offset');
43+
}
44+
}

src/Utils/ObjectMap.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Infinityloop\Utils;
6+
7+
abstract class ObjectMap extends \Infinityloop\Utils\BaseSet
8+
{
9+
public function __construct(array $data = [])
10+
{
11+
foreach ($data as $key => $object) {
12+
$this->offsetSet($key, $object);
13+
}
14+
}
15+
16+
public function key() : string
17+
{
18+
return \key($this->array);
19+
}
20+
21+
protected function mergeImpl(BaseSet $objectSet, bool $allowReplace = false) : self
22+
{
23+
foreach ($objectSet as $offset => $object) {
24+
if (!$allowReplace && $this->offsetExists($offset)) {
25+
throw new \Exception('Item already exists, use $allowReplace if you wish to replace');
26+
}
27+
28+
$this->offsetSet($offset, $object);
29+
}
30+
31+
return $this;
32+
}
33+
34+
protected function offsetSetImpl($offset, object $object) : void
35+
{
36+
if (\is_string($offset)) {
37+
$this->array[$offset] = $object;
38+
39+
return;
40+
}
41+
42+
throw new \Exception('Invalid offset for given object.');
43+
}
44+
}

src/Utils/ObjectSet.php

Lines changed: 8 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,22 @@
44

55
namespace Infinityloop\Utils;
66

7-
abstract class ObjectSet implements \Iterator, \ArrayAccess, \Countable
7+
abstract class ObjectSet extends \Infinityloop\Utils\BaseSet
88
{
9-
use \Nette\SmartObject;
10-
11-
protected const INNER_CLASS = self::class;
12-
13-
protected array $array = [];
14-
15-
public function __construct(array $data)
9+
public function __construct(array $data = [])
1610
{
1711
foreach ($data as $object) {
1812
$this->offsetSet(null, $object);
1913
}
2014
}
2115

22-
public function toArray() : array
16+
public function key() : int
2317
{
24-
return $this->array;
18+
return \key($this->array);
2519
}
2620

27-
public function merge(self $objectSet, bool $allowReplace = false) : self
21+
protected function mergeImpl(BaseSet $objectSet, bool $allowReplace = false) : self
2822
{
29-
if (!$objectSet instanceof static) {
30-
throw new \Exception('I can only merge ObjectSets of same type');
31-
}
32-
3323
foreach ($objectSet as $offset => $object) {
3424
$this->offsetSet($allowReplace
3525
? $offset
@@ -39,95 +29,20 @@ public function merge(self $objectSet, bool $allowReplace = false) : self
3929
return $this;
4030
}
4131

42-
public function current() : object
43-
{
44-
return \current($this->array);
45-
}
46-
47-
public function next() : void
48-
{
49-
\next($this->array);
50-
}
51-
52-
/** @return int|string */
53-
public function key()
32+
protected function offsetSetImpl($offset, object $object) : void
5433
{
55-
return \key($this->array);
56-
}
57-
58-
public function valid() : bool
59-
{
60-
return \key($this->array) !== null;
61-
}
62-
63-
public function rewind() : void
64-
{
65-
\reset($this->array);
66-
}
67-
68-
public function count() : int
69-
{
70-
return \count($this->array);
71-
}
72-
73-
public function offsetExists($offset) : bool
74-
{
75-
return \array_key_exists($offset, $this->array);
76-
}
77-
78-
public function offsetGet($offset) : object
79-
{
80-
if (!$this->offsetExists($offset)) {
81-
throw new \Exception('Item doesnt exist.');
82-
}
83-
84-
return $this->array[$offset];
85-
}
86-
87-
public function offsetSet($offset, $object) : void
88-
{
89-
if (!\is_a($object, static::INNER_CLASS)) {
90-
throw new \Exception('Invalid input.');
91-
}
92-
93-
$key = $this->getKey($object);
94-
9534
if ($offset === null) {
96-
if ($key === null) {
97-
$this->array[] = $object;
98-
99-
return;
100-
}
101-
102-
if ($this->offsetExists($key)) {
103-
throw new \Exception('Duplicated item. Set using explicit key if you wish to replace.');
104-
}
105-
106-
$this->array[$key] = $object;
35+
$this->array[] = $object;
10736

10837
return;
10938
}
11039

111-
if (($key === null && \is_int($offset)) || (\is_string($key) && \is_string($offset) && $key === $offset)) {
40+
if (\is_int($offset)) {
11241
$this->array[$offset] = $object;
11342

11443
return;
11544
}
11645

11746
throw new \Exception('Invalid offset for given object.');
11847
}
119-
120-
public function offsetUnset($offset) : void
121-
{
122-
if (!$this->offsetExists($offset)) {
123-
throw new \Exception('Item already doesnt exist.');
124-
}
125-
126-
unset($this->array[$offset]);
127-
}
128-
129-
protected function getKey(object $object) : ?string
130-
{
131-
return null;
132-
}
13348
}

tests/Utils/EmptyClassSet.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@
44

55
namespace Infinityloop\Tests\Utils;
66

7-
class EmptyClassSet extends \Infinityloop\Utils\ObjectSet
7+
/**
8+
* @method \Infinityloop\Tests\Utils\EmptyClass current() : object
9+
* @method \Infinityloop\Tests\Utils\EmptyClass offsetGet($offset) : object
10+
*/
11+
final class EmptyClassSet extends \Infinityloop\Utils\ObjectSet
812
{
913
protected const INNER_CLASS = EmptyClass::class;
10-
11-
public function current() : EmptyClass
12-
{
13-
return parent::current();
14-
}
15-
16-
public function offsetGet($offset) : EmptyClass
17-
{
18-
return parent::offsetGet($offset);
19-
}
2014
}

0 commit comments

Comments
 (0)