Skip to content

Commit 1f85b69

Browse files
committed
feat: Add to NULL converters
1 parent 0469a40 commit 1f85b69

File tree

6 files changed

+372
-0
lines changed

6 files changed

+372
-0
lines changed

src/TypeGuard.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,70 @@ public function asDateTimeString(mixed $value): string|null
154154
return $value->format($this->dateTimeFormat());
155155
}
156156

157+
/**
158+
* @param T $value
159+
*
160+
* @return T|null
161+
*
162+
* @template T
163+
*/
164+
public function blankAsNull(mixed $value): mixed
165+
{
166+
if ($value === '') {
167+
return null;
168+
}
169+
170+
return $value;
171+
}
172+
173+
/**
174+
* @param T $value
175+
*
176+
* @return T|null
177+
*
178+
* @template T
179+
*/
180+
public function falseAsNull(mixed $value): mixed
181+
{
182+
if ($value === false) {
183+
return null;
184+
}
185+
186+
return $value;
187+
}
188+
189+
/**
190+
* @param T $value
191+
*
192+
* @return T|null
193+
*
194+
* @template T
195+
*/
196+
public function zeroAsNull(mixed $value): mixed
197+
{
198+
if ($value === 0 || $value === 0.0) {
199+
return null;
200+
}
201+
202+
return $value;
203+
}
204+
205+
/**
206+
* @param T $value
207+
*
208+
* @return T|null
209+
*
210+
* @template T
211+
*/
212+
public function falsyAsNull(mixed $value): mixed
213+
{
214+
if (! (bool) $value) {
215+
return null;
216+
}
217+
218+
return $value;
219+
}
220+
157221
public function timeZone(DateTimeZone|string|null $timeZone = null): DateTimeZone
158222
{
159223
if (is_string($timeZone)) {

src/functions.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,66 @@ function asDateTimeString(mixed $value): string|null
6464
}
6565
}
6666

