Skip to content

Commit b859347

Browse files
authored
Merge pull request #11 from emulgeator/empty-string-cast
Casting empty string to null in case of nullable `int` or `float`
2 parents e46a2f7 + 83248dc commit b859347

File tree

6 files changed

+106
-3
lines changed

6 files changed

+106
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [1.1.0] - 2024-04-08
9+
10+
### Added
11+
- Casting empty string to null in case of nullable `int` or `float`
12+
13+
814
## [1.0.0] - 2024-03-26
915

1016
### Added

src/Caster.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,18 @@ public static function castToBool($input): ?bool
1010
{
1111
return is_null($input) ? null : filter_var($input, FILTER_VALIDATE_BOOLEAN);
1212
}
13+
14+
public static function castToInt($input): ?int
15+
{
16+
$result = (is_null($input) || $input === '') ? null : filter_var($input, FILTER_VALIDATE_INT);
17+
18+
return $result === false ? null : $result;
19+
}
20+
21+
public static function castToFloat($input): ?float
22+
{
23+
$result = (is_null($input) || $input === '') ? null : filter_var($input, FILTER_VALIDATE_FLOAT);
24+
25+
return $result === false ? null : $result;
26+
}
1327
}

src/Mapper.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,18 @@ private function setValueByPhpType(\ReflectionNamedType $type, ?DocBlockType $do
136136
$property->setValue($object, $valueCasted);
137137
}
138138
} else {
139-
if ($type->getName() === 'bool') {
140-
$value = Caster::castToBool($value);
139+
switch ($type->getName()) {
140+
case 'bool':
141+
$value = Caster::castToBool($value);
142+
break;
143+
144+
case 'int':
145+
$value = Caster::castToInt($value);
146+
break;
147+
148+
case 'float':
149+
$value = Caster::castToInt($value);
150+
break;
141151
}
142152
$property->setValue($object, $value);
143153
}

test/Unit/CasterTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,48 @@ public function testCastToBool($input, ?bool $mappedValue)
3030

3131
$this->assertSame($mappedValue, $result);
3232
}
33+
34+
public static function intProvider(): array
35+
{
36+
return [
37+
'integer' => [1, 1],
38+
'string integer' => ['11', 11],
39+
'null' => [null, null],
40+
'empty string' => ['', null],
41+
'zero' => [0, 0],
42+
'invalid' => [false, null],
43+
];
44+
}
45+
46+
/**
47+
* @dataProvider intProvider
48+
*/
49+
public function testCastToInt($input, ?int $mappedValue)
50+
{
51+
$result = Caster::castToInt($input);
52+
53+
$this->assertSame($mappedValue, $result);
54+
}
55+
56+
public static function floatProvider(): array
57+
{
58+
return [
59+
'float' => [1.1, 1.1],
60+
'string floateger' => ['2.2', 2.2],
61+
'null' => [null, null],
62+
'empty string' => ['', null],
63+
'zero' => [0, 0],
64+
'invalid' => [false, null],
65+
];
66+
}
67+
68+
/**
69+
* @dataProvider floatProvider
70+
*/
71+
public function testCastToFloat($input, ?float $mappedValue)
72+
{
73+
$result = Caster::castToFloat($input);
74+
75+
$this->assertSame($mappedValue, $result);
76+
}
3377
}

test/Unit/MapperTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,28 @@ public function testMapWhenNullGiven_shouldSet()
3535
$this->assertNull($result->getInt());
3636
}
3737

38+
public function testMapWhenEmptyStringGivenForNullableInt_shouldSetToNull()
39+
{
40+
$mapper = $this->getMapper();
41+
$input = ['int' => ''];
42+
43+
/** @var ScalarTypedStub $result */
44+
$result = $mapper->map($input, ScalarTypedStub::class);
45+
46+
$this->assertNull($result->getInt());
47+
}
48+
49+
public function testMapWhenEmptyStringGivenForNullableFloat_shouldSetToNull()
50+
{
51+
$mapper = $this->getMapper();
52+
$input = ['float' => ''];
53+
54+
/** @var ScalarTypedStub $result */
55+
$result = $mapper->map($input, ScalarTypedStub::class);
56+
57+
$this->assertNull($result->getFloat());
58+
}
59+
3860
public function testMapWhenKeyContainsSpecialCharacter_shouldSetByRemovingSpecialChars()
3961
{
4062
$mapper = $this->getMapper();

test/Unit/Stub/ScalarTypedStub.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace Emul\ArrayToClassMapper\Test\Unit\Stub;
56

67
class ScalarTypedStub
78
{
8-
private ?int $int = null;
9+
private ?int $int;
10+
private ?float $float;
911

1012
public function __construct()
1113
{
@@ -16,4 +18,9 @@ public function getInt(): ?int
1618
{
1719
return $this->int;
1820
}
21+
22+
public function getFloat(): ?float
23+
{
24+
return $this->float;
25+
}
1926
}

0 commit comments

Comments
 (0)