-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split ObjectSet into multiple classes depending on use
- Loading branch information
Showing
11 changed files
with
395 additions
and
269 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 |
---|---|---|
@@ -1,20 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<phpunit colors="true"> | ||
<testsuites> | ||
<testsuite name="unit"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">src</directory> | ||
</whitelist> | ||
</filter> | ||
<logging> | ||
<log type="coverage-html" target="build/html" lowUpperBound="50" highLowerBound="90"/> | ||
<log type="coverage-text" target="php://stdout" showOnlySummary="true"/> | ||
<log type="coverage-clover" target="build/coverage.xml"/> | ||
<log type="coverage-php" target="build/coverage.serialized"/> | ||
<log type="junit" target="build/logfile.xml"/> | ||
</logging> | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> | ||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">src</directory> | ||
</include> | ||
<report> | ||
<clover outputFile="build/coverage.xml"/> | ||
<html outputDirectory="build/html" lowUpperBound="50" highLowerBound="90"/> | ||
<php outputFile="build/coverage.serialized"/> | ||
<text outputFile="php://stdout" showOnlySummary="true"/> | ||
</report> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="unit"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<logging> | ||
<junit outputFile="build/logfile.xml"/> | ||
</logging> | ||
</phpunit> |
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,89 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Infinityloop\Utils; | ||
|
||
abstract class BaseSet implements \Iterator, \ArrayAccess, \Countable | ||
{ | ||
use \Nette\SmartObject; | ||
|
||
protected const INNER_CLASS = self::class; | ||
|
||
protected array $array = []; | ||
|
||
public function merge(self $objectSet, bool $allowReplace = false) : self | ||
{ | ||
if (!$objectSet instanceof static) { | ||
throw new \Exception('I can only merge Sets of same type'); | ||
} | ||
|
||
return $this->mergeImpl($objectSet, $allowReplace); | ||
} | ||
|
||
public function toArray() : array | ||
{ | ||
return $this->array; | ||
} | ||
|
||
public function current() : object | ||
{ | ||
return \current($this->array); | ||
} | ||
|
||
public function next() : void | ||
{ | ||
\next($this->array); | ||
} | ||
|
||
public function valid() : bool | ||
{ | ||
return \key($this->array) !== null; | ||
} | ||
|
||
public function rewind() : void | ||
{ | ||
\reset($this->array); | ||
} | ||
|
||
public function count() : int | ||
{ | ||
return \count($this->array); | ||
} | ||
|
||
public function offsetExists($offset) : bool | ||
{ | ||
return \array_key_exists($offset, $this->array); | ||
} | ||
|
||
public function offsetGet($offset) : object | ||
{ | ||
if (!$this->offsetExists($offset)) { | ||
throw new \Exception('Item doesnt exist.'); | ||
} | ||
|
||
return $this->array[$offset]; | ||
} | ||
|
||
public function offsetSet($offset, $object) : void | ||
{ | ||
if (!\is_a($object, static::INNER_CLASS)) { | ||
throw new \Exception('Invalid input.'); | ||
} | ||
|
||
$this->offsetSetImpl($offset, $object); | ||
} | ||
|
||
public function offsetUnset($offset) : void | ||
{ | ||
if (!$this->offsetExists($offset)) { | ||
throw new \Exception('Item already doesnt exist.'); | ||
} | ||
|
||
unset($this->array[$offset]); | ||
} | ||
|
||
abstract protected function mergeImpl(self $objectSet, bool $allowReplace) : self; | ||
|
||
abstract protected function offsetSetImpl($offset, object $object) : void; | ||
} |
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,44 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Infinityloop\Utils; | ||
|
||
abstract class ImplicitObjectMap extends ObjectMap | ||
{ | ||
public function __construct(array $data = []) | ||
{ | ||
parent::__construct(); | ||
|
||
foreach ($data as $key => $object) { | ||
$this->offsetSet(\is_string($key) | ||
? $key | ||
: null , $object); | ||
} | ||
} | ||
|
||
abstract protected function getKey(object $object) : string; | ||
|
||
protected function offsetSetImpl($offset, object $object) : void | ||
{ | ||
$key = $this->getKey($object); | ||
|
||
if ($offset === $key) { | ||
parent::offsetSetImpl($key, $object); | ||
|
||
return; | ||
} | ||
|
||
if ($offset === null) { | ||
if ($this->offsetExists($key)) { | ||
throw new \Exception('Duplicated item'); | ||
} | ||
|
||
parent::offsetSetImpl($key, $object); | ||
|
||
return; | ||
} | ||
|
||
throw new \Exception('Offset does not match implicit offset'); | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Infinityloop\Utils; | ||
|
||
abstract class ObjectMap extends \Infinityloop\Utils\BaseSet | ||
{ | ||
public function __construct(array $data = []) | ||
{ | ||
foreach ($data as $key => $object) { | ||
$this->offsetSet($key, $object); | ||
} | ||
} | ||
|
||
public function key() : string | ||
{ | ||
return \key($this->array); | ||
} | ||
|
||
protected function mergeImpl(BaseSet $objectSet, bool $allowReplace = false) : self | ||
{ | ||
foreach ($objectSet as $offset => $object) { | ||
if (!$allowReplace && $this->offsetExists($offset)) { | ||
throw new \Exception('Item already exists, use $allowReplace if you wish to replace'); | ||
} | ||
|
||
$this->offsetSet($offset, $object); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
protected function offsetSetImpl($offset, object $object) : void | ||
{ | ||
if (\is_string($offset)) { | ||
$this->array[$offset] = $object; | ||
|
||
return; | ||
} | ||
|
||
throw new \Exception('Invalid offset for given object.'); | ||
} | ||
} |
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
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
Oops, something went wrong.