Skip to content

Commit

Permalink
add nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Jul 31, 2024
1 parent 810e7f1 commit d1036a6
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 31 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"require": {
"php": ">=8.0",
"ext-mbstring": "*",
"h4kuna/memoize": "^0.1.4",
"h4kuna/memoize": "^0.1.5",
"nette/utils": "^3.0.1 || ^4.0"
},
"autoload": {
Expand Down
6 changes: 6 additions & 0 deletions src/Basic/Bools.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ final class Bools
{
use StaticClass;

public static function nullable(mixed $value): ?bool
{
return $value === null ? null : self::from($value);
}


public static function from(mixed $value): bool
{
if ($value === true) {
Expand Down
6 changes: 6 additions & 0 deletions src/Basic/Floats.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ final class Floats
{
use StaticClass;

public static function nullable(mixed $value): ?float
{
return $value === null ? null : self::from($value);
}


public static function from(
mixed $value,
string $decimalPoint = ',',
Expand Down
6 changes: 6 additions & 0 deletions src/Basic/Integer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ final class Integer
{
use StaticClass;

public static function nullable(mixed $value): ?int
{
return $value === null ? null : self::from($value);
}


public static function from(mixed $value): int
{
if (is_bool($value) || is_int($value) || $value === null || $value === '' || (is_numeric($value) && $value == (int) $value)) {
Expand Down
6 changes: 6 additions & 0 deletions src/Basic/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ final class Strings
{
use StaticClass;

public static function nullable(mixed $value): ?string
{
return $value === null ? null : self::from($value);
}


public static function strokeToPoint(string $value): string
{
return strtr($value, ',', '.');
Expand Down
4 changes: 2 additions & 2 deletions src/Date/Easter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace h4kuna\DataType\Date;

use DateTimeImmutable;
use h4kuna\Memoize\MemoryStorageStatic;
use h4kuna\Memoize\MemoizeStatic;
use Nette\StaticClass;

final class Easter
{
use StaticClass;
use MemoryStorageStatic;
use MemoizeStatic;

public static ?bool $useNative = null;

Expand Down
12 changes: 12 additions & 0 deletions tests/src/Unit/Basic/BoolsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ public function testFromFailed(mixed $input): void
Bools::from($input);
}


/**
* @dataProvider dataFrom
*/
public function testNullable(mixed $input, bool $expected): void
{
if ($input === null) {
$expected = null;
}
Assert::same($expected, Bools::nullable($input));
}

}

(new BoolsTest())->run();
61 changes: 42 additions & 19 deletions tests/src/Unit/Basic/FloatsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,38 @@
*/
final class FloatsTest extends \Tester\TestCase
{
/**
* @return array<array<mixed>>
*/
protected function dataFrom(): array
{
return [
['0', 0.0],
['0.0', 0.0],
['-0.0', 0.0],
['-.0', 0.0],
['-0.', 0.0],
['', 0.0],
[null, 0.0],
[false, 0.0],
[true, 1.0],
[-1, -1.0],
[-1.0, -1.0],
['-1', -1.0],
['-1.0', -1.0],
['-1,0', -1.0],
[' - 1 , 0 ', -1.0],
['1:30', 1.5],
];
}

public function testFromString(): void

/**
* @dataProvider dataFrom
*/
public function testFromString(mixed $input, float $expected): void
{
Assert::same(0.0, Floats::from('0'));
Assert::same(0.0, Floats::from('0.0'));
Assert::same(0.0, Floats::from('-0.0'));
Assert::same(0.0, Floats::from('-.0'));
Assert::same(0.0, Floats::from('-0.'));
Assert::same(0.0, Floats::from(''));
Assert::same(0.0, Floats::from(null));
Assert::same(1.0, Floats::from(true));
Assert::same(0.0, Floats::from(false));

Assert::same(-1.0, Floats::from(-1));
Assert::same(-1.0, Floats::from(-1.0));
Assert::same(-1.0, Floats::from('-1'));
Assert::same(-1.0, Floats::from('-1.0'));
Assert::same(-1.0, Floats::from('-1,0'));
Assert::same(-1.0, Floats::from(' - 1 , 0 '));

Assert::same(1.5, Floats::from('1:30'));
Assert::same($expected, Floats::from($input));
}


Expand Down Expand Up @@ -61,6 +72,18 @@ public function testThousand(): void
Assert::same(3620.0, Floats::from('3.620,00', ',', '.'));
}


/**
* @dataProvider dataFrom
*/
public function testNullable(mixed $input, float $expected): void
{
if ($input === null) {
$expected = null;
}
Assert::same($expected, Floats::nullable($input));
}

}

(new FloatsTest())->run();
41 changes: 33 additions & 8 deletions tests/src/Unit/Basic/IntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,29 @@
*/
final class IntegerTest extends Tester\TestCase
{
/**
* @return array<array<mixed>>
*/
protected function dataFrom(): array
{
return [
[false, 0],
[null, 0],
['', 0],
[1, 1],
['1.0', 1],
['1', 1],
[' 1 ', 1],
];
}

public function testFromString(): void

/**
* @dataProvider dataFrom
*/
public function testFromString(mixed $input, int $expected): void
{
Assert::same(1, Integer::from(1));
Assert::same(0, Integer::from(false));
Assert::same(0, Integer::from(null));
Assert::same(0, Integer::from(''));
Assert::same(1, Integer::from('1.0'));
Assert::same(1, Integer::from('1'));
Assert::same(1, Integer::from(' 1 '));
Assert::same($expected, Integer::from($input));
}


Expand All @@ -35,6 +48,18 @@ public function testFailed(): void
Assert::same(1, Integer::from('1.1')); // not int
}


/**
* @dataProvider dataFrom
*/
public function testNullable(mixed $input, int $expected): void
{
if ($input === null) {
$expected = null;
}
Assert::same($expected, Integer::nullable($input));
}

}

(new IntegerTest())->run();
7 changes: 7 additions & 0 deletions tests/src/Unit/Basic/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ protected function provideReplaceEnd(): array
['oof_', 'oof_', 'f', false],
];
}


public function testNullable(): void
{
Assert::null(Strings::nullable(null));
Assert::same('null', Strings::nullable('null'));
}
}

(new StringsTest())->run();
2 changes: 1 addition & 1 deletion tests/src/Unit/Date/EasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ public function testCompareNativeAndCounted(): void

}

Memoize\Helpers::bypassMemoize();
Memoize\Helper::bypassMemoize();
(new EasterTest())->run();

0 comments on commit d1036a6

Please sign in to comment.