From 3441d3bdd1bfceac3f4db3fd9beeec495a51841b Mon Sep 17 00:00:00 2001 From: maximtrunnikov Date: Mon, 21 Feb 2022 23:50:16 +0300 Subject: [PATCH] Introduced Rounded #13 Introduced MaxOf #14 Introduced IsEmpty/IsNotEmpty #15 Minimized overloading in classes #12 --- src/Logical/IsEmpty.php | 53 ++++++++++++++++++++++++++ src/Logical/IsNotEmpty.php | 49 ++++++++++++++++++++++++ src/Numerable/Addition.php | 32 ++++++++-------- src/Numerable/Decremented.php | 12 +++--- src/Numerable/Incremented.php | 12 +++--- src/Numerable/MaxOf.php | 45 ++++++++++++++++++++++ src/Numerable/Multiplication.php | 29 ++++++-------- src/Numerable/NumerableOf.php | 21 ++++------- src/Numerable/NumerableOverloaded.php | 35 +++++++++++++++++ src/Numerable/Rounded.php | 54 +++++++++++++++++++++++++++ tests/Logical/IsEmptyTest.php | 39 +++++++++++++++++++ tests/Logical/IsNotEmptyTest.php | 39 +++++++++++++++++++ tests/Logical/MatchRegexTest.php | 1 - tests/Numerable/MaxOfTest.php | 26 +++++++++++++ tests/Numerable/RoundedTest.php | 26 +++++++++++++ 15 files changed, 413 insertions(+), 60 deletions(-) create mode 100644 src/Logical/IsEmpty.php create mode 100644 src/Logical/IsNotEmpty.php create mode 100644 src/Numerable/MaxOf.php create mode 100644 src/Numerable/NumerableOverloaded.php create mode 100644 src/Numerable/Rounded.php create mode 100644 tests/Logical/IsEmptyTest.php create mode 100644 tests/Logical/IsNotEmptyTest.php create mode 100644 tests/Numerable/MaxOfTest.php create mode 100644 tests/Numerable/RoundedTest.php diff --git a/src/Logical/IsEmpty.php b/src/Logical/IsEmpty.php new file mode 100644 index 0000000..e07bb50 --- /dev/null +++ b/src/Logical/IsEmpty.php @@ -0,0 +1,53 @@ +value = $value; + } + + /** + * @inheritDoc + */ + public function asBool(): bool + { + return $this->overload([$this->value], [[ + 'string' => fn(string $str) => $str === "", + 'array' => fn(array $arr) => (new EqualityOf(new LengthOf($arr), 0))->asBool(), + Text::class => fn(Text $txt) => (new EqualityOf($txt, ""))->asBool(), + Arrayable::class => fn(Arrayable $arr) => (new EqualityOf(new LengthOf($arr), 0))->asBool() + ]])[0]; + } +} \ No newline at end of file diff --git a/src/Logical/IsNotEmpty.php b/src/Logical/IsNotEmpty.php new file mode 100644 index 0000000..6526cec --- /dev/null +++ b/src/Logical/IsNotEmpty.php @@ -0,0 +1,49 @@ +value = $value; + } + + /** + * @inheritDoc + */ + public function asBool(): bool + { + return (new Negation( + new IsEmpty($this->value) + ))->asBool(); + } +} \ No newline at end of file diff --git a/src/Numerable/Addition.php b/src/Numerable/Addition.php index f846445..a4f6b5b 100644 --- a/src/Numerable/Addition.php +++ b/src/Numerable/Addition.php @@ -7,36 +7,39 @@ use Maxonfjvipon\OverloadedElephant\Overloadable; use TypeError; +/** + * Addition. + */ final class Addition implements Numerable { - use Overloadable; + use NumerableOverloaded; /** - * @var float|int|string|Numerable $addTo + * @var float|int|Numerable $addTo */ - private float|int|string|Numerable $addTo; + private float|int|Numerable $addTo; /** - * @var float|int|string|Numerable $toAdd + * @var float|int|Numerable $toAdd */ - private float|int|string|Numerable $toAdd; + private float|int|Numerable $toAdd; /** - * @param float|int|string|Numerable $addTo - * @param float|int|string|Numerable $toAdd + * @param float|int|Numerable $addTo + * @param float|int|Numerable $toAdd * @return Addition */ - public static function new(float|int|string|Numerable $addTo, float|int|string|Numerable $toAdd): Addition + public static function new(float|int|Numerable $addTo, float|int|Numerable $toAdd): Addition { return new self($addTo, $toAdd); } /** * Ctor. - * @param float|int|string|Numerable $addTo - * @param float|int|string|Numerable $toAdd + * @param float|int|Numerable $addTo + * @param float|int|Numerable $toAdd */ - public function __construct(float|int|string|Numerable $addTo, float|int|string|Numerable $toAdd) + public function __construct(float|int|Numerable $addTo, float|int|Numerable $toAdd) { $this->addTo = $addTo; $this->toAdd = $toAdd; @@ -47,12 +50,7 @@ public function __construct(float|int|string|Numerable $addTo, float|int|string| */ public function asNumber(): float|int { - $operands = $this->overload([$this->addTo, $this->toAdd], [[ - 'double', - 'integer', - 'string' => fn(string $str) => $str * 1, - Numerable::class => fn(Numerable $numerable) => $numerable->asNumber() - ]]); + $operands = $this->numerableOverloaded($this->addTo, $this->toAdd); return $operands[0] + $operands[1]; } } \ No newline at end of file diff --git a/src/Numerable/Decremented.php b/src/Numerable/Decremented.php index 2bc193a..b892669 100644 --- a/src/Numerable/Decremented.php +++ b/src/Numerable/Decremented.php @@ -11,24 +11,24 @@ final class Decremented implements Numerable { /** - * @var float|int|string|Numerable $origin + * @var float|int|Numerable $origin */ - private float|int|string|Numerable $origin; + private float|int|Numerable $origin; /** - * @param float|int|string|Numerable $origin + * @param float|int|Numerable $origin * @return Decremented */ - public static function new(float|int|string|Numerable $origin): Decremented + public static function new(float|int|Numerable $origin): Decremented { return new self($origin); } /** * Ctor. - * @param float|int|string|Numerable $origin + * @param float|int|Numerable $origin */ - public function __construct(float|int|string|Numerable $origin) + public function __construct(float|int|Numerable $origin) { $this->origin = $origin; } diff --git a/src/Numerable/Incremented.php b/src/Numerable/Incremented.php index a3581f2..164fa0d 100644 --- a/src/Numerable/Incremented.php +++ b/src/Numerable/Incremented.php @@ -7,24 +7,24 @@ final class Incremented implements Numerable { /** - * @var float|int|Numerable|string $origin + * @var float|int|Numerable $origin */ - private float|int|string|Numerable $origin; + private float|int|Numerable $origin; /** - * @param float|int|string|Numerable $origin + * @param float|int|Numerable $origin * @return Incremented */ - public static function new(float|int|string|Numerable $origin): Incremented + public static function new(float|int|Numerable $origin): Incremented { return new self($origin); } /** * Ctor. - * @param float|int|string|Numerable $origin + * @param float|int|Numerable $origin */ - public function __construct(float|int|string|Numerable $origin) + public function __construct(float|int|Numerable $origin) { $this->origin = $origin; } diff --git a/src/Numerable/MaxOf.php b/src/Numerable/MaxOf.php new file mode 100644 index 0000000..7d42b97 --- /dev/null +++ b/src/Numerable/MaxOf.php @@ -0,0 +1,45 @@ +items = $items; + } + + /** + * @inheritDoc + */ + public function asNumber(): float|int + { + return max(...$this->numerableOverloaded(...$this->items)); + } +} \ No newline at end of file diff --git a/src/Numerable/Multiplication.php b/src/Numerable/Multiplication.php index 4f4e7cf..84aa762 100644 --- a/src/Numerable/Multiplication.php +++ b/src/Numerable/Multiplication.php @@ -8,34 +8,34 @@ final class Multiplication implements Numerable { - use Overloadable; + use NumerableOverloaded; /** - * @var float|int|string|Numerable $arg1 + * @var float|int|Numerable $arg1 */ - private float|int|string|Numerable $arg1; + private float|int|Numerable $arg1; /** - * @var float|int|string|Numerable $arg2 + * @var float|int|Numerable $arg2 */ - private float|int|string|Numerable $arg2; + private float|int|Numerable $arg2; /** - * @param float|int|string|Numerable $arg1 - * @param float|int|string|Numerable $arg2 + * @param float|int|Numerable $arg1 + * @param float|int|Numerable $arg2 * @return Multiplication */ - public static function new(float|int|string|Numerable $arg1, float|int|string|Numerable $arg2): Multiplication + public static function new(float|int|Numerable $arg1, float|int|Numerable $arg2): Multiplication { return new self($arg1, $arg2); } /** * Ctor. - * @param float|int|string|Numerable $arg1 - * @param float|int|string|Numerable $arg2 + * @param float|int|Numerable $arg1 + * @param float|int|Numerable $arg2 */ - public function __construct(float|int|string|Numerable $arg1, float|int|string|Numerable $arg2) + public function __construct(float|int|Numerable $arg1, float|int|Numerable $arg2) { $this->arg1 = $arg1; $this->arg2 = $arg2; @@ -46,12 +46,7 @@ public function __construct(float|int|string|Numerable $arg1, float|int|string|N */ public function asNumber(): float|int { - $operands = $this->overload([$this->arg1, $this->arg2], [[ - 'double', - 'integer', - 'string' => fn(string $str) => $str * 1, - Numerable::class => fn(Numerable $numerable) => $numerable->asNumber() - ]]); + $operands = $this->numerableOverloaded($this->arg1, $this->arg2); return $operands[0] * $operands[1]; } } \ No newline at end of file diff --git a/src/Numerable/NumerableOf.php b/src/Numerable/NumerableOf.php index f477083..0921346 100644 --- a/src/Numerable/NumerableOf.php +++ b/src/Numerable/NumerableOf.php @@ -12,27 +12,27 @@ */ final class NumerableOf implements Numerable { - use Overloadable; + use NumerableOverloaded; /** - * @var float|int|Text|string $origin + * @var float|int $origin */ - private float|int|string|Text $origin; + private float|int $origin; /** - * @param float|int|Text|string $num + * @param float|int $num * @return NumerableOf */ - public static function new(float|int|Text|string $num): NumerableOf + public static function new(float|int $num): NumerableOf { return new self($num); } /** * Ctor. - * @param float|int|Text|string $num + * @param float|int $num */ - public function __construct(float|int|Text|string $num) + public function __construct(float|int $num) { $this->origin = $num; } @@ -42,11 +42,6 @@ public function __construct(float|int|Text|string $num) */ public function asNumber(): float|int { - return $this->overload([$this->origin], [[ - 'double', - 'integer', - 'string' => fn(string $num) => $num * 1, - Text::class => fn(Text $text) => $text->asString() * 1 - ]])[0]; + return $this->firstNumerableOverloaded($this->asNumber()); } } \ No newline at end of file diff --git a/src/Numerable/NumerableOverloaded.php b/src/Numerable/NumerableOverloaded.php new file mode 100644 index 0000000..dbc9a81 --- /dev/null +++ b/src/Numerable/NumerableOverloaded.php @@ -0,0 +1,35 @@ +overload($args, [[ + 'double', + 'integer', + Numerable::class => fn(Numerable $num) => $num->asNumber() + ]]); + } + + /** + * @throws Exception + */ + private function firstNumerableOverloaded(float|int|Numerable $arg): float|int + { + return $this->numerableOverloaded($arg)[0]; + } +} \ No newline at end of file diff --git a/src/Numerable/Rounded.php b/src/Numerable/Rounded.php new file mode 100644 index 0000000..e3b0a0c --- /dev/null +++ b/src/Numerable/Rounded.php @@ -0,0 +1,54 @@ +number = $num; + $this->precision = $precision; + } + + /** + * @inheritDoc + */ + public function asNumber(): float|int + { + return round($this->firstNumerableOverloaded($this->number), $this->precision); + } +} \ No newline at end of file diff --git a/tests/Logical/IsEmptyTest.php b/tests/Logical/IsEmptyTest.php new file mode 100644 index 0000000..3cbfa42 --- /dev/null +++ b/tests/Logical/IsEmptyTest.php @@ -0,0 +1,39 @@ +assertEquals( + true, + (new IsEmpty([]))->asBool() + ); + $this->assertEquals( + true, + (new IsEmpty(""))->asBool() + ); + $this->assertEquals( + true, + (new IsEmpty(new TxtBlank()))->asBool() + ); + $this->assertEquals( + true, + (new IsEmpty(new ArrayableOf([])))->asBool() + ); + $this->assertEquals( + false, + (new IsEmpty(["foo", "bar"]))->asBool() + ); + } +} \ No newline at end of file diff --git a/tests/Logical/IsNotEmptyTest.php b/tests/Logical/IsNotEmptyTest.php new file mode 100644 index 0000000..2e1eef3 --- /dev/null +++ b/tests/Logical/IsNotEmptyTest.php @@ -0,0 +1,39 @@ +assertEquals( + false, + (new IsNotEmpty([]))->asBool() + ); + $this->assertEquals( + false, + (new IsNotEmpty(""))->asBool() + ); + $this->assertEquals( + false, + (new IsNotEmpty(new TxtBlank()))->asBool() + ); + $this->assertEquals( + false, + (new IsNotEmpty(new ArrayableOf([])))->asBool() + ); + $this->assertEquals( + true, + (new IsNotEmpty(["foo", "bar"]))->asBool() + ); + } +} \ No newline at end of file diff --git a/tests/Logical/MatchRegexTest.php b/tests/Logical/MatchRegexTest.php index ffcb7d9..78a4e2e 100644 --- a/tests/Logical/MatchRegexTest.php +++ b/tests/Logical/MatchRegexTest.php @@ -4,7 +4,6 @@ use Exception; use Maxonfjvipon\Elegant_Elephant\Logical\PregMatch; -use Maxonfjvipon\Elegant_Elephant\Logical\MatchToIn; use Maxonfjvipon\Elegant_Elephant\Text\TextOf; use PHPUnit\Framework\TestCase; diff --git a/tests/Numerable/MaxOfTest.php b/tests/Numerable/MaxOfTest.php new file mode 100644 index 0000000..4ddae73 --- /dev/null +++ b/tests/Numerable/MaxOfTest.php @@ -0,0 +1,26 @@ +assertEquals( + 10, + (new MaxOf(2, 4, 10, 1, -1))->asNumber() + ); + $this->assertEquals( + 5, + MaxOf::new(Addition::new(2, 3), 1, 2, 3)->asNumber() + ); + } +} \ No newline at end of file diff --git a/tests/Numerable/RoundedTest.php b/tests/Numerable/RoundedTest.php new file mode 100644 index 0000000..f4f9ed8 --- /dev/null +++ b/tests/Numerable/RoundedTest.php @@ -0,0 +1,26 @@ +assertEquals( + round(10.4567, 2), + (new Rounded(10.4567, 2))->asNumber() + ); + $this->assertEquals( + round(11.2792), + (new Rounded(new NumerableOf(11.2792)))->asNumber() + ); + } +} \ No newline at end of file