diff --git a/composer.json b/composer.json index d820b06..f5cbfad 100644 --- a/composer.json +++ b/composer.json @@ -18,12 +18,12 @@ "ergebnis/phpstan-rules": "^2.2.0", "thecodingmachine/phpstan-safe-rule": "^1.2.0", "thecodingmachine/phpstan-strict-rules": "^1.0.0", - "phpstan/phpstan-phpunit": "^1.3.16", - "squizlabs/php_codesniffer": "^3.9.0", - "phpstan/phpstan": "^1.10.59", - "phpstan/phpstan-strict-rules": "^1.5.2", - "phpunit/phpunit": "^11.0.3", - "rector/rector": "^1.0.1" + "phpstan/phpstan-phpunit": "^1.4.0", + "squizlabs/php_codesniffer": "^3.10.3", + "phpstan/phpstan": "^1.12.7", + "phpstan/phpstan-strict-rules": "^1.6.1", + "phpunit/phpunit": "^11.4.3", + "rector/rector": "^1.2.9" }, "autoload": { "psr-4": { diff --git a/rector.php b/rector.php index 01ceb93..e487f31 100644 --- a/rector.php +++ b/rector.php @@ -5,8 +5,8 @@ use Rector\Config\RectorConfig; use Rector\Naming\Rector\Class_\RenamePropertyToMatchTypeRector; use Rector\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector; +use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitSelfCallRector; use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector; -use Rector\PHPUnit\Rector\Class_\PreferPHPUnitSelfCallRector; use Rector\PHPUnit\Set\PHPUnitSetList; use Rector\Set\ValueObject\LevelSetList; use Rector\Set\ValueObject\SetList; diff --git a/src/TypeGuard.php b/src/TypeGuard.php index 6bef943..b9ed289 100644 --- a/src/TypeGuard.php +++ b/src/TypeGuard.php @@ -154,6 +154,70 @@ public function asDateTimeString(mixed $value): string|null return $value->format($this->dateTimeFormat()); } + /** + * @param T $value + * + * @return T|null + * + * @template T + */ + public function blankAsNull(mixed $value): mixed + { + if ($value === '') { + return null; + } + + return $value; + } + + /** + * @param T $value + * + * @return T|null + * + * @template T + */ + public function falseAsNull(mixed $value): mixed + { + if ($value === false) { + return null; + } + + return $value; + } + + /** + * @param T $value + * + * @return T|null + * + * @template T + */ + public function zeroAsNull(mixed $value): mixed + { + if ($value === 0 || $value === 0.0) { + return null; + } + + return $value; + } + + /** + * @param T $value + * + * @return T|null + * + * @template T + */ + public function falsyAsNull(mixed $value): mixed + { + if (! (bool) $value) { + return null; + } + + return $value; + } + public function timeZone(DateTimeZone|string|null $timeZone = null): DateTimeZone { if (is_string($timeZone)) { diff --git a/src/functions.php b/src/functions.php index 2d0a78a..e82f5fb 100644 --- a/src/functions.php +++ b/src/functions.php @@ -64,6 +64,66 @@ function asDateTimeString(mixed $value): string|null } } +if (!function_exists('\Plook\TypeGuard\blankAsNull')) { // @codeCoverageIgnore + + /** + * @param T $value + * + * @return T|null + * + * @template T + */ + function blankAsNull(mixed $value): mixed + { + return TypeGuard::instance()->blankAsNull($value); + } +} + +if (!function_exists('\Plook\TypeGuard\falseAsNull')) { // @codeCoverageIgnore + + /** + * @param T $value + * + * @return T|null + * + * @template T + */ + function falseAsNull(mixed $value): mixed + { + return TypeGuard::instance()->falseAsNull($value); + } +} + +if (!function_exists('\Plook\TypeGuard\zeroAsNull')) { // @codeCoverageIgnore + + /** + * @param T $value + * + * @return T|null + * + * @template T + */ + function zeroAsNull(mixed $value): mixed + { + return TypeGuard::instance()->zeroAsNull($value); + } +} + +if (!function_exists('\Plook\TypeGuard\falsyAsNull')) { // @codeCoverageIgnore + + /** + * @param T $value + * + * @return T|null + * + * @template T + */ + function falsyAsNull(mixed $value): mixed + { + return TypeGuard::instance()->falsyAsNull($value); + } +} + if (!function_exists('\Plook\TypeGuard\notNull')) { // @codeCoverageIgnore /** diff --git a/tests/BlankAsNullTest.php b/tests/BlankAsNullTest.php new file mode 100644 index 0000000..560dd69 --- /dev/null +++ b/tests/BlankAsNullTest.php @@ -0,0 +1,57 @@ +