67+
if (!function_exists('\Plook\TypeGuard\blankAsNull')) { // @codeCoverageIgnore
68+
69+
/**
70+
* @param T $value
71+
*
72+
* @return T|null
73+
*
74+
* @template T
75+
*/
76+
function blankAsNull(mixed $value): mixed
77+
{
78+
return TypeGuard::instance()->blankAsNull($value);
79+
}
80+
}
81+
82+
if (!function_exists('\Plook\TypeGuard\falseAsNull')) { // @codeCoverageIgnore
83+
84+
/**
85+
* @param T $value
86+
*
87+
* @return T|null
88+
*
89+
* @template T
90+
*/
91+
function falseAsNull(mixed $value): mixed
92+
{
93+
return TypeGuard::instance()->falseAsNull($value);
94+
}
95+
}
96+
97+
if (!function_exists('\Plook\TypeGuard\zeroAsNull')) { // @codeCoverageIgnore
98+
99+
/**
100+
* @param T $value
101+
*
102+
* @return T|null
103+
*
104+
* @template T
105+
*/
106+
function zeroAsNull(mixed $value): mixed
107+
{
108+
return TypeGuard::instance()->zeroAsNull($value);
109+
}
110+
}
111+
112+
if (!function_exists('\Plook\TypeGuard\falsyAsNull')) { // @codeCoverageIgnore
113+
114+
/**
115+
* @param T $value
116+
*
117+
* @return T|null
118+
*
119+
* @template T
120+
*/
121+
function falsyAsNull(mixed $value): mixed
122+
{
123+
return TypeGuard::instance()->falsyAsNull($value);
124+
}
125+
}
126+
67127
if (!function_exists('\Plook\TypeGuard\notNull')) { // @codeCoverageIgnore
68128

69129
/**

tests/BlankAsNullTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Plook\Tests\TypeGuard;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\CoversFunction;
9+
use PHPUnit\Framework\TestCase;
10+
use Plook\TypeGuard\NotConvertable;
11+
use Plook\TypeGuard\TypeGuard;
12+
use stdClass;
13+
14+
use function Plook\TypeGuard\blankAsNull;
15+
16+
#[CoversClass(TypeGuard::class)]
17+
#[CoversClass(NotConvertable::class)]
18+
#[CoversFunction('\Plook\TypeGuard\blankAsNull')]
19+
final class BlankAsNullTest extends TestCase
20+
{
21+
public function testConvertsBlankToNull(): void
22+
{
23+
self::assertNull(blankAsNull(''));
24+
}
25+
26+
public function testDoesNotTouchStrings(): void
27+
{
28+
self::assertSame('String', blankAsNull('String'));
29+
}
30+
31+
public function testDoesNotTouchInts(): void
32+
{
33+
self::assertSame(1000, blankAsNull(1000));
34+
}
35+
36+
public function testDoesNotTouchZero(): void
37+
{
38+
self::assertSame(0, blankAsNull(0));
39+
}
40+
41+
public function testDoesTouchTrue(): void
42+
{
43+
self::assertTrue(blankAsNull(true));
44+
}
45+
46+
public function testDoesTouchFalse(): void
47+
{
48+
self::assertFalse(blankAsNull(false));
49+
}
50+
51+
public function testDoesNotTouchObjects(): void
52+
{
53+
$object = new stdClass();
54+
55+
self::assertSame($object, blankAsNull($object));
56+
}
57+
}

tests/FalseAsNullTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Plook\Tests\TypeGuard;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\CoversFunction;
9+
use PHPUnit\Framework\TestCase;
10+
use Plook\TypeGuard\NotConvertable;
11+
use Plook\TypeGuard\TypeGuard;
12+
use stdClass;
13+
14+
use function Plook\TypeGuard\falseAsNull;
15+
16+
#[CoversClass(TypeGuard::class)]
17+
#[CoversClass(NotConvertable::class)]
18+
#[CoversFunction('\Plook\TypeGuard\falseAsNull')]
19+
final class FalseAsNullTest extends TestCase
20+
{
21+
public function testConvertsFalseToNull(): void
22+
{
23+
self::assertNull(falseAsNull(false));
24+
}
25+
26+
public function testDoesTouchTrue(): void
27+
{
28+
self::assertTrue(falseAsNull(true));
29+
}
30+
31+
public function testDoesNotTouchStrings(): void
32+
{
33+
self::assertSame('String', falseAsNull('String'));
34+
}
35+
36+
public function testDoesNotTouchBlanks(): void
37+
{
38+
self::assertSame('', falseAsNull(''));
39+
}
40+
41+
public function testDoesNotTouchInts(): void
42+
{
43+
self::assertSame(1000, falseAsNull(1000));
44+
}
45+
46+
public function testDoesNotTouchZero(): void
47+
{
48+
self::assertSame(0, falseAsNull(0));
49+
}
50+
51+
public function testDoesNotTouchObjects(): void
52+
{
53+
$object = new stdClass();
54+
55+
self::assertSame($object, falseAsNull($object));
56+
}
57+
}

tests/FalsyAsNullTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Plook\Tests\TypeGuard;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\CoversFunction;
9+
use PHPUnit\Framework\TestCase;
10+
use Plook\TypeGuard\NotConvertable;
11+
use Plook\TypeGuard\TypeGuard;
12+
use stdClass;
13+
14+
use function Plook\TypeGuard\falsyAsNull;
15+
16+
#[CoversClass(TypeGuard::class)]
17+
#[CoversClass(NotConvertable::class)]
18+
#[CoversFunction('\Plook\TypeGuard\falsyAsNull')]
19+
final class FalsyAsNullTest extends TestCase
20+
{
21+
public function testConvertsBlankToNull(): void
22+
{
23+
self::assertNull(falsyAsNull(''));
24+
}
25+
26+
public function testDoesNotTouchStrings(): void
27+
{
28+
self::assertSame('String', falsyAsNull('String'));
29+
}
30+
31+
public function testConvertsZeroIntToNUll(): void
32+
{
33+
self::assertNull(falsyAsNull(0));
34+
}
35+
36+
public function testConvertsZeroFloatToNull(): void
37+
{
38+
self::assertNull(falsyAsNull(0.0));
39+
}
40+
41+
public function testDoesNotTouchInts(): void
42+
{
43+
self::assertSame(1000, falsyAsNull(1000));
44+
}
45+
46+
public function testDoesNotTouchFloats(): void
47+
{
48+
self::assertSame(1234.56, falsyAsNull(1234.56));
49+
}
50+
51+
public function testConvertsFalseToNull(): void
52+
{
53+
self::assertNull(falsyAsNull(false));
54+
}
55+
56+
public function testDoesTouchTrue(): void
57+
{
58+
self::assertTrue(falsyAsNull(true));
59+
}
60+
61+
public function testDoesNotTouchObjects(): void
62+
{
63+
$object = new stdClass();
64+
65+
self::assertSame($object, falsyAsNull($object));
66+
}
67+
}

tests/ZeroAsNullTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Plook\Tests\TypeGuard;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\CoversFunction;
9+
use PHPUnit\Framework\TestCase;
10+
use Plook\TypeGuard\NotConvertable;
11+
use Plook\TypeGuard\TypeGuard;
12+
use stdClass;
13+
14+
use function Plook\TypeGuard\zeroAsNull;
15+
16+
#[CoversClass(TypeGuard::class)]
17+
#[CoversClass(NotConvertable::class)]
18+
#[CoversFunction('\Plook\TypeGuard\zeroAsNull')]
19+
final class ZeroAsNullTest extends TestCase
20+
{
21+
public function testConvertsZeroIntToNull(): void
22+
{
23+
self::assertNull(zeroAsNull(0));
24+
}
25+
26+
public function testConvertsZeroFloatToNull(): void
27+
{
28+
self::assertNull(zeroAsNull(0.0));
29+
}
30+
31+
public function testDoesNotTouchInts(): void
32+
{
33+
self::assertSame(1000, zeroAsNull(1000));
34+
}
35+
36+
public function testDoesNotTouchFloats(): void
37+
{
38+
self::assertSame(1234.567, zeroAsNull(1234.567));
39+
}
40+
41+
public function testDoesNotTouchStrings(): void
42+
{
43+
self::assertSame('String', zeroAsNull('String'));
44+
}
45+
46+
public function testDoesNotTouchBlanks(): void
47+
{
48+
self::assertSame('', zeroAsNull(''));
49+
}
50+
51+
public function testDoesTouchTrue(): void
52+
{
53+
self::assertTrue(zeroAsNull(true));
54+
}
55+
56+
public function testDoesTouchFalse(): void
57+
{
58+
self::assertFalse(zeroAsNull(false));
59+
}
60+
61+
public function testDoesNotTouchObjects(): void
62+
{
63+
$object = new stdClass();
64+
65+
self::assertSame($object, zeroAsNull($object));
66+
}
67+
}

0 commit comments

Comments
 (0)