123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- <?php
- declare(strict_types = 1);
- namespace DASPRiD\Enum;
- use DASPRiD\Enum\Exception\ExpectationException;
- use DASPRiD\Enum\Exception\IllegalArgumentException;
- use IteratorAggregate;
- use Serializable;
- use Traversable;
- final class EnumMap implements Serializable, IteratorAggregate
- {
-
- private $keyType;
-
- private $valueType;
-
- private $allowNullValues;
-
- private $keyUniverse;
-
- private $values;
-
- private $size = 0;
-
- public function __construct(string $keyType, string $valueType, bool $allowNullValues)
- {
- if (! is_subclass_of($keyType, AbstractEnum::class)) {
- throw new IllegalArgumentException(sprintf(
- 'Class %s does not extend %s',
- $keyType,
- AbstractEnum::class
- ));
- }
- $this->keyType = $keyType;
- $this->valueType = $valueType;
- $this->allowNullValues = $allowNullValues;
- $this->keyUniverse = $keyType::values();
- $this->values = array_fill(0, count($this->keyUniverse), null);
- }
-
- public function expect(string $keyType, string $valueType, bool $allowNullValues) : void
- {
- if ($keyType !== $this->keyType) {
- throw new ExpectationException(sprintf(
- 'Callee expected an EnumMap with key type %s, but got %s',
- $keyType,
- $this->keyType
- ));
- }
- if ($valueType !== $this->valueType) {
- throw new ExpectationException(sprintf(
- 'Callee expected an EnumMap with value type %s, but got %s',
- $keyType,
- $this->keyType
- ));
- }
- if ($allowNullValues !== $this->allowNullValues) {
- throw new ExpectationException(sprintf(
- 'Callee expected an EnumMap with nullable flag %s, but got %s',
- ($allowNullValues ? 'true' : 'false'),
- ($this->allowNullValues ? 'true' : 'false')
- ));
- }
- }
-
- public function size() : int
- {
- return $this->size;
- }
-
- public function containsValue($value) : bool
- {
- return in_array($this->maskNull($value), $this->values, true);
- }
-
- public function containsKey(AbstractEnum $key) : bool
- {
- $this->checkKeyType($key);
- return null !== $this->values[$key->ordinal()];
- }
-
- public function get(AbstractEnum $key)
- {
- $this->checkKeyType($key);
- return $this->unmaskNull($this->values[$key->ordinal()]);
- }
-
- public function put(AbstractEnum $key, $value)
- {
- $this->checkKeyType($key);
- if (! $this->isValidValue($value)) {
- throw new IllegalArgumentException(sprintf('Value is not of type %s', $this->valueType));
- }
- $index = $key->ordinal();
- $oldValue = $this->values[$index];
- $this->values[$index] = $this->maskNull($value);
- if (null === $oldValue) {
- ++$this->size;
- }
- return $this->unmaskNull($oldValue);
- }
-
- public function remove(AbstractEnum $key)
- {
- $this->checkKeyType($key);
- $index = $key->ordinal();
- $oldValue = $this->values[$index];
- $this->values[$index] = null;
- if (null !== $oldValue) {
- --$this->size;
- }
- return $this->unmaskNull($oldValue);
- }
-
- public function clear() : void
- {
- $this->values = array_fill(0, count($this->keyUniverse), null);
- $this->size = 0;
- }
-
- public function equals(self $other) : bool
- {
- if ($this === $other) {
- return true;
- }
- if ($this->size !== $other->size) {
- return false;
- }
- return $this->values === $other->values;
- }
-
- public function values() : array
- {
- return array_values(array_map(function ($value) {
- return $this->unmaskNull($value);
- }, array_filter($this->values, function ($value) : bool {
- return null !== $value;
- })));
- }
- public function serialize() : string
- {
- $values = [];
- foreach ($this->values as $ordinal => $value) {
- if (null === $value) {
- continue;
- }
- $values[$ordinal] = $this->unmaskNull($value);
- }
- return serialize([
- 'keyType' => $this->keyType,
- 'valueType' => $this->valueType,
- 'allowNullValues' => $this->allowNullValues,
- 'values' => $values,
- ]);
- }
- public function unserialize($serialized) : void
- {
- $data = unserialize($serialized);
- $this->__construct($data['keyType'], $data['valueType'], $data['allowNullValues']);
- foreach ($this->keyUniverse as $key) {
- if (array_key_exists($key->ordinal(), $data['values'])) {
- $this->put($key, $data['values'][$key->ordinal()]);
- }
- }
- }
- public function getIterator() : Traversable
- {
- foreach ($this->keyUniverse as $key) {
- if (null === $this->values[$key->ordinal()]) {
- continue;
- }
- yield $key => $this->unmaskNull($this->values[$key->ordinal()]);
- }
- }
- private function maskNull($value)
- {
- if (null === $value) {
- return NullValue::instance();
- }
- return $value;
- }
- private function unmaskNull($value)
- {
- if ($value instanceof NullValue) {
- return null;
- }
- return $value;
- }
-
- private function checkKeyType(AbstractEnum $key) : void
- {
- if (get_class($key) !== $this->keyType) {
- throw new IllegalArgumentException(sprintf(
- 'Object of type %s is not the same type as %s',
- get_class($key),
- $this->keyType
- ));
- }
- }
- private function isValidValue($value) : bool
- {
- if (null === $value) {
- if ($this->allowNullValues) {
- return true;
- }
- return false;
- }
- switch ($this->valueType) {
- case 'mixed':
- return true;
- case 'bool':
- case 'boolean':
- return is_bool($value);
- case 'int':
- case 'integer':
- return is_int($value);
- case 'float':
- case 'double':
- return is_float($value);
- case 'string':
- return is_string($value);
- case 'object':
- return is_object($value);
- case 'array':
- return is_array($value);
- }
- return $value instanceof $this->valueType;
- }
- }
|