Skip to content

Commit

Permalink
#72 - NumOf::txt introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed Mar 24, 2023
1 parent 89e1153 commit 08943b2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Num/NumOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@

use Maxonfjvipon\ElegantElephant\Any;
use Maxonfjvipon\ElegantElephant\Num;
use Maxonfjvipon\ElegantElephant\Txt;
use Maxonfjvipon\ElegantElephant\Txt\EnsureTxt;

/**
* Number of.
*/
final class NumOf implements Num
{
use EnsureNum;
use EnsureTxt;

/**
* @var callable Callback
Expand Down Expand Up @@ -68,6 +71,15 @@ final public static function number(int|float $number): NumOf
return new NumOf(fn () => $number);
}

/**
* Num of text
* @param string|Txt $txt Text
*/
final public static function txt(string|Txt $txt): NumOf
{
return new NumOf(fn (NumOf $self) => +$self->ensuredString($txt)); /** @phpstan-ignore-line */
}

/**
* Num of Any.
* @param Any $any Any
Expand Down
37 changes: 37 additions & 0 deletions tests/Num/NumOfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,43 @@ public function numOfFloat(): void
);
}

/**
* @test
* @return void
* @throws Exception
*/
public function numOfNotNumericTest(): void
{
$this->expectErrorMessage("Unsupported operand types: string * int");
NumOf::txt('foo')->asNumber();
}

/**
* @test
* @return void
* @throws Exception
*/
public function numOfIntText(): void
{
$this->assertNumThat(
NumOf::txt("42"),
new IsEqual(42)
);
}

/**
* @test
* @return void
* @throws Exception
*/
public function numOfFloatText(): void
{
$this->assertNumThat(
NumOf::txt("42.5"),
new IsEqual(42.5)
);
}

/**
* @test
* @return void
Expand Down

0 comments on commit 08943b2

Please sign in to comment.