diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ef25c5..8109fcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [0.5.0] - 2020-08-01 +### Added +- It is Mess now 🍺 + +## [0.4.0] - 2020-08-01 +### Removed +- `TypedAccessor` (deprecated, use `Mess` instead) + +## [0.3.1] - 2020-08-01 +### Added +- `Mess` alias (can be used instead long and boring `TypedAccessor`) +- more tests + ## [0.3.0] - 2020-08-01 ### Added - `getObject()` diff --git a/README.md b/README.md index cdcfd8b..1eaf38d 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ - Illogical type casting (`PHP`'s native implementation is way too "smart") - Pointless casts like `array => float` are **allowed** -- Arrays & boilerplate code to work with them (check if `isset()`, throw an exception, cast the type, etc.) +- Boilerplate code to work with arrays (check if `isset()`, throw an exception, cast the type, etc.) Consider an example: ```php @@ -16,28 +16,32 @@ $userId = (int)$userId; ## Way too verbose. Any ideas? ```php -$userId = (new TypedAccessor($queryParams))['userId']->getAsInt(); +$userId = (new Mess($queryParams))['userId']->getAsInt(); +``` + +```bash +$ composer require zakirullin/mess ``` ## A few real-world examples ```php -$queryParams = new TypedAccessor(['isDeleted' => 'true']); +$queryParams = new Mess(['isDeleted' => 'true']); $queryParams['isDeleted']->getBool(); // UnexpectedTypeException $queryParams['isDeleted']->getAsBool(); // true -$value = new TypedAccessor('25'); +$value = new Mess('25'); $value->getInt(); // UnexpectedTypeException $value->getAsInt(); // 25 $value->getString(); // '25' -$value = new TypedAccessor('abc'); +$value = new Mess('abc'); $value->getInt(); // UnexpectedTypeException $value->getAsInt(); // UncastableValueException $value->findInt(); // null $value->findInt() ?? 1; // 1 -$config = new TypedAccessor(['param' => '1']); +$config = new Mess(['param' => '1']); $config['a']['b']->getInt(); // MissingKeyException: "MissingKeyException: a.b" $config['a']->findInt(); // null $config['param']->getInt(); // UnexpectedTypeException @@ -95,11 +99,9 @@ And that's the worst thing about it. It will continue to work, though, not in a ## The library comes in handy in a variety of scenarios 🚀 +Where does mess come from? + - Deserialized data - Request `body`/`query` - `API` responses - etc. - -```bash -$ composer require zakirullin/typed-accessor -``` diff --git a/composer.json b/composer.json index 15237ac..16f96ae 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "zakirullin/typed-accessor", + "name": "zakirullin/mess", "type": "library", "description": "Convenient array-related routine & better type casting", "keywords": [ @@ -10,7 +10,7 @@ "php", "json" ], - "homepage": "https://github.com/zakirullin/typed-accessor", + "homepage": "https://github.com/zakirullin/mess", "license": "MIT", "authors": [ { @@ -31,12 +31,13 @@ }, "autoload": { "psr-4": { + "Zakirullin\\Mess\\": "src/", "Zakirullin\\TypedAccessor\\": "src/" } }, "autoload-dev": { "psr-4": { - "Zakirullin\\TypedAccessor\\Tests\\": "tests/unit" + "Zakirullin\\Mess\\Tests\\": "tests/unit" } } } diff --git a/src/Caster/ArrayOfStringToTypeCaster.php b/src/Caster/ArrayOfStringToTypeCaster.php index 35eb4f4..4b0add1 100644 --- a/src/Caster/ArrayOfStringToTypeCaster.php +++ b/src/Caster/ArrayOfStringToTypeCaster.php @@ -1,9 +1,9 @@ @@ -25,7 +25,7 @@ public function __construct(array $keySequence, Throwable $previous = null) { $this->keySequence = $keySequence; - $message = "TypedAccessor cannot modify it's value"; + $message = "Mess cannot modify it's value"; parent::__construct($message, 0, $previous); } diff --git a/src/Exception/TypedAccessorExceptionInterface.php b/src/Exception/MessExceptionInterface.php similarity index 64% rename from src/Exception/TypedAccessorExceptionInterface.php rename to src/Exception/MessExceptionInterface.php index 7df3af2..1da0bd8 100644 --- a/src/Exception/TypedAccessorExceptionInterface.php +++ b/src/Exception/MessExceptionInterface.php @@ -1,9 +1,9 @@ diff --git a/src/Exception/MissingKeyException.php b/src/Exception/MissingKeyException.php index 79b8ff4..7f6d513 100644 --- a/src/Exception/MissingKeyException.php +++ b/src/Exception/MissingKeyException.php @@ -1,13 +1,13 @@ diff --git a/src/Exception/UncastableValueException.php b/src/Exception/UncastableValueException.php index f674ca9..e89c13b 100644 --- a/src/Exception/UncastableValueException.php +++ b/src/Exception/UncastableValueException.php @@ -1,12 +1,12 @@ + * + * @var array + */ + private $keySequence = []; + + /** + * + * @param mixed $value + */ + public function __construct($value) + { + $this->value = $value; + } + + /** + * @psalm-pure + * + * @return int + */ + public function getInt(): int + { + $this->assertType($this->findInt(), TypeEnum::INT); + + /** + * @var int + */ + return $this->value; + } + + /** + * @psalm-pure + * + * @return bool + */ + public function getBool(): bool + { + $this->assertType($this->findBool(), TypeEnum::BOOL); + + /** + * @var bool + */ + return $this->value; + } + + /** + * @psalm-pure + * + * @return string + */ + public function getString(): string + { + $this->assertType($this->findString(), TypeEnum::STRING); + + /** + * @var string + */ + return $this->value; + } + + /** + * @psalm-pure + * @psalm-return list + * + * @return array + */ + public function getListOfInt(): array + { + $this->assertType($this->findListOfInt(), TypeEnum::LIST_OF_INT); + + /** + * @psalm-var list + */ + return $this->value; + } + + /** + * @psalm-pure + * @psalm-return list + * + * @return array + */ + public function getListOfString(): array + { + $this->assertType($this->findListOfString(), TypeEnum::LIST_OF_STRING); + + /** + * @psalm-var list + */ + return $this->value; + } + + /** + * @psalm-pure + * @psalm-return array + * + * @return array + */ + public function getArrayOfStringToInt(): array + { + $this->assertType($this->findArrayOfStringToInt(), TypeEnum::ARRAY_OF_STRING_TO_INT); + + /** + * @psalm-var array + */ + return $this->value; + } + + /** + * @psalm-pure + * @psalm-return array + * + * @return array + */ + public function getArrayOfStringToBool(): array + { + $this->assertType($this->findArrayOfStringToBool(), TypeEnum::ARRAY_OF_STRING_TO_BOOL); + + /** + * @var array + */ + return $this->value; + } + + /** + * @psalm-pure + * @psalm-return array + * + * @return array + */ + public function getArrayOfStringToString(): array + { + $this->assertType($this->findArrayOfStringToString(), TypeEnum::ARRAY_OF_STRING_TO_STRING); + + /** + * @psalm-var array + */ + return $this->value; + } + + /** + * @psalm-pure + * + * @return int + */ + public function getAsInt(): int + { + $intValue = $this->findAsInt(); + + $this->assertCastable($intValue, TypeEnum::INT); + + /** + * @var int + */ + return $intValue; + } + + /** + * @psalm-pure + * + * @return bool + */ + public function getAsBool(): bool + { + $boolValue = $this->findAsBool(); + + $this->assertCastable($boolValue, TypeEnum::BOOL); + + return $boolValue; + } + + /** + * @psalm-pure + * + * @return string + */ + public function getAsString(): string + { + $stringValue = $this->findAsString(); + + $this->assertCastable($stringValue, TypeEnum::STRING); + + return $stringValue; + } + + /** + * @psalm-pure + * @psalm-return list + * + * @return array + */ + public function getAsListOfInt(): array + { + $listOfInt = $this->findAsListOfInt(); + + $this->assertCastable($listOfInt, TypeEnum::LIST_OF_INT); + + return $listOfInt; + } + + /** + * @psalm-pure + * @psalm-return list + * + * @return array + */ + public function getAsListOfString(): array + { + $listOfString = $this->findAsListOfString(); + + $this->assertCastable($listOfString, TypeEnum::LIST_OF_STRING); + + return $listOfString; + } + + /** + * @psalm-pure + * @psalm-return array + * + * @return array + */ + public function getAsArrayOfStringToInt(): array + { + $arrayOfStringToInt = $this->findAsArrayOfStringToInt(); + + $this->assertCastable($arrayOfStringToInt, TypeEnum::ARRAY_OF_STRING_TO_INT); + + return $arrayOfStringToInt; + } + + /** + * @psalm-pure + * @psalm-return array + * + * @return array + */ + public function getAsArrayOfStringToBool(): array + { + $arrayOfStringToBool = $this->findAsArrayOfStringToBool(); + + $this->assertCastable($arrayOfStringToBool, TypeEnum::ARRAY_OF_STRING_TO_BOOL); + + return $arrayOfStringToBool; + } + + /** + * @psalm-pure + * @psalm-return array + * + * @return array + */ + public function getAsArrayOfStringToString(): array + { + $arrayOfStringToString = $this->findAsArrayOfStringToString(); + + $this->assertCastable($arrayOfStringToString, TypeEnum::ARRAY_OF_STRING_TO_STRING); + + return $arrayOfStringToString; + } + + /** + * @psalm-pure + * + * @return int|null + */ + public function findInt(): ?int + { + if (!is_int($this->value)) { + return null; + } + + return $this->value; + } + + /** + * @psalm-pure + * + * @return bool|null + */ + public function findBool(): ?bool + { + if (!is_bool($this->value)) { + return null; + } + + return $this->value; + } + + /** + * @psalm-pure + * + * @return string|null + */ + public function findString(): ?string + { + if (!is_string($this->value)) { + return null; + } + + return $this->value; + } + + /** + * @psalm-pure + * @psalm-return list|null + * + * @return array|null + */ + public function findListOfInt(): ?array + { + /** + * @psalm-var list|null + */ + return ListOfTypeFinder::find($this->value, 'is_int'); + } + + /** + * @psalm-pure + * @psalm-return list|null + * + * @return array|null + */ + public function findListOfString(): ?array + { + /** + * @psalm-var list|null + */ + return ListOfTypeFinder::find($this->value, 'is_string'); + } + + /** + * @psalm-pure + * @psalm-return array|null + * + * @return array|null + */ + public function findArrayOfStringToInt(): ?array + { + /** + * @psalm-var array|null + */ + return ArrayOfStringToTypeFinder::find($this->value, 'is_int'); + } + + /** + * @psalm-pure + * @psalm-return array|null + * + * @return array|null + */ + public function findArrayOfStringToBool(): ?array + { + /** + * @psalm-var array|null + */ + return ArrayOfStringToTypeFinder::find($this->value, 'is_bool'); + } + + /** + * @psalm-pure + * @psalm-return array|null + * + * @return array|null + */ + public function findArrayOfStringToString(): ?array + { + /** + * @psalm-var array|null + */ + return ArrayOfStringToTypeFinder::find($this->value, 'is_string'); + } + + /** + * @psalm-pure + * + * @return int|null + */ + public function findAsInt(): ?int + { + return IntCaster::cast($this->value); + } + + /** + * @psalm-pure + * + * @return bool|null + */ + public function findAsBool(): ?bool + { + return BoolCaster::cast($this->value); + } + + /** + * @psalm-pure + * + * @return string|null + */ + public function findAsString(): ?string + { + return StringCaster::cast($this->value); + } + + /** + * @psalm-return list|null + * + * @return array|null + */ + public function findAsListOfInt(): ?array + { + /** + * @psalm-var list|null + */ + return ListOfTypeCaster::cast($this->value, [IntCaster::class, 'cast']); + } + + /** + * @psalm-return list|null + * + * @return array|null + */ + public function findAsListOfString(): ?array + { + /** + * @psalm-var list|null + */ + return ListOfTypeCaster::cast($this->value, [StringCaster::class, 'cast']); + } + + /** + * @psalm-pure + * @psalm-return array|null + * + * @return array|null + */ + public function findAsArrayOfStringToInt(): ?array + { + /** + * @psalm-var array|null + */ + return ArrayOfStringToTypeCaster::cast($this->value, [IntCaster::class, 'cast']); + } + + /** + * @psalm-pure + * @psalm-return array|null + * + * @return array|null + */ + public function findAsArrayOfStringToBool(): ?array + { + /** + * @psalm-var array|null + */ + return ArrayOfStringToTypeCaster::cast($this->value, [BoolCaster::class, 'cast']); + } + + /** + * @psalm-pure + * @psalm-return array|null + * + * @return array|null + */ + public function findAsArrayOfStringToString(): ?array + { + /** + * @psalm-var array|null + */ + return ArrayOfStringToTypeCaster::cast($this->value, [StringCaster::class, 'cast']); + } + + /** + * @psalm-pure + * + * @return mixed + */ + public function getMixed() + { + return $this->value; + } + + /** + * @return object + */ + public function getObject(): object + { + $this->assertType($this->findObject(), TypeEnum::OBJECT); + + /** + * @var object + */ + return $this->value; + } + + /** + * @return array + */ + public function getArray(): array + { + $this->assertType($this->findArray(), TypeEnum::ARRAY); + + /** + * @var array + */ + return $this->value; + } + + /** + * @psalm-pure + * + * @return mixed + */ + public function findMixed() + { + return $this->value; + } + + /** + * @return object|null + */ + public function findObject(): ?object + { + if (!is_object($this->value)) { + return null; + } + + return $this->value; + } + + /** + * @return array|null + */ + public function findArray(): ?array + { + if (!is_array($this->value)) { + return null; + } + + return $this->value; + } + + /** + * @psalm-pure + * + * @param string|int $offset + * @return MessInterface + */ + public function offsetGet($offset) + { + /** + * @psalm-suppress DocblockTypeContradiction + */ + if (!is_string($offset) && !is_int($offset)) { + throw new UnexpectedKeyTypeException($offset, $this->keySequence); + } + + $clonedKeySequence = $this->keySequence; + $clonedKeySequence[] = $offset; + + if (!$this->offsetExists($offset)) { + return new MissingMess($clonedKeySequence); + } + + /** + * @var array + */ + $array = $this->value; + + return (new self($array[$offset]))->setKeySequence($clonedKeySequence); + } + + /** + * @psalm-pure + * + * @param string|int $offset + * @return bool + */ + public function offsetExists($offset): bool + { + if (!is_array($this->value)) { + return false; + } + + return key_exists($offset, $this->value); + } + + /** + * @psalm-pure + * + * @param mixed $offset + * @param mixed $value + */ + public function offsetSet($offset, $value): void + { + throw new CannotModifyMessException($this->keySequence); + } + + /** + * @psalm-pure + * + * @param mixed $offset + */ + public function offsetUnset($offset): void + { + throw new CannotModifyMessException($this->keySequence); + } + + /** + * @psalm-pure + * @psalm-param list $keySequence + * + * @param array $keySequence + * @return Mess + */ + private function setKeySequence(array $keySequence): self + { + $this->keySequence = $keySequence; + + return $this; + } + + /** + * @param mixed $value + * @param string $expectedType + */ + private function assertType($value, string $expectedType): void + { + if ($value === null) { + throw new UnexpectedTypeException($expectedType, $value, $this->keySequence); + } + } + + /** + * @param mixed $value + * @param string $desiredType + */ + private function assertCastable($value, string $desiredType): void + { + if ($value === null) { + throw new UncastableValueException($desiredType, $this->value, $this->keySequence); + } + } +} \ No newline at end of file diff --git a/src/TypedAccessorInterface.php b/src/MessInterface.php similarity index 95% rename from src/TypedAccessorInterface.php rename to src/MessInterface.php index 1e3f37f..14bebfe 100644 --- a/src/TypedAccessorInterface.php +++ b/src/MessInterface.php @@ -1,11 +1,11 @@ keySequence); + throw new CannotModifyMessException($this->keySequence); } /** @@ -482,6 +482,6 @@ public function offsetSet($offset, $value): void */ public function offsetUnset($offset): void { - throw new CannotModifyAccessorException($this->keySequence); + throw new CannotModifyMessException($this->keySequence); } } \ No newline at end of file diff --git a/src/TypedAccessor.php b/src/TypedAccessor.php index b7ab894..2304762 100644 --- a/src/TypedAccessor.php +++ b/src/TypedAccessor.php @@ -3,676 +3,12 @@ namespace Zakirullin\TypedAccessor; -use Zakirullin\TypedAccessor\Caster\ArrayOfStringToTypeCaster; -use Zakirullin\TypedAccessor\Caster\BoolCaster; -use Zakirullin\TypedAccessor\Caster\IntCaster; -use Zakirullin\TypedAccessor\Caster\ListOfTypeCaster; -use Zakirullin\TypedAccessor\Caster\StringCaster; -use Zakirullin\TypedAccessor\Enum\TypeEnum; -use Zakirullin\TypedAccessor\Exception\CannotModifyAccessorException; -use Zakirullin\TypedAccessor\Exception\UncastableValueException; -use Zakirullin\TypedAccessor\Exception\UnexpectedKeyTypeException; -use Zakirullin\TypedAccessor\Exception\UnexpectedTypeException; -use Zakirullin\TypedAccessor\Finder\ArrayOfStringToTypeFinder; -use Zakirullin\TypedAccessor\Finder\ListOfTypeFinder; -use function is_array; -use function is_bool; -use function is_int; -use function is_object; -use function is_string; -use function key_exists; +use Zakirullin\Mess\Mess; /** + * @deprecated will be removed in 1.0, use Mess instead * @psalm-immutable */ -final class TypedAccessor implements TypedAccessorInterface +final class TypedAccessor extends Mess { - /** - * @var mixed - */ - private $value; - - /** - * @psalm-allow-private-mutation - * @psalm-var list - * - * @var array - */ - private $keySequence = []; - - /** - * - * @param mixed $value - */ - public function __construct($value) - { - $this->value = $value; - } - - /** - * @psalm-pure - * - * @return int - */ - public function getInt(): int - { - $this->assertType($this->findInt(), TypeEnum::INT); - - /** - * @var int - */ - return $this->value; - } - - /** - * @psalm-pure - * - * @return bool - */ - public function getBool(): bool - { - $this->assertType($this->findBool(), TypeEnum::BOOL); - - /** - * @var bool - */ - return $this->value; - } - - /** - * @psalm-pure - * - * @return string - */ - public function getString(): string - { - $this->assertType($this->findString(), TypeEnum::STRING); - - /** - * @var string - */ - return $this->value; - } - - /** - * @psalm-pure - * @psalm-return list - * - * @return array - */ - public function getListOfInt(): array - { - $this->assertType($this->findListOfInt(), TypeEnum::LIST_OF_INT); - - /** - * @psalm-var list - */ - return $this->value; - } - - /** - * @psalm-pure - * @psalm-return list - * - * @return array - */ - public function getListOfString(): array - { - $this->assertType($this->findListOfString(), TypeEnum::LIST_OF_STRING); - - /** - * @psalm-var list - */ - return $this->value; - } - - /** - * @psalm-pure - * @psalm-return array - * - * @return array - */ - public function getArrayOfStringToInt(): array - { - $this->assertType($this->findArrayOfStringToInt(), TypeEnum::ARRAY_OF_STRING_TO_INT); - - /** - * @psalm-var array - */ - return $this->value; - } - - /** - * @psalm-pure - * @psalm-return array - * - * @return array - */ - public function getArrayOfStringToBool(): array - { - $this->assertType($this->findArrayOfStringToBool(), TypeEnum::ARRAY_OF_STRING_TO_BOOL); - - /** - * @var array - */ - return $this->value; - } - - /** - * @psalm-pure - * @psalm-return array - * - * @return array - */ - public function getArrayOfStringToString(): array - { - $this->assertType($this->findArrayOfStringToString(), TypeEnum::ARRAY_OF_STRING_TO_STRING); - - /** - * @psalm-var array - */ - return $this->value; - } - - /** - * @psalm-pure - * - * @return int - */ - public function getAsInt(): int - { - $intValue = $this->findAsInt(); - - $this->assertCastable($intValue, TypeEnum::INT); - - /** - * @var int - */ - return $intValue; - } - - /** - * @psalm-pure - * - * @return bool - */ - public function getAsBool(): bool - { - $boolValue = $this->findAsBool(); - - $this->assertCastable($boolValue, TypeEnum::BOOL); - - return $boolValue; - } - - /** - * @psalm-pure - * - * @return string - */ - public function getAsString(): string - { - $stringValue = $this->findAsString(); - - $this->assertCastable($stringValue, TypeEnum::STRING); - - return $stringValue; - } - - /** - * @psalm-pure - * @psalm-return list - * - * @return array - */ - public function getAsListOfInt(): array - { - $listOfInt = $this->findAsListOfInt(); - - $this->assertCastable($listOfInt, TypeEnum::LIST_OF_INT); - - return $listOfInt; - } - - /** - * @psalm-pure - * @psalm-return list - * - * @return array - */ - public function getAsListOfString(): array - { - $listOfString = $this->findAsListOfString(); - - $this->assertCastable($listOfString, TypeEnum::LIST_OF_STRING); - - return $listOfString; - } - - /** - * @psalm-pure - * @psalm-return array - * - * @return array - */ - public function getAsArrayOfStringToInt(): array - { - $arrayOfStringToInt = $this->findAsArrayOfStringToInt(); - - $this->assertCastable($arrayOfStringToInt, TypeEnum::ARRAY_OF_STRING_TO_INT); - - return $arrayOfStringToInt; - } - - /** - * @psalm-pure - * @psalm-return array - * - * @return array - */ - public function getAsArrayOfStringToBool(): array - { - $arrayOfStringToBool = $this->findAsArrayOfStringToBool(); - - $this->assertCastable($arrayOfStringToBool, TypeEnum::ARRAY_OF_STRING_TO_BOOL); - - return $arrayOfStringToBool; - } - - /** - * @psalm-pure - * @psalm-return array - * - * @return array - */ - public function getAsArrayOfStringToString(): array - { - $arrayOfStringToString = $this->findAsArrayOfStringToString(); - - $this->assertCastable($arrayOfStringToString, TypeEnum::ARRAY_OF_STRING_TO_STRING); - - return $arrayOfStringToString; - } - - /** - * @psalm-pure - * - * @return int|null - */ - public function findInt(): ?int - { - if (!is_int($this->value)) { - return null; - } - - return $this->value; - } - - /** - * @psalm-pure - * - * @return bool|null - */ - public function findBool(): ?bool - { - if (!is_bool($this->value)) { - return null; - } - - return $this->value; - } - - /** - * @psalm-pure - * - * @return string|null - */ - public function findString(): ?string - { - if (!is_string($this->value)) { - return null; - } - - return $this->value; - } - - /** - * @psalm-pure - * @psalm-return list|null - * - * @return array|null - */ - public function findListOfInt(): ?array - { - /** - * @psalm-var list|null - */ - return ListOfTypeFinder::find($this->value, 'is_int'); - } - - /** - * @psalm-pure - * @psalm-return list|null - * - * @return array|null - */ - public function findListOfString(): ?array - { - /** - * @psalm-var list|null - */ - return ListOfTypeFinder::find($this->value, 'is_string'); - } - - /** - * @psalm-pure - * @psalm-return array|null - * - * @return array|null - */ - public function findArrayOfStringToInt(): ?array - { - /** - * @psalm-var array|null - */ - return ArrayOfStringToTypeFinder::find($this->value, 'is_int'); - } - - /** - * @psalm-pure - * @psalm-return array|null - * - * @return array|null - */ - public function findArrayOfStringToBool(): ?array - { - /** - * @psalm-var array|null - */ - return ArrayOfStringToTypeFinder::find($this->value, 'is_bool'); - } - - /** - * @psalm-pure - * @psalm-return array|null - * - * @return array|null - */ - public function findArrayOfStringToString(): ?array - { - /** - * @psalm-var array|null - */ - return ArrayOfStringToTypeFinder::find($this->value, 'is_string'); - } - - /** - * @psalm-pure - * - * @return int|null - */ - public function findAsInt(): ?int - { - return IntCaster::cast($this->value); - } - - /** - * @psalm-pure - * - * @return bool|null - */ - public function findAsBool(): ?bool - { - return BoolCaster::cast($this->value); - } - - /** - * @psalm-pure - * - * @return string|null - */ - public function findAsString(): ?string - { - return StringCaster::cast($this->value); - } - - /** - * @psalm-return list|null - * - * @return array|null - */ - public function findAsListOfInt(): ?array - { - /** - * @psalm-var list|null - */ - return ListOfTypeCaster::cast($this->value, [IntCaster::class, 'cast']); - } - - /** - * @psalm-return list|null - * - * @return array|null - */ - public function findAsListOfString(): ?array - { - /** - * @psalm-var list|null - */ - return ListOfTypeCaster::cast($this->value, [StringCaster::class, 'cast']); - } - - /** - * @psalm-pure - * @psalm-return array|null - * - * @return array|null - */ - public function findAsArrayOfStringToInt(): ?array - { - /** - * @psalm-var array|null - */ - return ArrayOfStringToTypeCaster::cast($this->value, [IntCaster::class, 'cast']); - } - - /** - * @psalm-pure - * @psalm-return array|null - * - * @return array|null - */ - public function findAsArrayOfStringToBool(): ?array - { - /** - * @psalm-var array|null - */ - return ArrayOfStringToTypeCaster::cast($this->value, [BoolCaster::class, 'cast']); - } - - /** - * @psalm-pure - * @psalm-return array|null - * - * @return array|null - */ - public function findAsArrayOfStringToString(): ?array - { - /** - * @psalm-var array|null - */ - return ArrayOfStringToTypeCaster::cast($this->value, [StringCaster::class, 'cast']); - } - - /** - * @psalm-pure - * - * @return mixed - */ - public function getMixed() - { - return $this->value; - } - - /** - * @return object - */ - public function getObject(): object - { - $this->assertType($this->findObject(), TypeEnum::OBJECT); - - /** - * @var object - */ - return $this->value; - } - - /** - * @return array - */ - public function getArray(): array - { - $this->assertType($this->findArray(), TypeEnum::ARRAY); - - /** - * @var array - */ - return $this->value; - } - - /** - * @psalm-pure - * - * @return mixed - */ - public function findMixed() - { - return $this->value; - } - - /** - * @return object|null - */ - public function findObject(): ?object - { - if (!is_object($this->value)) { - return null; - } - - return $this->value; - } - - /** - * @return array|null - */ - public function findArray(): ?array - { - if (!is_array($this->value)) { - return null; - } - - return $this->value; - } - - /** - * @psalm-pure - * - * @param string|int $offset - * @return TypedAccessorInterface - */ - public function offsetGet($offset) - { - /** - * @psalm-suppress DocblockTypeContradiction - */ - if (!is_string($offset) && !is_int($offset)) { - throw new UnexpectedKeyTypeException($offset, $this->keySequence); - } - - $clonedKeySequence = $this->keySequence; - $clonedKeySequence[] = $offset; - - if (!$this->offsetExists($offset)) { - return new MissingValueAccessor($clonedKeySequence); - } - - /** - * @var array - */ - $array = $this->value; - - return (new self($array[$offset]))->setKeySequence($clonedKeySequence); - } - - /** - * @psalm-pure - * - * @param string|int $offset - * @return bool - */ - public function offsetExists($offset): bool - { - if (!is_array($this->value)) { - return false; - } - - return key_exists($offset, $this->value); - } - - /** - * @psalm-pure - * - * @param mixed $offset - * @param mixed $value - */ - public function offsetSet($offset, $value): void - { - throw new CannotModifyAccessorException($this->keySequence); - } - - /** - * @psalm-pure - * - * @param mixed $offset - */ - public function offsetUnset($offset): void - { - throw new CannotModifyAccessorException($this->keySequence); - } - - /** - * @psalm-pure - * @psalm-param list $keySequence - * - * @param array $keySequence - * @return TypedAccessor - */ - private function setKeySequence(array $keySequence): self - { - $this->keySequence = $keySequence; - - return $this; - } - - /** - * @param mixed $value - * @param string $expectedType - */ - private function assertType($value, string $expectedType): void - { - if ($value === null) { - throw new UnexpectedTypeException($expectedType, $value, $this->keySequence); - } - } - - /** - * @param mixed $value - * @param string $desiredType - */ - private function assertCastable($value, string $desiredType): void - { - if ($value === null) { - throw new UncastableValueException($desiredType, $this->value, $this->keySequence); - } - } } \ No newline at end of file diff --git a/tests/unit/Exception/MissingKeyExceptionTest.php b/tests/unit/Exception/MissingKeyExceptionTest.php index 4971a01..778a2f6 100644 --- a/tests/unit/Exception/MissingKeyExceptionTest.php +++ b/tests/unit/Exception/MissingKeyExceptionTest.php @@ -1,13 +1,13 @@ getInt(); + $actualValue = (new Mess(1))->getInt(); $this->assertSame(1, $actualValue); } @@ -28,12 +28,12 @@ public function testGetInt_StringValue_ThrowsUnexpectedTypeException() { $this->expectException(UnexpectedTypeException::class); - (new TypedAccessor('crusoe'))->getInt(); + (new Mess('crusoe'))->getInt(); } public function testGetBool_BoolValue_ReturnsSameBoolValue() { - $actualValue = (new TypedAccessor(true))->getBool(); + $actualValue = (new Mess(true))->getBool(); $this->assertSame(true, $actualValue); } @@ -42,12 +42,12 @@ public function testGetBool_StringValue_ThrowsUnexpectedTypeException() { $this->expectException(UnexpectedTypeException::class); - (new TypedAccessor('true'))->getBool(); + (new Mess('true'))->getBool(); } public function testGetString_StringValue_ReturnsSameStringValue() { - $actualValue = (new TypedAccessor('crusoe'))->getString(); + $actualValue = (new Mess('crusoe'))->getString(); $this->assertSame('crusoe', $actualValue); } @@ -56,12 +56,12 @@ public function testGetString_IntValue_ThrowsUnexpectedTypeException() { $this->expectException(UnexpectedTypeException::class); - (new TypedAccessor(1))->getString(); + (new Mess(1))->getString(); } public function testGetListOfInt_ListOfIntValue_ReturnsSameListOfIntValue() { - $actualValue = (new TypedAccessor([1, 5, 10]))->getListOfInt(); + $actualValue = (new Mess([1, 5, 10]))->getListOfInt(); $this->assertSame([1, 5, 10], $actualValue); } @@ -70,26 +70,26 @@ public function testGetListOfInt_AssociativeArrayOfInt_ThrowsUnexpectedTypeExcep { $this->expectException(UnexpectedTypeException::class); - (new TypedAccessor([1, 2 => 3]))->getListOfInt(); + (new Mess([1, 2 => 3]))->getListOfInt(); } public function testGetListOfInt_ListOfMixed_ThrowsUnexpectedTypeException() { $this->expectException(UnexpectedTypeException::class); - (new TypedAccessor(['a']))->getListOfInt(); + (new Mess(['a']))->getListOfInt(); } public function testGetListOfInt_Int_ThrowsUnexpectedTypeException() { $this->expectException(UnexpectedTypeException::class); - (new TypedAccessor(1))->getListOfInt(); + (new Mess(1))->getListOfInt(); } public function testGetListOfString_ListOfStringValue_ReturnsSameListOfStringValue() { - $actualValue = (new TypedAccessor(['a', 'b']))->getListOfString(); + $actualValue = (new Mess(['a', 'b']))->getListOfString(); $this->assertSame(['a', 'b'], $actualValue); } @@ -98,40 +98,40 @@ public function testGetListOfString_AssociativeArrayOfString_ThrowsUnexpectedTyp { $this->expectException(UnexpectedTypeException::class); - (new TypedAccessor(['a', 2 => 'b']))->getListOfString(); + (new Mess(['a', 2 => 'b']))->getListOfString(); } public function testGetListOfString_ListOfMixed_ThrowsUnexpectedTypeException() { $this->expectException(UnexpectedTypeException::class); - (new TypedAccessor([1]))->getListOfString(); + (new Mess([1]))->getListOfString(); } public function testGetListOfString_Int_ThrowsUnexpectedTypeException() { $this->expectException(UnexpectedTypeException::class); - (new TypedAccessor(1))->getListOfString(); + (new Mess(1))->getListOfString(); } public function testGetArrayOfStringToInt_ArrayOfStringToInt_ReturnsSameValue() { - $actualValue = (new TypedAccessor(['a' => 1, 'b' => 2]))->getArrayOfStringToInt(); + $actualValue = (new Mess(['a' => 1, 'b' => 2]))->getArrayOfStringToInt(); $this->assertSame(['a' => 1, 'b' => 2], $actualValue); } public function testGetArrayOfStringToBool_ArrayOfStringToBool_ReturnsSameValue() { - $actualValue = (new TypedAccessor(['a' => true, 'b' => false]))->getAsArrayOfStringToBool(); + $actualValue = (new Mess(['a' => true, 'b' => false]))->getAsArrayOfStringToBool(); $this->assertSame(['a' => true, 'b' => false], $actualValue); } public function testGetArrayOfStringToString_ArrayOfStringToString_ReturnsSameValue() { - $actualValue = (new TypedAccessor(['a' => 'A', 'b' => 'B']))->getAsArrayOfStringToString(); + $actualValue = (new Mess(['a' => 'A', 'b' => 'B']))->getAsArrayOfStringToString(); $this->assertSame(['a' => 'A', 'b' => 'B'], $actualValue); } @@ -141,7 +141,7 @@ public function testGetArrayOfStringToString_ArrayOfStringToString_ReturnsSameVa */ public function testGetAsInt_GivenCastableValue_ReturnsMatchingCastedValue($value, int $castedValue) { - $actualValue = (new TypedAccessor($value))->getAsInt(); + $actualValue = (new Mess($value))->getAsInt(); $this->assertSame($castedValue, $actualValue); } @@ -166,7 +166,7 @@ public function testGetAsInt_GivenUncastableValue_ThrowsUncastableValueException { $this->expectException(UncastableValueException::class); - (new TypedAccessor($value))->getAsInt(); + (new Mess($value))->getAsInt(); } /** @@ -193,7 +193,7 @@ function () { */ public function testGetAsBool_GivenCastableValue_ReturnsMatchingCastedValue($value, bool $castedValue) { - $actualValue = (new TypedAccessor($value))->getAsBool(); + $actualValue = (new Mess($value))->getAsBool(); $this->assertSame($castedValue, $actualValue); } @@ -222,7 +222,7 @@ public function testGetAsBool_GivenUncastableValue_ThrowsUncastableValueExceptio { $this->expectException(UncastableValueException::class); - (new TypedAccessor($value))->getAsBool(); + (new Mess($value))->getAsBool(); } /** @@ -247,7 +247,7 @@ function () { */ public function testGetAsString_GivenCastableValue_ReturnsMatchingCastedValue($value, string $castedValue) { - $actualValue = (new TypedAccessor($value))->getAsString(); + $actualValue = (new Mess($value))->getAsString(); $this->assertSame($castedValue, $actualValue); } @@ -270,7 +270,7 @@ public function testGetAsString_GiveUncastableValue_ThrowsUncastableValueExcepti { $this->expectException(UncastableValueException::class); - (new TypedAccessor($value))->getAsString(); + (new Mess($value))->getAsString(); } /** @@ -294,7 +294,7 @@ function () { */ public function testGetAsListOfInt_GivenCastableValue_ReturnsMatchingCastedValue($value, array $castedValue) { - $actualValue = (new TypedAccessor($value))->getAsListOfInt(); + $actualValue = (new Mess($value))->getAsListOfInt(); $this->assertSame($castedValue, $actualValue); } @@ -318,7 +318,7 @@ public function testGetAsListOfInt_GivenUncastableValue_ThrowsUuncastableValueEx { $this->expectException(UncastableValueException::class); - (new TypedAccessor($value))->getAsListOfInt(); + (new Mess($value))->getAsListOfInt(); } /** @@ -338,7 +338,7 @@ public function providerCannotCastToListOfIntValues() */ public function testGetAsListOfString_GivenCastableValue_ReturnsMatchingCastedValue($value, array $castedValue) { - $actualValue = (new TypedAccessor($value))->getAsListOfString(); + $actualValue = (new Mess($value))->getAsListOfString(); $this->assertSame($castedValue, $actualValue); } @@ -362,7 +362,7 @@ public function testGetAsListOfString_GivenUncastableValue_ThrowsUncastableValue { $this->expectException(UncastableValueException::class); - (new TypedAccessor($value))->getAsListOfString(); + (new Mess($value))->getAsListOfString(); } /** @@ -382,7 +382,7 @@ public function providerCannotCastToListOfStringValues() */ public function testGetAsArrayOfStringToInt_GivenCastableValue_ReturnsMatchingCastedValue($value, array $castedValue) { - $actualValue = (new TypedAccessor($value))->getAsArrayOfStringToInt(); + $actualValue = (new Mess($value))->getAsArrayOfStringToInt(); $this->assertSame($castedValue, $actualValue); } @@ -406,7 +406,7 @@ public function testGetAsArrayOfStringToInt_GivenUncastableValue_ThrowsUncastabl { $this->expectException(UncastableValueException::class); - (new TypedAccessor($value))->getAsArrayOfStringToInt(); + (new Mess($value))->getAsArrayOfStringToInt(); } /** @@ -423,98 +423,98 @@ public function providerCannotCastToArrayOfStringToIntValues() public function testFindInt_IntValue_ReturnsSameIntValue() { - $actualValue = (new TypedAccessor(1))->findInt(); + $actualValue = (new Mess(1))->findInt(); $this->assertSame(1, $actualValue); } public function testFindInt_StringValue_ReturnsNull() { - $actualValue = (new TypedAccessor('crusoe'))->findInt(); + $actualValue = (new Mess('crusoe'))->findInt(); $this->assertNull($actualValue); } public function testFindBool_BoolValue_ReturnsSameBoolValue() { - $actualValue = (new TypedAccessor(true))->findBool(); + $actualValue = (new Mess(true))->findBool(); $this->assertSame(true, $actualValue); } public function testFindBool_StringValue_ReturnsNull() { - $actualValue = (new TypedAccessor('crusoe'))->findBool(); + $actualValue = (new Mess('crusoe'))->findBool(); $this->assertNull($actualValue); } public function testFindString_StringValue_ReturnsSameStringValue() { - $actualValue = (new TypedAccessor('crusoe'))->findString(); + $actualValue = (new Mess('crusoe'))->findString(); $this->assertSame('crusoe', $actualValue); } public function testFindString_GivenUncastableValue_ReturnsNull() { - $value = (new TypedAccessor(1))->findString(); + $value = (new Mess(1))->findString(); $this->assertNull($value); } public function testFindListOfInt_ListOfIntValue_ReturnsSameListOfIntValue() { - $actualValue = (new TypedAccessor([1, 5, 10]))->findListOfInt(); + $actualValue = (new Mess([1, 5, 10]))->findListOfInt(); $this->assertSame([1, 5, 10], $actualValue); } public function testFindListOfInt_AssociativeArrayOfInt_ReturnsNull() { - $actualValue = (new TypedAccessor([1, 2 => 3]))->findListOfInt(); + $actualValue = (new Mess([1, 2 => 3]))->findListOfInt(); $this->assertNull($actualValue); } public function testFindListOfInt_ListOfMixed_ReturnsNull() { - $actualValue = (new TypedAccessor(['a']))->findListOfInt(); + $actualValue = (new Mess(['a']))->findListOfInt(); $this->assertNull($actualValue); } public function testFindListOfInt_Int_ReturnsNull() { - $actualValue = (new TypedAccessor(1))->findListOfInt(); + $actualValue = (new Mess(1))->findListOfInt(); $this->assertNull($actualValue); } public function testFindListOfString_ListOfStringValue_ReturnsSameListOfStringValue() { - $actualValue = (new TypedAccessor(['a', 'b']))->findListOfString(); + $actualValue = (new Mess(['a', 'b']))->findListOfString(); $this->assertSame(['a', 'b'], $actualValue); } public function testFindListOfString_AssociativeArrayOfString_ReturnsNull() { - $actualValue = (new TypedAccessor(['a', 2 => 'b']))->findListOfString(); + $actualValue = (new Mess(['a', 2 => 'b']))->findListOfString(); $this->assertNull($actualValue); } public function testFindListOfString_ListOfMixed_ReturnsNull() { - $actualValue = (new TypedAccessor([1]))->findListOfString(); + $actualValue = (new Mess([1]))->findListOfString(); $this->assertNull($actualValue); } public function testFindListOfString_Int_ReturnsNull() { - $actualValue = (new TypedAccessor(1))->findListOfString(); + $actualValue = (new Mess(1))->findListOfString(); $this->assertNull($actualValue); } @@ -524,7 +524,7 @@ public function testFindListOfString_Int_ReturnsNull() */ public function testFindAsInt_GivenCastableValue_ReturnsMatchingCastedValue($value, int $castedValue) { - $actualValue = (new TypedAccessor($value))->findAsInt(); + $actualValue = (new Mess($value))->findAsInt(); $this->assertSame($castedValue, $actualValue); } @@ -534,7 +534,7 @@ public function testFindAsInt_GivenCastableValue_ReturnsMatchingCastedValue($val */ public function testFindAsInt_GivenUncastableValue_ReturnsNull($value) { - $actualValue = (new TypedAccessor($value))->findAsInt(); + $actualValue = (new Mess($value))->findAsInt(); $this->assertNull($actualValue); } @@ -544,7 +544,7 @@ public function testFindAsInt_GivenUncastableValue_ReturnsNull($value) */ public function testFindAsBool_GivenCastableValue_ReturnsMatchingCastedValue($value, bool $castedValue) { - $actualValue = (new TypedAccessor($value))->findAsBool(); + $actualValue = (new Mess($value))->findAsBool(); $this->assertSame($castedValue, $actualValue); } @@ -554,7 +554,7 @@ public function testFindAsBool_GivenCastableValue_ReturnsMatchingCastedValue($va */ public function testFindAsBool_GivenUncastableValue_ReturnsNull($value) { - $actualValue = (new TypedAccessor($value))->findAsBool(); + $actualValue = (new Mess($value))->findAsBool(); $this->assertNull($actualValue); } @@ -564,7 +564,7 @@ public function testFindAsBool_GivenUncastableValue_ReturnsNull($value) */ public function testFindAsString_GivenCastableValue_ReturnsMatchingCastedValue($value, string $castedValue) { - $actualValue = (new TypedAccessor($value))->findAsString(); + $actualValue = (new Mess($value))->findAsString(); $this->assertSame($castedValue, $actualValue); } @@ -574,7 +574,7 @@ public function testFindAsString_GivenCastableValue_ReturnsMatchingCastedValue($ */ public function testFindAsString_GivenUncastableValue_ReturnsNull($value) { - $actualValue = (new TypedAccessor($value))->findAsString(); + $actualValue = (new Mess($value))->findAsString(); $this->assertNull($actualValue); } @@ -584,7 +584,7 @@ public function testFindAsString_GivenUncastableValue_ReturnsNull($value) */ public function testFindAsListOfInt_GivenCastableValue_ReturnsMatchingCastedValue($value, array $castedValue) { - $actualValue = (new TypedAccessor($value))->findAsListOfInt(); + $actualValue = (new Mess($value))->findAsListOfInt(); $this->assertSame($castedValue, $actualValue); } @@ -594,7 +594,7 @@ public function testFindAsListOfInt_GivenCastableValue_ReturnsMatchingCastedValu */ public function testFindAsListOfInt_GivenUncastableValue_ReturnsNull($value) { - $actualValue = (new TypedAccessor($value))->findAsListOfInt(); + $actualValue = (new Mess($value))->findAsListOfInt(); $this->assertNull($actualValue); } @@ -604,7 +604,7 @@ public function testFindAsListOfInt_GivenUncastableValue_ReturnsNull($value) */ public function testFindAsListOfString_GivenCastableValue_ReturnsMatchingCastedValue($value, array $castedValue) { - $actualValue = (new TypedAccessor($value))->findAsListOfString(); + $actualValue = (new Mess($value))->findAsListOfString(); $this->assertSame($castedValue, $actualValue); } @@ -614,14 +614,14 @@ public function testFindAsListOfString_GivenCastableValue_ReturnsMatchingCastedV */ public function testFindAsListOfString_GivenUncastableValue_ReturnsNull($value) { - $actualValue = (new TypedAccessor($value))->findAsListOfString(); + $actualValue = (new Mess($value))->findAsListOfString(); $this->assertNull($actualValue); } public function testGetMixed_AnyValue_ReturnsSameValue() { - $actualValue = (new TypedAccessor('1'))->getMixed(); + $actualValue = (new Mess('1'))->getMixed(); $this->assertSame('1', $actualValue); } @@ -629,7 +629,7 @@ public function testGetMixed_AnyValue_ReturnsSameValue() public function testGetObject_Object_ReturnsSameObject() { $object = new stdClass(); - $actualValue = (new TypedAccessor($object))->getObject(); + $actualValue = (new Mess($object))->getObject(); $this->assertSame($object, $actualValue); } @@ -638,12 +638,12 @@ public function testGetObject_Int_ThrowsUnexpectedTypeException() { $this->expectException(UnexpectedTypeException::class); - (new TypedAccessor(1))->getObject(); + (new Mess(1))->getObject(); } public function testGetArray_Array_ReturnsSameArray() { - $actualValue = (new TypedAccessor([1]))->getArray(); + $actualValue = (new Mess([1]))->getArray(); $this->assertSame([1], $actualValue); } @@ -652,12 +652,12 @@ public function testGetArray_Int_ThrowsUnexpectedTypeException() { $this->expectException(UnexpectedTypeException::class); - (new TypedAccessor(1))->getArray(); + (new Mess(1))->getArray(); } public function testFindMixed_AnyValue_ReturnsSameValue() { - $actualValue = (new TypedAccessor('1'))->findMixed(); + $actualValue = (new Mess('1'))->findMixed(); $this->assertSame('1', $actualValue); } @@ -665,98 +665,98 @@ public function testFindMixed_AnyValue_ReturnsSameValue() public function testFindObject_Object_ReturnsSameObject() { $object = new stdClass(); - $actualValue = (new TypedAccessor($object))->findObject(); + $actualValue = (new Mess($object))->findObject(); $this->assertSame($object, $actualValue); } public function testFindObject_Array_ReturnsNull() { - $actualValue = (new TypedAccessor([]))->findObject(); + $actualValue = (new Mess([]))->findObject(); $this->assertNull($actualValue); } public function testFindArray_Array_ReturnsSameArray() { - $actualValue = (new TypedAccessor([1]))->findArray(); + $actualValue = (new Mess([1]))->findArray(); $this->assertSame([1], $actualValue); } public function testFindArray_Int_ReturnsNull() { - $actualValue = (new TypedAccessor(1))->findArray(); + $actualValue = (new Mess(1))->findArray(); $this->assertNull($actualValue); } public function testOffsetExists_ArrayWithExistingOffset_ReturnsTrue() { - $accessor = new TypedAccessor([1]); + $accessor = new Mess([1]); $this->assertTrue(isset($accessor[0])); } public function testOffsetExists_ArrayWithNonExistingOffset_ReturnsFalse() { - $accessor = new TypedAccessor([]); + $accessor = new Mess([]); $this->assertFalse(isset($accessor[0])); } public function testOffsetExists_NonArray_ReturnsFalse() { - $accessor = new TypedAccessor(1); + $accessor = new Mess(1); $this->assertFalse(isset($accessor[0])); } public function testOffsetGet_ArrayWithExistingOffset_ReturnsTypedValueAccessor() { - $accessor = new TypedAccessor([1]); + $accessor = new Mess([1]); - $this->assertInstanceOf(TypedAccessor::class, $accessor[0]); + $this->assertInstanceOf(Mess::class, $accessor[0]); } public function testOffsetGet_InnerArrayWithExistingOffset_ReturnsTypedValueAccessor() { - $accessor = new TypedAccessor([0 => [0 => 1]]); + $accessor = new Mess([0 => [0 => 1]]); - $this->assertInstanceOf(TypedAccessor::class, $accessor[0][0]); + $this->assertInstanceOf(Mess::class, $accessor[0][0]); } public function testOffsetGet_ArrayWithNonExistingOffset_ReturnsMissingValueAccessor() { - $accessor = new TypedAccessor([1]); + $accessor = new Mess([1]); - $this->assertInstanceOf(MissingValueAccessor::class, $accessor[1]); + $this->assertInstanceOf(MissingMess::class, $accessor[1]); } public function testOffsetGet_InnerArrayWithNonExistingOffset_ReturnsMissingValueAccessor() { - $accessor = new TypedAccessor([0 => [0 => 1]]); + $accessor = new Mess([0 => [0 => 1]]); - $this->assertInstanceOf(MissingValueAccessor::class, $accessor[0][1]); + $this->assertInstanceOf(MissingMess::class, $accessor[0][1]); } public function testOffsetGet_String_ReturnsValueByKey() { - $accessor = new TypedAccessor(['key' => 1]); + $accessor = new Mess(['key' => 1]); $this->assertSame(1, $accessor['key']->getInt()); } public function testOffsetGet_Int_ReturnsValueByKey() { - $accessor = new TypedAccessor([0 => 1]); + $accessor = new Mess([0 => 1]); $this->assertSame(1, $accessor[0]->getInt()); } public function testOffsetGet_Bool_ThrowsUnexpectedKeyTypeException() { - $accessor = new TypedAccessor([0 => 1]); + $accessor = new Mess([0 => 1]); $this->expectException(UnexpectedKeyTypeException::class); @@ -765,17 +765,17 @@ public function testOffsetGet_Bool_ThrowsUnexpectedKeyTypeException() public function testOffsetSet_Offset_ThrowsCannotModifyAccessorException() { - $this->expectException(CannotModifyAccessorException::class); + $this->expectException(CannotModifyMessException::class); - $accessor = new TypedAccessor([1]); + $accessor = new Mess([1]); $accessor[0] = 1; } public function testOffsetUnset_Offset_ThrowsCannotModifyAccessorException() { - $this->expectException(CannotModifyAccessorException::class); + $this->expectException(CannotModifyMessException::class); - $accessor = new TypedAccessor([1]); + $accessor = new Mess([1]); unset($accessor[0]); } } \ No newline at end of file diff --git a/tests/unit/MissingValueAccessorTest.php b/tests/unit/MissingMessTest.php similarity index 66% rename from tests/unit/MissingValueAccessorTest.php rename to tests/unit/MissingMessTest.php index e13e02e..5679f96 100644 --- a/tests/unit/MissingValueAccessorTest.php +++ b/tests/unit/MissingMessTest.php @@ -1,22 +1,22 @@ expectException(MissingKeyException::class); @@ -25,7 +25,7 @@ public function testGetInt_CorrectAccessor_ThrowsMissingKeyException() public function testGetBool_CorrectAccessor_ThrowsMissingKeyException() { - $accessor = new MissingValueAccessor([]); + $accessor = new MissingMess([]); $this->expectException(MissingKeyException::class); @@ -34,7 +34,7 @@ public function testGetBool_CorrectAccessor_ThrowsMissingKeyException() public function testGetString_CorrectAccessor_ThrowsMissingKeyException() { - $accessor = new MissingValueAccessor([]); + $accessor = new MissingMess([]); $this->expectException(MissingKeyException::class); @@ -43,7 +43,7 @@ public function testGetString_CorrectAccessor_ThrowsMissingKeyException() public function testGetAsInt_CorrectAccessor_ThrowsMissingKeyException() { - $accessor = new MissingValueAccessor([]); + $accessor = new MissingMess([]); $this->expectException(MissingKeyException::class); @@ -52,7 +52,7 @@ public function testGetAsInt_CorrectAccessor_ThrowsMissingKeyException() public function testGetAsBool_CorrectAccessor_ThrowsMissingKeyException() { - $accessor = new MissingValueAccessor([]); + $accessor = new MissingMess([]); $this->expectException(MissingKeyException::class); @@ -61,7 +61,7 @@ public function testGetAsBool_CorrectAccessor_ThrowsMissingKeyException() public function testGetAsString_CorrectAccessor_ThrowsMissingKeyException() { - $accessor = new MissingValueAccessor([]); + $accessor = new MissingMess([]); $this->expectException(MissingKeyException::class); @@ -70,49 +70,49 @@ public function testGetAsString_CorrectAccessor_ThrowsMissingKeyException() public function testFindInt_CorrectAccessor_ReturnsNull() { - $accessor = new MissingValueAccessor([]); + $accessor = new MissingMess([]); $this->assertNull($accessor->findInt()); } public function testFindBool_CorrectAccessor_ReturnsNull() { - $accessor = new MissingValueAccessor([]); + $accessor = new MissingMess([]); $this->assertNull($accessor->findBool()); } public function testFindString_CorrectAccessor_ReturnsNull() { - $accessor = new MissingValueAccessor([]); + $accessor = new MissingMess([]); $this->assertNull($accessor->findString()); } public function testFindAsInt_CorrectAccessor_ReturnsNull() { - $accessor = new MissingValueAccessor([]); + $accessor = new MissingMess([]); $this->assertNull($accessor->findInt()); } public function testFindAsBool_CorrectAccessor_ReturnsNull() { - $accessor = new MissingValueAccessor([]); + $accessor = new MissingMess([]); $this->assertNull($accessor->findBool()); } public function testFindAsString_CorrectAccessor_ReturnsNull() { - $accessor = new MissingValueAccessor([]); + $accessor = new MissingMess([]); $this->assertNull($accessor->findString()); } public function testGetMixed_CorrectAccessor_ThrowsMissingKeyException() { - $accessor = new MissingValueAccessor([]); + $accessor = new MissingMess([]); $this->expectException(MissingKeyException::class); @@ -121,28 +121,28 @@ public function testGetMixed_CorrectAccessor_ThrowsMissingKeyException() public function testFindMixed_CorrectAccessor_ReturnsNull() { - $accessor = new MissingValueAccessor([]); + $accessor = new MissingMess([]); $this->assertNull($accessor->findMixed()); } public function testOffsetExists_CorrectAccessor_ReturnsFalse() { - $accessor = new MissingValueAccessor([]); + $accessor = new MissingMess([]); $this->assertFalse($accessor->offsetExists(0)); } public function testOffsetGet_CorrectAccessor_ReturnsMissingValueAccessor() { - $accessor = new TypedAccessor([]); + $accessor = new Mess([]); - $this->assertInstanceOf(MissingValueAccessor::class, $accessor[0]); + $this->assertInstanceOf(MissingMess::class, $accessor[0]); } public function testOffsetGet_CorrectAccessorWithFluentAccess_ThrowsMissingKeyException() { - $accessor = new TypedAccessor([]); + $accessor = new Mess([]); try { $accessor['a']['b']['c']->getInt(); } catch (MissingKeyException $e) { @@ -155,18 +155,18 @@ public function testOffsetGet_CorrectAccessorWithFluentAccess_ThrowsMissingKeyEx public function testOffsetSet_CorrectAccessor_ThrowsCannotModifyAccessorException() { - $accessor = new MissingValueAccessor([]); + $accessor = new MissingMess([]); - $this->expectException(CannotModifyAccessorException::class); + $this->expectException(CannotModifyMessException::class); $accessor[0] = 1; } public function testOffsetUnset_CorrectAccessor_ThrowsCannotModifyAccessorException() { - $accessor = new MissingValueAccessor([]); + $accessor = new MissingMess([]); - $this->expectException(CannotModifyAccessorException::class); + $this->expectException(CannotModifyMessException::class); unset($accessor[0]); }