From f0511eb604e0e10e3d3791997ddddaaf2c43bea4 Mon Sep 17 00:00:00 2001 From: maximtrunnikov Date: Sat, 15 Oct 2022 20:44:00 +0300 Subject: [PATCH] reremake --- src/Arrayable.php | 4 +- src/Arrayable/ArrCast.php | 6 +- src/Arrayable/ArrCombined.php | 10 ++-- src/Arrayable/ArrEnvelope.php | 4 +- src/Arrayable/ArrExploded.php | 8 +-- src/Arrayable/ArrFiltered.php | 6 +- src/Arrayable/ArrFlatten.php | 8 +-- src/Arrayable/ArrFromCallback.php | 8 +-- src/Arrayable/ArrKeys.php | 6 +- src/Arrayable/ArrMapped.php | 6 +- src/Arrayable/ArrMerged.php | 6 +- src/Arrayable/ArrObject.php | 6 +- src/Arrayable/ArrRange.php | 6 +- src/Arrayable/ArrReversed.php | 6 +- src/Arrayable/ArrSorted.php | 6 +- src/Arrayable/ArrSortedByKeys.php | 6 +- src/Arrayable/ArrSticky.php | 3 +- src/Arrayable/ArrUnique.php | 6 +- src/Arrayable/ArrValues.php | 6 +- src/Arrayable/ArrayableOf.php | 2 +- src/Arrayable/CountArrayable.php | 2 +- src/Arrayable/HasArrayableIterator.php | 2 +- src/Boolean.php | 4 +- src/Boolean/BoolEnvelope.php | 4 +- src/Boolean/BooleanOf.php | 2 +- src/Boolean/Conjunction.php | 8 +-- src/Boolean/Disjunction.php | 8 +-- src/Boolean/EqualityOf.php | 8 +-- src/Boolean/InArray.php | 8 +-- src/Boolean/KeyExists.php | 8 +-- src/Boolean/Not.php | 8 +-- src/Boolean/PregMatch.php | 8 +-- src/Boolean/StrContains.php | 8 +-- src/Number.php | 4 +- src/Number/ArraySum.php | 8 +-- src/Number/LengthOf.php | 8 +-- src/Number/MaxOf.php | 8 +-- src/Number/MinOf.php | 8 +-- src/Number/NumEnvelope.php | 4 +- src/Number/NumSticky.php | 4 +- src/Number/NumberOf.php | 6 +- src/Number/Rounded.php | 8 +-- src/Number/SumOf.php | 6 +- src/Scalar/AtKey.php | 4 +- src/Scalar/CastMixed.php | 76 +++++++++++++++++++++++++ src/Scalar/CastScalar.php | 56 ------------------ src/Scalar/FirstOf.php | 5 +- src/Scalar/LastOf.php | 6 +- src/Scalar/SclTernary.php | 8 +-- src/Text.php | 4 +- src/Text/StringableText.php | 2 +- src/Text/TextOf.php | 6 +- src/Text/TxtEnvelope.php | 4 +- src/Text/TxtFromCallback.php | 8 +-- src/Text/TxtImploded.php | 8 +-- src/Text/TxtJsonEncoded.php | 8 +-- src/Text/TxtLowered.php | 6 +- src/Text/TxtLtrimmed.php | 6 +- src/Text/TxtPregReplaced.php | 10 ++-- src/Text/TxtReplaced.php | 10 ++-- src/Text/TxtRtrimmed.php | 6 +- src/Text/TxtSticky.php | 2 +- src/Text/TxtSubstr.php | 8 +-- src/Text/TxtTrimmed.php | 6 +- src/Text/TxtUpper.php | 6 +- tests/Arrayable/ArrCombinedTest.php | 2 +- tests/Arrayable/ArrFromCallbackTest.php | 2 +- tests/Arrayable/ArrKeysTest.php | 4 +- tests/Arrayable/ArrMappedTest.php | 8 +-- tests/Arrayable/ArrReversedTest.php | 1 - tests/Arrayable/ArrSortedTest.php | 4 +- tests/Arrayable/ArrStickyTest.php | 6 +- tests/Boolean/KeyExistsTest.php | 1 - tests/Number/NumStickyTest.php | 4 +- tests/TestCase.php | 10 ++-- tests/Text/TxtFromCallbackTest.php | 2 +- tests/Text/TxtPregReplacedTest.php | 1 - tests/Text/TxtStickyTest.php | 7 +-- tests/Text/TxtUpperTest.php | 4 +- 79 files changed, 294 insertions(+), 278 deletions(-) create mode 100644 src/Scalar/CastMixed.php delete mode 100644 src/Scalar/CastScalar.php diff --git a/src/Arrayable.php b/src/Arrayable.php index 42dc9f8..d167803 100644 --- a/src/Arrayable.php +++ b/src/Arrayable.php @@ -11,11 +11,11 @@ /** * Arrayable. */ -interface Arrayable extends Scalar, Countable, IteratorAggregate +interface Arrayable extends Countable, IteratorAggregate { /** * @return array * @throws Exception */ - public function value(): array; + public function asArray(): array; } diff --git a/src/Arrayable/ArrCast.php b/src/Arrayable/ArrCast.php index bf5f3a7..e561996 100644 --- a/src/Arrayable/ArrCast.php +++ b/src/Arrayable/ArrCast.php @@ -5,14 +5,14 @@ namespace Maxonfjvipon\Elegant_Elephant\Arrayable; use Maxonfjvipon\Elegant_Elephant\Arrayable; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Array cast. */ final class ArrCast extends ArrEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -24,7 +24,7 @@ public function __construct($arr) parent::__construct( new ArrMapped( $arr, - fn ($value) => $this->scalarCast($value) + fn ($value) => $this->mixedCast($value) ) ); } diff --git a/src/Arrayable/ArrCombined.php b/src/Arrayable/ArrCombined.php index 05ba795..28d2348 100644 --- a/src/Arrayable/ArrCombined.php +++ b/src/Arrayable/ArrCombined.php @@ -7,14 +7,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Arrayable; use Maxonfjvipon\Elegant_Elephant\Number; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Array combined of keys and values. */ final class ArrCombined extends ArrEnvelope { - use CastScalar; + use CastMixed; /** * @var array|Arrayable $keys @@ -38,10 +38,10 @@ public function __construct($keys, $values) new ArrFromCallback( function () use ($keys, $values) { /** @var array $keys */ - $keys = (array) $this->scalarCast($keys); + $keys = (array) $this->mixedCast($keys); /** @var array $values */ - $values = (array) $this->scalarCast($values); + $values = (array) $this->mixedCast($values); if (count($keys) !== count($values)) { throw new Exception("Keys and values arrays must have the same length"); @@ -65,7 +65,7 @@ private function mapped(array $array): array { return array_map( function ($item) { - if (is_array($cast = $this->scalarCast($item))) { + if (is_array($cast = $this->mixedCast($item))) { throw new Exception("Array can't be the key of array"); } diff --git a/src/Arrayable/ArrEnvelope.php b/src/Arrayable/ArrEnvelope.php index b3a6117..2db999f 100644 --- a/src/Arrayable/ArrEnvelope.php +++ b/src/Arrayable/ArrEnvelope.php @@ -34,8 +34,8 @@ public function __construct(Arrayable $origin) * @return array * @throws Exception */ - public function value(): array + public function asArray(): array { - return $this->origin->value(); + return $this->origin->asArray(); } } diff --git a/src/Arrayable/ArrExploded.php b/src/Arrayable/ArrExploded.php index 95e1550..4225c7a 100644 --- a/src/Arrayable/ArrExploded.php +++ b/src/Arrayable/ArrExploded.php @@ -5,7 +5,7 @@ namespace Maxonfjvipon\Elegant_Elephant\Arrayable; use Exception; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use Maxonfjvipon\Elegant_Elephant\Text; /** @@ -13,7 +13,7 @@ */ final class ArrExploded extends ArrEnvelope { - use CastScalar; + use CastMixed; /** * Exploded by comma. @@ -38,9 +38,9 @@ public function __construct($separator, $text) new ArrFromCallback( function () use ($separator, $text) { /** @var non-empty-string $separator */ - $separator = (string) $this->scalarCast($separator); + $separator = (string) $this->mixedCast($separator); - $exploded = explode($separator, (string) $this->scalarCast($text)); + $exploded = explode($separator, (string) $this->mixedCast($text)); if (!is_array($exploded)) { throw new Exception("Separator can't be an empty string or instance of TxtBlank class"); diff --git a/src/Arrayable/ArrFiltered.php b/src/Arrayable/ArrFiltered.php index 7e26c00..79e7eec 100644 --- a/src/Arrayable/ArrFiltered.php +++ b/src/Arrayable/ArrFiltered.php @@ -6,14 +6,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Arrayable; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Filtered array. */ final class ArrFiltered extends ArrEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -25,7 +25,7 @@ public function __construct($arr, callable $callback) { parent::__construct( new ArrFromCallback( - fn () => array_filter((array) $this->scalarCast($arr), $callback, ARRAY_FILTER_USE_BOTH) + fn () => array_filter((array) $this->mixedCast($arr), $callback, ARRAY_FILTER_USE_BOTH) ) ); } diff --git a/src/Arrayable/ArrFlatten.php b/src/Arrayable/ArrFlatten.php index 59adae2..90922ca 100644 --- a/src/Arrayable/ArrFlatten.php +++ b/src/Arrayable/ArrFlatten.php @@ -6,14 +6,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Arrayable; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Flatten array. */ final class ArrFlatten extends ArrEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -25,7 +25,7 @@ public function __construct($arr, int $deep = 1) { parent::__construct( new ArrFromCallback( - fn () => $this->flat($this->scalarCast($arr), [], $deep) + fn () => $this->flat($this->mixedCast($arr), [], $deep) ) ); } @@ -42,7 +42,7 @@ private function flat(array $arr, array $new, int $neededDeep, int $currentDeep { foreach ($arr as $item) { if ($neededDeep !== $currentDeep && (is_array($item) || $item instanceof Arrayable)) { - $new = $this->flat($this->scalarCast($item), $new, $neededDeep, $currentDeep + 1); + $new = $this->flat($this->mixedCast($item), $new, $neededDeep, $currentDeep + 1); } else { $new[] = $item; } diff --git a/src/Arrayable/ArrFromCallback.php b/src/Arrayable/ArrFromCallback.php index 08ef6a4..1c09347 100644 --- a/src/Arrayable/ArrFromCallback.php +++ b/src/Arrayable/ArrFromCallback.php @@ -6,14 +6,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Arrayable; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Array from callback */ final class ArrFromCallback implements Arrayable { - use CastScalar; + use CastMixed; use CountArrayable; use HasArrayableIterator; @@ -47,9 +47,9 @@ public function __construct(callable $callback) * @return array * @throws Exception */ - public function value(): array + public function asArray(): array { - if (!is_array($res = $this->scalarOrCallableCast($this->callback))) { + if (!is_array($res = $this->mixedOrCallableCast($this->callback))) { throw new Exception("Callback must return an array or Arrayable!"); } diff --git a/src/Arrayable/ArrKeys.php b/src/Arrayable/ArrKeys.php index 694f623..2cd5bd0 100644 --- a/src/Arrayable/ArrKeys.php +++ b/src/Arrayable/ArrKeys.php @@ -6,14 +6,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Arrayable; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Array keys. */ final class ArrKeys extends ArrEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -24,7 +24,7 @@ public function __construct($arr) { parent::__construct( new ArrFromCallback( - fn () => array_keys((array) $this->scalarCast($arr)) + fn () => array_keys((array) $this->mixedCast($arr)) ) ); } diff --git a/src/Arrayable/ArrMapped.php b/src/Arrayable/ArrMapped.php index 77f94db..5328575 100644 --- a/src/Arrayable/ArrMapped.php +++ b/src/Arrayable/ArrMapped.php @@ -6,7 +6,7 @@ use Closure; use Maxonfjvipon\Elegant_Elephant\Arrayable; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use ReflectionFunction; /** @@ -14,7 +14,7 @@ */ final class ArrMapped extends ArrEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -30,7 +30,7 @@ function () use ($arr, $callback) { $count = (new ReflectionFunction(Closure::fromCallable($callback)))->getNumberOfParameters(); /** @var array $array */ - $array = $this->scalarCast($arr); + $array = $this->mixedCast($arr); /** @var array> $arguments */ $arguments = $count > 1 ? [array_keys($array), $array] : [$array]; diff --git a/src/Arrayable/ArrMerged.php b/src/Arrayable/ArrMerged.php index ad41c5e..efa2998 100644 --- a/src/Arrayable/ArrMerged.php +++ b/src/Arrayable/ArrMerged.php @@ -5,14 +5,14 @@ namespace Maxonfjvipon\Elegant_Elephant\Arrayable; use Maxonfjvipon\Elegant_Elephant\Arrayable; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Merged array. */ final class ArrMerged extends ArrEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -23,7 +23,7 @@ public function __construct(...$items) { parent::__construct( new ArrFromCallback( - fn () => array_merge(...$this->scalarsCast(...$items)) + fn () => array_merge(...$this->mixedArrCast(...$items)) ) ); } diff --git a/src/Arrayable/ArrObject.php b/src/Arrayable/ArrObject.php index 75ca20d..ce8525d 100644 --- a/src/Arrayable/ArrObject.php +++ b/src/Arrayable/ArrObject.php @@ -5,7 +5,7 @@ namespace Maxonfjvipon\Elegant_Elephant\Arrayable; use Maxonfjvipon\Elegant_Elephant\Number; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use Maxonfjvipon\Elegant_Elephant\Text; /** @@ -13,7 +13,7 @@ */ final class ArrObject extends ArrEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -25,7 +25,7 @@ public function __construct($key, $object) { parent::__construct( new ArrFromCallback( - fn () => [$this->scalarCast($key) => $this->scalarCast($object)] + fn () => [$this->mixedCast($key) => $this->mixedCast($object)] ) ); } diff --git a/src/Arrayable/ArrRange.php b/src/Arrayable/ArrRange.php index 2137b31..a22abe5 100644 --- a/src/Arrayable/ArrRange.php +++ b/src/Arrayable/ArrRange.php @@ -6,14 +6,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Number; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Array range. */ final class ArrRange extends ArrEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -26,7 +26,7 @@ public function __construct($from, $to, $step = 1) { parent::__construct( new ArrFromCallback( - fn () => range(...$this->scalarsCast($from, $to, $step)) + fn () => range(...$this->mixedArrCast($from, $to, $step)) ) ); } diff --git a/src/Arrayable/ArrReversed.php b/src/Arrayable/ArrReversed.php index bb85d18..9b433f4 100644 --- a/src/Arrayable/ArrReversed.php +++ b/src/Arrayable/ArrReversed.php @@ -5,14 +5,14 @@ namespace Maxonfjvipon\Elegant_Elephant\Arrayable; use Maxonfjvipon\Elegant_Elephant\Arrayable; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Reversed array. */ final class ArrReversed extends ArrEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -23,7 +23,7 @@ public function __construct($arr) { parent::__construct( new ArrFromCallback( - fn () => array_reverse((array) $this->scalarCast($arr)) + fn () => array_reverse((array) $this->mixedCast($arr)) ) ); } diff --git a/src/Arrayable/ArrSorted.php b/src/Arrayable/ArrSorted.php index b286fa8..78158df 100644 --- a/src/Arrayable/ArrSorted.php +++ b/src/Arrayable/ArrSorted.php @@ -6,14 +6,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Arrayable; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Array sorted. */ final class ArrSorted extends ArrEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -26,7 +26,7 @@ public function __construct($arr, $compare = null) parent::__construct( new ArrFromCallback( function () use ($arr, $compare) { - $arr = (array) $this->scalarCast($arr); + $arr = (array) $this->mixedCast($arr); if ($compare != null) { usort($arr, is_string($compare) diff --git a/src/Arrayable/ArrSortedByKeys.php b/src/Arrayable/ArrSortedByKeys.php index c7961f2..77f6678 100644 --- a/src/Arrayable/ArrSortedByKeys.php +++ b/src/Arrayable/ArrSortedByKeys.php @@ -6,14 +6,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Arrayable; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Array sorted by keys. */ final class ArrSortedByKeys extends ArrEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -26,7 +26,7 @@ public function __construct($arr, $compare = null) parent::__construct( new ArrFromCallback( function () use ($arr, $compare) { - $arr = (array) $this->scalarCast($arr); + $arr = (array) $this->mixedCast($arr); if (!!$compare && !is_callable($compare)) { throw new Exception("Compare must be callable!"); diff --git a/src/Arrayable/ArrSticky.php b/src/Arrayable/ArrSticky.php index c5599da..c419c99 100644 --- a/src/Arrayable/ArrSticky.php +++ b/src/Arrayable/ArrSticky.php @@ -11,7 +11,6 @@ */ final class ArrSticky extends ArrEnvelope { - /** * @var array $cache */ @@ -26,7 +25,7 @@ public function __construct(Arrayable $arr) { parent::__construct( new ArrFromCallback( - fn () => $this->cache[0] ??= $arr->value() + fn () => $this->cache[0] ??= $arr->asArray() ) ); } diff --git a/src/Arrayable/ArrUnique.php b/src/Arrayable/ArrUnique.php index f697c26..16403c9 100644 --- a/src/Arrayable/ArrUnique.php +++ b/src/Arrayable/ArrUnique.php @@ -6,14 +6,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Arrayable; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Unique array. */ final class ArrUnique extends ArrEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -24,7 +24,7 @@ public function __construct($arr) { parent::__construct( new ArrFromCallback( - fn () => array_unique((array)$this->scalarCast($arr)) + fn () => array_unique((array) $this->mixedCast($arr)) ) ); } diff --git a/src/Arrayable/ArrValues.php b/src/Arrayable/ArrValues.php index db2c1f2..1ead0b0 100644 --- a/src/Arrayable/ArrValues.php +++ b/src/Arrayable/ArrValues.php @@ -6,14 +6,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Arrayable; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Array values */ final class ArrValues extends ArrEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -24,7 +24,7 @@ public function __construct($arr) { parent::__construct( new ArrFromCallback( - fn () => array_values((array) $this->scalarCast($arr)) + fn () => array_values((array) $this->mixedCast($arr)) ) ); } diff --git a/src/Arrayable/ArrayableOf.php b/src/Arrayable/ArrayableOf.php index a758001..519d282 100644 --- a/src/Arrayable/ArrayableOf.php +++ b/src/Arrayable/ArrayableOf.php @@ -44,7 +44,7 @@ public function __construct($arr) * @return array * @throws Exception */ - public function value(): array + public function asArray(): array { if ($this->origin instanceof Scalar) { if (!is_array($res = $this->origin->value())) { diff --git a/src/Arrayable/CountArrayable.php b/src/Arrayable/CountArrayable.php index ab0c26a..3130d6f 100644 --- a/src/Arrayable/CountArrayable.php +++ b/src/Arrayable/CountArrayable.php @@ -16,6 +16,6 @@ trait CountArrayable */ public function count(): int { - return count($this->value()); + return count($this->asArray()); } } diff --git a/src/Arrayable/HasArrayableIterator.php b/src/Arrayable/HasArrayableIterator.php index d6d3467..9811fe6 100644 --- a/src/Arrayable/HasArrayableIterator.php +++ b/src/Arrayable/HasArrayableIterator.php @@ -18,6 +18,6 @@ trait HasArrayableIterator */ public function getIterator(): Traversable { - return new ArrayIterator($this->value()); + return new ArrayIterator($this->asArray()); } } diff --git a/src/Boolean.php b/src/Boolean.php index e315e36..57dce58 100644 --- a/src/Boolean.php +++ b/src/Boolean.php @@ -9,11 +9,11 @@ /** * Boolean. */ -interface Boolean extends Scalar +interface Boolean { /** * @return bool * @throws Exception */ - public function value(): bool; + public function asBool(): bool; } diff --git a/src/Boolean/BoolEnvelope.php b/src/Boolean/BoolEnvelope.php index a2ddbaf..1bc41c4 100644 --- a/src/Boolean/BoolEnvelope.php +++ b/src/Boolean/BoolEnvelope.php @@ -31,8 +31,8 @@ public function __construct(\Maxonfjvipon\Elegant_Elephant\Boolean $origin) * @return bool * @throws Exception */ - public function value(): bool + public function asBool(): bool { - return $this->origin->value(); + return $this->origin->asBool(); } } diff --git a/src/Boolean/BooleanOf.php b/src/Boolean/BooleanOf.php index 24055e2..2573dcf 100644 --- a/src/Boolean/BooleanOf.php +++ b/src/Boolean/BooleanOf.php @@ -33,7 +33,7 @@ public function __construct($bool) * @return bool * @throws Exception */ - public function value(): bool + public function asBool(): bool { if ($this->origin instanceof Scalar) { if (!is_bool($res = $this->origin->value())) { diff --git a/src/Boolean/Conjunction.php b/src/Boolean/Conjunction.php index 85dab79..feb14ea 100644 --- a/src/Boolean/Conjunction.php +++ b/src/Boolean/Conjunction.php @@ -6,14 +6,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Boolean; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Logical AND. */ final class Conjunction implements Boolean { - use CastScalar; + use CastMixed; /** * @var array $args @@ -34,11 +34,11 @@ public function __construct(...$args) * @return bool * @throws Exception */ - public function value(): bool + public function asBool(): bool { /** @var bool|Boolean $arg */ foreach ($this->args as $arg) { - if (!(bool) $this->scalarCast($arg)) { + if (!(bool) $this->mixedCast($arg)) { return false; } } diff --git a/src/Boolean/Disjunction.php b/src/Boolean/Disjunction.php index 93828f1..d14939c 100644 --- a/src/Boolean/Disjunction.php +++ b/src/Boolean/Disjunction.php @@ -6,14 +6,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Boolean; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Logical OR. */ final class Disjunction implements Boolean { - use CastScalar; + use CastMixed; /** * @var array $args @@ -34,10 +34,10 @@ public function __construct(...$args) * @return bool * @throws Exception */ - public function value(): bool + public function asBool(): bool { foreach ($this->args as $arg) { - if ($this->scalarCast($arg)) { + if ($this->mixedCast($arg)) { return true; } } diff --git a/src/Boolean/EqualityOf.php b/src/Boolean/EqualityOf.php index cc0b9e2..1171072 100644 --- a/src/Boolean/EqualityOf.php +++ b/src/Boolean/EqualityOf.php @@ -6,14 +6,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Boolean; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Equality of. */ final class EqualityOf implements Boolean { - use CastScalar; + use CastMixed; /** * @var mixed $first @@ -42,8 +42,8 @@ public function __construct($arg1, $arg2) * @return bool * @throws Exception */ - public function value(): bool + public function asBool(): bool { - return $this->scalarCast($this->first) === $this->scalarCast($this->second); + return $this->mixedCast($this->first) === $this->mixedCast($this->second); } } diff --git a/src/Boolean/InArray.php b/src/Boolean/InArray.php index bd13463..78bdb34 100644 --- a/src/Boolean/InArray.php +++ b/src/Boolean/InArray.php @@ -7,14 +7,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Arrayable; use Maxonfjvipon\Elegant_Elephant\Boolean; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * In array. */ final class InArray implements Boolean { - use CastScalar; + use CastMixed; /** * @var array|Arrayable $arr @@ -49,8 +49,8 @@ public function __construct($arr, $needle, bool $strict = false) * @return bool * @throws Exception */ - public function value(): bool + public function asBool(): bool { - return in_array($this->needle, (array) $this->scalarCast($this->arr), $this->strict); + return in_array($this->needle, (array) $this->mixedCast($this->arr), $this->strict); } } diff --git a/src/Boolean/KeyExists.php b/src/Boolean/KeyExists.php index ed368d8..2e9e5cb 100644 --- a/src/Boolean/KeyExists.php +++ b/src/Boolean/KeyExists.php @@ -8,7 +8,7 @@ use Maxonfjvipon\Elegant_Elephant\Arrayable; use Maxonfjvipon\Elegant_Elephant\Boolean; use Maxonfjvipon\Elegant_Elephant\Number; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use Maxonfjvipon\Elegant_Elephant\Text; /** @@ -16,7 +16,7 @@ */ final class KeyExists implements Boolean { - use CastScalar; + use CastMixed; /** * @var int|string|Number|Text $key @@ -44,8 +44,8 @@ public function __construct($key, $arr) * @return bool * @throws Exception */ - public function value(): bool + public function asBool(): bool { - return array_key_exists($this->scalarCast($this->key), (array) $this->scalarCast($this->arr)); + return array_key_exists($this->mixedCast($this->key), (array) $this->mixedCast($this->arr)); } } diff --git a/src/Boolean/Not.php b/src/Boolean/Not.php index d678331..d61b6db 100644 --- a/src/Boolean/Not.php +++ b/src/Boolean/Not.php @@ -6,14 +6,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Boolean; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Not. */ final class Not implements Boolean { - use CastScalar; + use CastMixed; /** * @var bool|Boolean $origin @@ -34,8 +34,8 @@ public function __construct($origin) * @return bool * @throws Exception */ - public function value(): bool + public function asBool(): bool { - return !(bool) $this->scalarCast($this->origin); + return !(bool) $this->mixedCast($this->origin); } } diff --git a/src/Boolean/PregMatch.php b/src/Boolean/PregMatch.php index b22d998..9180829 100644 --- a/src/Boolean/PregMatch.php +++ b/src/Boolean/PregMatch.php @@ -6,7 +6,7 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Boolean; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use Maxonfjvipon\Elegant_Elephant\Text; use Maxonfjvipon\Elegant_Elephant\Text\CastText; @@ -15,7 +15,7 @@ */ final class PregMatch implements Boolean { - use CastScalar; + use CastMixed; /** * @var string|Text $pattern @@ -43,8 +43,8 @@ public function __construct($pattern, $subject) * @return bool * @throws Exception */ - public function value(): bool + public function asBool(): bool { - return (bool) preg_match(...$this->scalarsCast($this->pattern, $this->subject)); + return (bool) preg_match(...$this->mixedArrCast($this->pattern, $this->subject)); } } diff --git a/src/Boolean/StrContains.php b/src/Boolean/StrContains.php index 50f9fd8..491a7eb 100644 --- a/src/Boolean/StrContains.php +++ b/src/Boolean/StrContains.php @@ -6,7 +6,7 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Boolean; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use Maxonfjvipon\Elegant_Elephant\Text; /** @@ -14,7 +14,7 @@ */ final class StrContains implements Boolean { - use CastScalar; + use CastMixed; /** * @var string|Text $haystack @@ -42,8 +42,8 @@ public function __construct($haystack, $needle) * @return bool * @throws Exception */ - public function value(): bool + public function asBool(): bool { - return strpos(...$this->scalarsCast($this->haystack, $this->needle)) !== false; + return strpos(...$this->mixedArrCast($this->haystack, $this->needle)) !== false; } } diff --git a/src/Number.php b/src/Number.php index 01f6d4d..bffc981 100644 --- a/src/Number.php +++ b/src/Number.php @@ -10,11 +10,11 @@ /** * Number. */ -interface Number extends Scalar +interface Number { /** * @return float|int * @throws Exception */ - public function value(); + public function asNumber(); } diff --git a/src/Number/ArraySum.php b/src/Number/ArraySum.php index abda1e1..1cf4ed4 100644 --- a/src/Number/ArraySum.php +++ b/src/Number/ArraySum.php @@ -7,14 +7,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Arrayable; use Maxonfjvipon\Elegant_Elephant\Number; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Sum of array elements. */ final class ArraySum implements Number { - use CastScalar; + use CastMixed; /** * @var array|Arrayable $container @@ -35,8 +35,8 @@ public function __construct($arr) * @return float|int * @throws Exception */ - public function value() + public function asNumber() { - return array_sum($this->scalarCast($this->container)); + return array_sum($this->mixedCast($this->container)); } } diff --git a/src/Number/LengthOf.php b/src/Number/LengthOf.php index 0c81e79..acc33ab 100644 --- a/src/Number/LengthOf.php +++ b/src/Number/LengthOf.php @@ -5,7 +5,7 @@ namespace Maxonfjvipon\Elegant_Elephant\Number; use Maxonfjvipon\Elegant_Elephant\Arrayable; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use Maxonfjvipon\Elegant_Elephant\Text; /** @@ -13,7 +13,7 @@ */ final class LengthOf extends NumEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -25,8 +25,8 @@ public function __construct($arg) parent::__construct( new NumTernary( is_array($arg) || $arg instanceof Arrayable, - fn () => count((array) $this->scalarCast($arg)), - fn () => strlen((string) $this->scalarCast($arg)) + fn () => count((array) $this->mixedCast($arg)), + fn () => strlen((string) $this->mixedCast($arg)) ) ); } diff --git a/src/Number/MaxOf.php b/src/Number/MaxOf.php index 5bca8ed..ca302af 100644 --- a/src/Number/MaxOf.php +++ b/src/Number/MaxOf.php @@ -6,14 +6,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Number; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Max. */ final class MaxOf implements Number { - use CastScalar; + use CastMixed; /** * @var array $items @@ -34,9 +34,9 @@ public function __construct(...$items) * @return float|int * @throws Exception */ - public function value() + public function asNumber() { - $max = max(...$this->scalarsCast(...$this->items)); + $max = max(...$this->mixedArrCast(...$this->items)); if (!is_numeric($max)) { throw new Exception("Max can work with numbers only!"); diff --git a/src/Number/MinOf.php b/src/Number/MinOf.php index afe21f5..c25ead9 100644 --- a/src/Number/MinOf.php +++ b/src/Number/MinOf.php @@ -6,14 +6,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Number; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Min. */ final class MinOf implements Number { - use CastScalar; + use CastMixed; /** * @var array $items @@ -34,9 +34,9 @@ public function __construct(...$items) * @return float|int * @throws Exception */ - public function value() + public function asNumber() { - $min = min(...$this->scalarsCast(...$this->items)); + $min = min(...$this->mixedArrCast(...$this->items)); if (!is_numeric($min)) { throw new Exception("Min can work with numbers only!"); diff --git a/src/Number/NumEnvelope.php b/src/Number/NumEnvelope.php index 3e3d32c..b9bbaa0 100644 --- a/src/Number/NumEnvelope.php +++ b/src/Number/NumEnvelope.php @@ -29,8 +29,8 @@ public function __construct(Number $origin) * @return float|int * @throws Exception */ - public function value() + public function asNumber() { - return $this->origin->value(); + return $this->origin->asNumber(); } } diff --git a/src/Number/NumSticky.php b/src/Number/NumSticky.php index 2c8de5d..b65e3e4 100644 --- a/src/Number/NumSticky.php +++ b/src/Number/NumSticky.php @@ -47,8 +47,8 @@ public function __construct(Number $num) * @return float|int * @throws Exception */ - public function value() + public function asNumber() { - return $this->cache[0] ??= $this->origin->value(); + return $this->cache[0] ??= $this->origin->asNumber(); } } diff --git a/src/Number/NumberOf.php b/src/Number/NumberOf.php index 8850b86..ba51fff 100644 --- a/src/Number/NumberOf.php +++ b/src/Number/NumberOf.php @@ -7,14 +7,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Scalar; use Maxonfjvipon\Elegant_Elephant\Number; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Numerable of. */ final class NumberOf implements Number { - use CastScalar; + use CastMixed; /** * @var float|int|Scalar $origin @@ -35,7 +35,7 @@ public function __construct($num) * @return float|int * @throws Exception */ - public function value() + public function asNumber() { if ($this->origin instanceof Scalar) { $res = $this->origin->value(); diff --git a/src/Number/Rounded.php b/src/Number/Rounded.php index 733617a..88887f2 100644 --- a/src/Number/Rounded.php +++ b/src/Number/Rounded.php @@ -6,14 +6,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Number; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Rounded number. */ final class Rounded implements Number { - use CastScalar; + use CastMixed; /** * @var float|int|Number $origin @@ -41,8 +41,8 @@ public function __construct($num, int $precision = 0) * @return float * @throws Exception */ - public function value(): float + public function asNumber(): float { - return round($this->scalarCast($this->origin), $this->precision); + return round($this->mixedCast($this->origin), $this->precision); } } diff --git a/src/Number/SumOf.php b/src/Number/SumOf.php index 6477d59..badd1b5 100644 --- a/src/Number/SumOf.php +++ b/src/Number/SumOf.php @@ -6,14 +6,14 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Number; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; /** * Sum of items. */ final class SumOf extends NumEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -24,7 +24,7 @@ final class SumOf extends NumEnvelope public function __construct(...$items) { parent::__construct( - new ArraySum($this->scalarsCast(...$items)) + new ArraySum($this->mixedArrCast(...$items)) ); } } diff --git a/src/Scalar/AtKey.php b/src/Scalar/AtKey.php index 05276f1..4acf660 100644 --- a/src/Scalar/AtKey.php +++ b/src/Scalar/AtKey.php @@ -15,7 +15,7 @@ */ final class AtKey implements Scalar { - use CastScalar; + use CastMixed; /** * @var array|Arrayable $arr @@ -45,6 +45,6 @@ public function __construct($key, $arr) */ public function value() { - return $this->scalarCast($this->arr)[$this->scalarCast($this->key)]; + return $this->mixedCast($this->arr)[$this->mixedCast($this->key)]; } } diff --git a/src/Scalar/CastMixed.php b/src/Scalar/CastMixed.php new file mode 100644 index 0000000..4d27b6d --- /dev/null +++ b/src/Scalar/CastMixed.php @@ -0,0 +1,76 @@ +value(); + } + + if ($mixed instanceof Arrayable) { + return $mixed->asArray(); + } + + if ($mixed instanceof Text) { + return $mixed->asString(); + } + + if ($mixed instanceof Number) { + return $mixed->asNumber(); + } + + if ($mixed instanceof Boolean) { + return $mixed->asBool(); + } + + return $mixed; + } + + /** + * @param mixed $mixed + * @return mixed + * @throws Exception + */ + private function mixedOrCallableCast($mixed) + { + if (is_callable($mixed)) { + return $this->mixedOrCallableCast(call_user_func($mixed)); + } + + return $this->mixedCast($mixed); + } + + + /** + * @param mixed ...$mixedArr + * @return array + * @throws Exception + */ + private function mixedArrCast(...$mixedArr): array + { + return array_map( + fn ($mixed) => $this->mixedCast($mixed), + $mixedArr + ); + } +} diff --git a/src/Scalar/CastScalar.php b/src/Scalar/CastScalar.php deleted file mode 100644 index 410c782..0000000 --- a/src/Scalar/CastScalar.php +++ /dev/null @@ -1,56 +0,0 @@ -value(); - } - - return $mixed; - } - - /** - * @param mixed $scalar - * @return mixed - * @throws Exception - */ - private function scalarOrCallableCast($scalar) - { - if (is_callable($scalar)) { - return $this->scalarOrCallableCast(call_user_func($scalar)); - } - - return $this->scalarCast($scalar); - } - - - /** - * @param mixed ...$scalars - * @return array - * @throws Exception - */ - private function scalarsCast(...$scalars): array - { - return array_map( - fn ($scalar) => $this->scalarCast($scalar), - $scalars - ); - } -} diff --git a/src/Scalar/FirstOf.php b/src/Scalar/FirstOf.php index 36c5226..055321e 100644 --- a/src/Scalar/FirstOf.php +++ b/src/Scalar/FirstOf.php @@ -7,7 +7,6 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Arrayable; use Maxonfjvipon\Elegant_Elephant\Scalar; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; use Maxonfjvipon\Elegant_Elephant\Text; /** @@ -15,7 +14,7 @@ */ final class FirstOf implements Scalar { - use CastScalar; + use CastMixed; /** * @var string|array|Text|Arrayable $container @@ -38,6 +37,6 @@ public function __construct($container) */ public function value() { - return $this->scalarCast($this->container)[0]; + return $this->mixedCast($this->container)[0]; } } diff --git a/src/Scalar/LastOf.php b/src/Scalar/LastOf.php index 27d6680..b03b581 100644 --- a/src/Scalar/LastOf.php +++ b/src/Scalar/LastOf.php @@ -12,7 +12,7 @@ */ final class LastOf extends SclEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -25,11 +25,11 @@ public function __construct($container) new SclTernary( is_array($container) || $container instanceof Arrayable, function () use ($container) { - $arr = (array) $this->scalarCast($container); + $arr = (array) $this->mixedCast($container); return $arr[count($arr) - 1]; }, - fn () => substr($this->scalarCast($container), -1) + fn () => substr($this->mixedCast($container), -1) ) ); } diff --git a/src/Scalar/SclTernary.php b/src/Scalar/SclTernary.php index 86a3450..2b400ab 100644 --- a/src/Scalar/SclTernary.php +++ b/src/Scalar/SclTernary.php @@ -11,7 +11,7 @@ */ final class SclTernary implements Scalar { - use CastScalar; + use CastMixed; /** * @var bool|Boolean $condition @@ -48,8 +48,8 @@ public function __construct($condition, $first, $second) */ public function value() { - return $this->scalarCast($this->condition) - ? $this->scalarOrCallableCast($this->first) - : $this->scalarOrCallableCast($this->second); + return $this->mixedCast($this->condition) + ? $this->mixedOrCallableCast($this->first) + : $this->mixedOrCallableCast($this->second); } } diff --git a/src/Text.php b/src/Text.php index c7c8fce..a84b428 100644 --- a/src/Text.php +++ b/src/Text.php @@ -10,11 +10,11 @@ /** * Text. */ -interface Text extends Scalar, Stringable +interface Text extends Stringable { /** * @return string * @throws Exception */ - public function value(): string; + public function asString(): string; } diff --git a/src/Text/StringableText.php b/src/Text/StringableText.php index 394d9a2..9c1e086 100644 --- a/src/Text/StringableText.php +++ b/src/Text/StringableText.php @@ -16,6 +16,6 @@ trait StringableText */ public function __toString() { - return $this->value(); + return $this->asString(); } } diff --git a/src/Text/TextOf.php b/src/Text/TextOf.php index 978cf33..6da0fd2 100644 --- a/src/Text/TextOf.php +++ b/src/Text/TextOf.php @@ -6,7 +6,7 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Scalar; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use Maxonfjvipon\Elegant_Elephant\Text; /** @@ -14,7 +14,7 @@ */ final class TextOf implements Text { - use CastScalar; + use CastMixed; use StringableText; /** @@ -36,7 +36,7 @@ public function __construct($text) * @return string * @throws Exception */ - public function value(): string + public function asString(): string { if ($this->origin instanceof Scalar) { if (!is_string($res = $this->origin->value())) { diff --git a/src/Text/TxtEnvelope.php b/src/Text/TxtEnvelope.php index 25c201e..181710a 100644 --- a/src/Text/TxtEnvelope.php +++ b/src/Text/TxtEnvelope.php @@ -33,8 +33,8 @@ public function __construct(Text $text) * @return string * @throws Exception */ - public function value(): string + public function asString(): string { - return $this->origin->value(); + return $this->origin->asString(); } } diff --git a/src/Text/TxtFromCallback.php b/src/Text/TxtFromCallback.php index d6c8a0d..e5c5116 100644 --- a/src/Text/TxtFromCallback.php +++ b/src/Text/TxtFromCallback.php @@ -5,7 +5,7 @@ namespace Maxonfjvipon\Elegant_Elephant\Text; use Exception; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use Maxonfjvipon\Elegant_Elephant\Text; /** @@ -13,7 +13,7 @@ */ final class TxtFromCallback implements Text { - use CastScalar; + use CastMixed; use StringableText; /** @@ -35,8 +35,8 @@ public function __construct(callable $callback) * @return string * @throws Exception */ - public function value(): string + public function asString(): string { - return (string) $this->scalarOrCallableCast($this->callback); + return (string) $this->mixedOrCallableCast($this->callback); } } diff --git a/src/Text/TxtImploded.php b/src/Text/TxtImploded.php index 0519cc8..4b01a7d 100644 --- a/src/Text/TxtImploded.php +++ b/src/Text/TxtImploded.php @@ -5,7 +5,7 @@ namespace Maxonfjvipon\Elegant_Elephant\Text; use Exception; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use Maxonfjvipon\Elegant_Elephant\Text; /** @@ -13,7 +13,7 @@ */ final class TxtImploded extends TxtEnvelope { - use CastScalar; + use CastMixed; /** * @param string|Text ...$pieces @@ -45,8 +45,8 @@ public function __construct($separator, ...$pieces) parent::__construct( new TxtFromCallback( fn () => implode( - (string) $this->scalarCast($separator), - $this->scalarsCast(...$pieces) + (string) $this->mixedCast($separator), + $this->mixedArrCast(...$pieces) ) ) ); diff --git a/src/Text/TxtJsonEncoded.php b/src/Text/TxtJsonEncoded.php index f1adc1b..738ff77 100644 --- a/src/Text/TxtJsonEncoded.php +++ b/src/Text/TxtJsonEncoded.php @@ -6,7 +6,7 @@ use Maxonfjvipon\Elegant_Elephant\Boolean; use Maxonfjvipon\Elegant_Elephant\Number; use Maxonfjvipon\Elegant_Elephant\Scalar; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use Maxonfjvipon\Elegant_Elephant\Text; /** @@ -14,18 +14,18 @@ */ final class TxtJsonEncoded extends TxtEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. * - * @param string|int|float|array|bool|Text|Number|Arrayable|Boolean|Scalar $value + * @param string|int|float|array|bool|Text|Number|Arrayable|Boolean|Scalar $value */ public function __construct($value) { parent::__construct( new TxtFromCallback( - fn () => (string) json_encode($this->scalarCast($value)) + fn () => (string) json_encode($this->mixedCast($value)) ) ); } diff --git a/src/Text/TxtLowered.php b/src/Text/TxtLowered.php index 58dd1e8..07ddaca 100644 --- a/src/Text/TxtLowered.php +++ b/src/Text/TxtLowered.php @@ -4,7 +4,7 @@ namespace Maxonfjvipon\Elegant_Elephant\Text; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use Maxonfjvipon\Elegant_Elephant\Text; /** @@ -12,7 +12,7 @@ */ final class TxtLowered extends TxtEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -23,7 +23,7 @@ public function __construct($text) { parent::__construct( new TxtFromCallback( - fn () => strtolower((string) $this->scalarCast($text)) + fn () => strtolower((string) $this->mixedCast($text)) ) ); } diff --git a/src/Text/TxtLtrimmed.php b/src/Text/TxtLtrimmed.php index 2aabb9a..351ba39 100644 --- a/src/Text/TxtLtrimmed.php +++ b/src/Text/TxtLtrimmed.php @@ -4,7 +4,7 @@ namespace Maxonfjvipon\Elegant_Elephant\Text; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use Maxonfjvipon\Elegant_Elephant\Text; /** @@ -12,7 +12,7 @@ */ final class TxtLtrimmed extends TxtEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -23,7 +23,7 @@ public function __construct($text) { parent::__construct( new TxtFromCallback( - fn () => ltrim((string) $this->scalarCast($text)) + fn () => ltrim((string) $this->mixedCast($text)) ) ); } diff --git a/src/Text/TxtPregReplaced.php b/src/Text/TxtPregReplaced.php index 0cf0a89..eb0a9b0 100644 --- a/src/Text/TxtPregReplaced.php +++ b/src/Text/TxtPregReplaced.php @@ -4,7 +4,7 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Arrayable; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use Maxonfjvipon\Elegant_Elephant\Text; /** @@ -12,7 +12,7 @@ */ final class TxtPregReplaced extends TxtEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -26,9 +26,9 @@ public function __construct($pattern, $replacement, $subject) parent::__construct( new TxtFromCallback( fn () => preg_replace( - $this->scalarCast($pattern), - $this->scalarCast($replacement), - $this->scalarCast($subject) + $this->mixedCast($pattern), + $this->mixedCast($replacement), + $this->mixedCast($subject) ) ) ); diff --git a/src/Text/TxtReplaced.php b/src/Text/TxtReplaced.php index 03d37e1..2f6c6d3 100644 --- a/src/Text/TxtReplaced.php +++ b/src/Text/TxtReplaced.php @@ -5,7 +5,7 @@ namespace Maxonfjvipon\Elegant_Elephant\Text; use Exception; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use Maxonfjvipon\Elegant_Elephant\Text; /** @@ -13,7 +13,7 @@ */ final class TxtReplaced extends TxtEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -27,9 +27,9 @@ public function __construct($search, $replace, $subject) parent::__construct( new TxtFromCallback( fn () => str_replace( - (string) $this->scalarCast($search), - (string) $this->scalarCast($replace), - (string) $this->scalarCast($subject) + (string) $this->mixedCast($search), + (string) $this->mixedCast($replace), + (string) $this->mixedCast($subject) ) ) ); diff --git a/src/Text/TxtRtrimmed.php b/src/Text/TxtRtrimmed.php index ffce6d8..f1485f9 100644 --- a/src/Text/TxtRtrimmed.php +++ b/src/Text/TxtRtrimmed.php @@ -5,7 +5,7 @@ namespace Maxonfjvipon\Elegant_Elephant\Text; use Exception; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use Maxonfjvipon\Elegant_Elephant\Text; /** @@ -13,7 +13,7 @@ */ final class TxtRtrimmed extends TxtEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -24,7 +24,7 @@ public function __construct($text) { parent::__construct( new TxtFromCallback( - fn () => rtrim((string) $this->scalarCast($text)) + fn () => rtrim((string) $this->mixedCast($text)) ) ); } diff --git a/src/Text/TxtSticky.php b/src/Text/TxtSticky.php index 4a7552f..472be03 100644 --- a/src/Text/TxtSticky.php +++ b/src/Text/TxtSticky.php @@ -26,7 +26,7 @@ public function __construct(Text $text) { parent::__construct( new TxtFromCallback( - fn () => $this->cache[0] ??= $text->value() + fn () => $this->cache[0] ??= $text->asString() ) ); } diff --git a/src/Text/TxtSubstr.php b/src/Text/TxtSubstr.php index 48f877d..358fe35 100644 --- a/src/Text/TxtSubstr.php +++ b/src/Text/TxtSubstr.php @@ -5,7 +5,7 @@ namespace Maxonfjvipon\Elegant_Elephant\Text; use Exception; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use Maxonfjvipon\Elegant_Elephant\Text; /** @@ -13,7 +13,7 @@ */ final class TxtSubstr extends TxtEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -27,8 +27,8 @@ public function __construct($text, int $offset, ?int $length = null) parent::__construct( new TxtFromCallback( fn () => !!$length - ? substr((string) $this->scalarCast($text), $offset, $length) - : substr((string) $this->scalarCast($text), $offset) + ? substr((string) $this->mixedCast($text), $offset, $length) + : substr((string) $this->mixedCast($text), $offset) ) ); } diff --git a/src/Text/TxtTrimmed.php b/src/Text/TxtTrimmed.php index 916c614..6d3ff88 100644 --- a/src/Text/TxtTrimmed.php +++ b/src/Text/TxtTrimmed.php @@ -5,7 +5,7 @@ namespace Maxonfjvipon\Elegant_Elephant\Text; use Exception; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use Maxonfjvipon\Elegant_Elephant\Text; /** @@ -13,7 +13,7 @@ */ final class TxtTrimmed extends TxtEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -24,7 +24,7 @@ public function __construct($text) { parent::__construct( new TxtFromCallback( - fn () => trim((string) $this->scalarCast($text)) + fn () => trim((string) $this->mixedCast($text)) ) ); } diff --git a/src/Text/TxtUpper.php b/src/Text/TxtUpper.php index bdc97b2..212b56c 100644 --- a/src/Text/TxtUpper.php +++ b/src/Text/TxtUpper.php @@ -5,7 +5,7 @@ namespace Maxonfjvipon\Elegant_Elephant\Text; use Exception; -use Maxonfjvipon\Elegant_Elephant\Scalar\CastScalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use Maxonfjvipon\Elegant_Elephant\Text; /** @@ -13,7 +13,7 @@ */ final class TxtUpper extends TxtEnvelope { - use CastScalar; + use CastMixed; /** * Ctor. @@ -24,7 +24,7 @@ public function __construct($text) { parent::__construct( new TxtFromCallback( - fn () => strtoupper((string) $this->scalarCast($text)) + fn () => strtoupper((string) $this->mixedCast($text)) ) ); } diff --git a/tests/Arrayable/ArrCombinedTest.php b/tests/Arrayable/ArrCombinedTest.php index 9b556f3..674b821 100644 --- a/tests/Arrayable/ArrCombinedTest.php +++ b/tests/Arrayable/ArrCombinedTest.php @@ -49,6 +49,6 @@ public function combinedWithArrayAsKey(): void { $this->expectException(\Exception::class); $this->expectExceptionMessage("Array can't be the key of array"); - (new ArrCombined([['key']], [['value']]))->value(); + (new ArrCombined([['key']], [['value']]))->asArray(); } } diff --git a/tests/Arrayable/ArrFromCallbackTest.php b/tests/Arrayable/ArrFromCallbackTest.php index 292d091..7d2db25 100644 --- a/tests/Arrayable/ArrFromCallbackTest.php +++ b/tests/Arrayable/ArrFromCallbackTest.php @@ -43,6 +43,6 @@ public function arrayableFromCallback(): void public function withError(): void { $this->expectExceptionMessage("Callback must return an array or Arrayable!"); - (new ArrFromCallback(fn () => "Hello world"))->value(); + (new ArrFromCallback(fn () => "Hello world"))->asArray(); } } diff --git a/tests/Arrayable/ArrKeysTest.php b/tests/Arrayable/ArrKeysTest.php index a13e9e3..7b51e2a 100644 --- a/tests/Arrayable/ArrKeysTest.php +++ b/tests/Arrayable/ArrKeysTest.php @@ -9,8 +9,8 @@ final class ArrKeysTest extends TestCase { - const EXPECTED = [1, 2, 3, 4]; - const GIVEN = [ + public const EXPECTED = [1, 2, 3, 4]; + public const GIVEN = [ 1 => "value1", 2 => "value2", 3 => "value3", diff --git a/tests/Arrayable/ArrMappedTest.php b/tests/Arrayable/ArrMappedTest.php index 480ee68..74ccb62 100644 --- a/tests/Arrayable/ArrMappedTest.php +++ b/tests/Arrayable/ArrMappedTest.php @@ -11,8 +11,8 @@ final class ArrMappedTest extends TestCase { - const GIVEN = [1, 2, 3, 4, 5]; - const EXPECTED = [1, 4, 9, 16, 25]; + public const GIVEN = [1, 2, 3, 4, 5]; + public const EXPECTED = [1, 4, 9, 16, 25]; /** * @test @@ -23,7 +23,7 @@ public function mapped(): void $this->assertScalarThat( new ArrMapped( self::GIVEN, - fn($value) => $value * $value + fn ($value) => $value * $value ), new IsEqual(self::EXPECTED) ); @@ -40,7 +40,7 @@ public function mappedCast(): void new ArrCast( new ArrMapped( self::GIVEN, - fn($num) => new TextOf($num . "L"), + fn ($num) => new TextOf($num . "L"), ) ), new IsEqual([ diff --git a/tests/Arrayable/ArrReversedTest.php b/tests/Arrayable/ArrReversedTest.php index d9bc310..209210c 100644 --- a/tests/Arrayable/ArrReversedTest.php +++ b/tests/Arrayable/ArrReversedTest.php @@ -9,7 +9,6 @@ use PHPUnit\Framework\Assert; use PHPUnit\Framework\Constraint\IsEqual; - /** * Array reversed test */ diff --git a/tests/Arrayable/ArrSortedTest.php b/tests/Arrayable/ArrSortedTest.php index ac9bde7..33889cc 100644 --- a/tests/Arrayable/ArrSortedTest.php +++ b/tests/Arrayable/ArrSortedTest.php @@ -10,8 +10,8 @@ final class ArrSortedTest extends TestCase { - const GIVEN = [2, 4, 1, 3]; - const EXPECTED = [1, 2, 3, 4]; + public const GIVEN = [2, 4, 1, 3]; + public const EXPECTED = [1, 2, 3, 4]; /** * @test diff --git a/tests/Arrayable/ArrStickyTest.php b/tests/Arrayable/ArrStickyTest.php index 261a913..fab34d5 100644 --- a/tests/Arrayable/ArrStickyTest.php +++ b/tests/Arrayable/ArrStickyTest.php @@ -20,7 +20,7 @@ public function cacheWorks(): void { $num = 2; $arr = new ArrSticky( - new class($num) implements Arrayable { + new class ($num) implements Arrayable { use CountArrayable; use HasArrayableIterator; @@ -40,14 +40,14 @@ public function __construct(int $num) /** * @return array */ - public function value(): array + public function asArray(): array { $this->num = $this->num + 2; // BIG calculations :) return [$this->num]; } } ); - $arr->value(); + $arr->asArray(); $this->assertScalarThat( $arr, new IsEqual([4]) // no recalculation diff --git a/tests/Boolean/KeyExistsTest.php b/tests/Boolean/KeyExistsTest.php index ae00462..5059e6a 100644 --- a/tests/Boolean/KeyExistsTest.php +++ b/tests/Boolean/KeyExistsTest.php @@ -10,7 +10,6 @@ use Maxonfjvipon\Elegant_Elephant\Text\TextOf; use PHPUnit\Framework\Constraint\IsTrue; - use function PHPUnit\Framework\assertEquals; final class KeyExistsTest extends TestCase diff --git a/tests/Number/NumStickyTest.php b/tests/Number/NumStickyTest.php index 61a265d..88a74c1 100644 --- a/tests/Number/NumStickyTest.php +++ b/tests/Number/NumStickyTest.php @@ -35,14 +35,14 @@ public function __construct(int $num) /** * @return int */ - public function value(): int + public function asNumber(): int { $this->num += 2; return $this->num; } } ); - $cached->value(); + $cached->asNumber(); $this->assertScalarThat( $cached, diff --git a/tests/TestCase.php b/tests/TestCase.php index 7cb09e5..7a1049c 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,7 +5,7 @@ namespace Maxonfjvipon\Elegant_Elephant\Tests; use Exception; -use Maxonfjvipon\Elegant_Elephant\Scalar; +use Maxonfjvipon\Elegant_Elephant\Scalar\CastMixed; use PHPUnit\Framework\Constraint\Constraint; use PHPUnit\Framework\TestCase as BaseTestCase; @@ -14,17 +14,19 @@ */ class TestCase extends BaseTestCase { + use CastMixed; + /** - * @param Scalar $scalar + * @param mixed $mixed * @param Constraint $constraint * @param string $message * @return void * @throws Exception */ - protected function assertScalarThat(Scalar $scalar, Constraint $constraint, string $message = ''): void + protected function assertScalarThat($mixed, Constraint $constraint, string $message = ''): void { $this->assertThat( - $scalar->value(), + $this->mixedCast($mixed), $constraint, $message ); diff --git a/tests/Text/TxtFromCallbackTest.php b/tests/Text/TxtFromCallbackTest.php index 9ed3c52..d80e543 100644 --- a/tests/Text/TxtFromCallbackTest.php +++ b/tests/Text/TxtFromCallbackTest.php @@ -44,6 +44,6 @@ public function textFromCallback(): void public function withError(): void { $this->expectError(); - (new TxtFromCallback(fn () => ["Hello world"]))->value(); + (new TxtFromCallback(fn () => ["Hello world"]))->asString(); } } diff --git a/tests/Text/TxtPregReplacedTest.php b/tests/Text/TxtPregReplacedTest.php index 5d512d5..457d71e 100644 --- a/tests/Text/TxtPregReplacedTest.php +++ b/tests/Text/TxtPregReplacedTest.php @@ -7,7 +7,6 @@ use Maxonfjvipon\Elegant_Elephant\Text\TxtPregReplaced; use PHPUnit\Framework\Constraint\IsEqual; - final class TxtPregReplacedTest extends TestCase { /** diff --git a/tests/Text/TxtStickyTest.php b/tests/Text/TxtStickyTest.php index d50c436..ebdb51d 100644 --- a/tests/Text/TxtStickyTest.php +++ b/tests/Text/TxtStickyTest.php @@ -9,7 +9,6 @@ use Maxonfjvipon\Elegant_Elephant\Text\TxtSticky; use PHPUnit\Framework\Constraint\IsEqual; - final class TxtStickyTest extends TestCase { /** @@ -20,7 +19,7 @@ public function doesNotChangeValue(): void { $str = "hello"; $cached = new TxtSticky( - new class($str) implements Text { + new class ($str) implements Text { use StringableText; /** @@ -41,14 +40,14 @@ public function __construct(string $text) /** * @return string */ - public function value(): string + public function asString(): string { $this->text = $this->text . " world!"; return $this->text; } } ); - $cached->value(); + $cached->asString(); $this->assertScalarThat( $cached, diff --git a/tests/Text/TxtUpperTest.php b/tests/Text/TxtUpperTest.php index 58882af..7eba805 100644 --- a/tests/Text/TxtUpperTest.php +++ b/tests/Text/TxtUpperTest.php @@ -10,8 +10,8 @@ final class TxtUpperTest extends TestCase { - const GIVEN = "foo"; - const UPPER = "FOO"; + public const GIVEN = "foo"; + public const UPPER = "FOO"; /** * @test