Skip to content

Commit

Permalink
Introduced Rounded #13
Browse files Browse the repository at this point in the history
Introduced MaxOf #14
Introduced IsEmpty/IsNotEmpty #15
Minimized overloading in classes #12
  • Loading branch information
maximtrunnikov committed Feb 21, 2022
1 parent 1c16942 commit 3441d3b
Show file tree
Hide file tree
Showing 15 changed files with 413 additions and 60 deletions.
53 changes: 53 additions & 0 deletions src/Logical/IsEmpty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Maxonfjvipon\Elegant_Elephant\Logical;

use Maxonfjvipon\Elegant_Elephant\Arrayable;
use Maxonfjvipon\Elegant_Elephant\Logical;
use Maxonfjvipon\Elegant_Elephant\Numerable\LengthOf;
use Maxonfjvipon\Elegant_Elephant\Text;
use Maxonfjvipon\OverloadedElephant\Overloadable;

/**
* Is empty.
*/
final class IsEmpty implements Logical
{
use Overloadable;

/**
* @var array|Arrayable|Text|string $value
*/
private Text|string|array|Arrayable $value;

/**
* @param string|array|Arrayable|Text $value
* @return IsEmpty
*/
public static function new(string|array|Arrayable|Text $value)
{
return new self($value);
}

/**
* Ctor.
* @param string|array|Arrayable|Text $value
*/
public function __construct(string|array|Arrayable|Text $value)
{
$this->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];
}
}
49 changes: 49 additions & 0 deletions src/Logical/IsNotEmpty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Maxonfjvipon\Elegant_Elephant\Logical;

use Maxonfjvipon\Elegant_Elephant\Arrayable;
use Maxonfjvipon\Elegant_Elephant\Logical;
use Maxonfjvipon\Elegant_Elephant\Text;
use Maxonfjvipon\OverloadedElephant\Overloadable;

/**
* Is not empty.
*/
final class IsNotEmpty implements Logical
{
use Overloadable;

/**
* @var array|Arrayable|Text|string $value
*/
private Text|string|array|Arrayable $value;

/**
* @param string|array|Arrayable|Text $value
* @return IsNotEmpty
*/
public static function new(string|array|Arrayable|Text $value)
{
return new self($value);
}

/**
* Ctor.
* @param string|array|Arrayable|Text $value
*/
public function __construct(string|array|Arrayable|Text $value)
{
$this->value = $value;
}

/**
* @inheritDoc
*/
public function asBool(): bool
{
return (new Negation(
new IsEmpty($this->value)
))->asBool();
}
}
32 changes: 15 additions & 17 deletions src/Numerable/Addition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
}
}
12 changes: 6 additions & 6 deletions src/Numerable/Decremented.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Numerable/Incremented.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
45 changes: 45 additions & 0 deletions src/Numerable/MaxOf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Maxonfjvipon\Elegant_Elephant\Numerable;

use Maxonfjvipon\Elegant_Elephant\Numerable;

/**
* Max of
*/
final class MaxOf implements Numerable
{
use NumerableOverloaded;

/**
* @var float[]|int[]|Numerable[] $items
*/
private array $items;

/**
* Ctor wrap.
* @param float|int|Numerable ...$items
* @return MaxOf
*/
public static function new(float|int|Numerable ...$items)
{
return new self(...$items);
}

/**
* Ctor.
* @param float|int|Numerable ...$items
*/
public function __construct(float|int|Numerable ...$items)
{
$this->items = $items;
}

/**
* @inheritDoc
*/
public function asNumber(): float|int
{
return max(...$this->numerableOverloaded(...$this->items));
}
}
29 changes: 12 additions & 17 deletions src/Numerable/Multiplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
}
}
21 changes: 8 additions & 13 deletions src/Numerable/NumerableOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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());
}
}
Loading

0 comments on commit 3441d3b

Please sign in to comment.