Skip to content

Commit c762949

Browse files
committed
feat(getFloat): Add support for float values using comma
1 parent 34d67ca commit c762949

11 files changed

+156
-33
lines changed

docs/content/en/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ $value = $data->getRequiredInt('key');
176176

177177
### Float
178178

179-
> Throws `ValidationFailedException` if value is not numeric.
179+
> Throws `ValidationFailedException` if value is not numeric. String value using comma instead of dot are converted to
180+
> valid float. (1200,50 => 1200.50)
180181
181182
Get nullable float value.
182183

docs/content/en/transformers.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,25 @@ $value = $data->getString('key', transformers: [new TrimString()]);
9595
// $value === 'Marco Polo'
9696
```
9797

98+
### ReplaceCommaWithDot
99+
100+
Replaces `,` with `.` in given string **before** validation starts. This is used in **default float strategy**.
101+
102+
```php
103+
use Wrkflow\GetValue\GetValue;
104+
use Wrkflow\GetValue\DataHolders\ArrayData;
105+
use Wrkflow\GetValue\Transformers\ReplaceCommaWithDot;
106+
107+
// Get trimmed string (no '' to null transformation)
108+
$data = new GetValue(new ArrayData([
109+
'key' => '1200,50',
110+
]));
111+
$value = $data->getFloat('key');
112+
// $value === 1200.50
113+
$string = $data->getString('key', transformers: [new ReplaceCommaWithDot()]);
114+
// $string === '1200.50'
115+
```
116+
98117
### ClosureTransformer
99118

100119
> Can't be used with get\*Array\* methods.

src/Strategies/DefaultTransformerStrategy.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Wrkflow\GetValue\Strategies;
66

77
use Wrkflow\GetValue\Contracts\TransformerStrategyContract;
8+
use Wrkflow\GetValue\Transformers\ReplaceCommaWithDot;
89
use Wrkflow\GetValue\Transformers\TransformToBool;
910
use Wrkflow\GetValue\Transformers\TrimAndEmptyStringToNull;
1011

@@ -32,7 +33,7 @@ public function dateTime(): array
3233

3334
public function float(): array
3435
{
35-
return [new TrimAndEmptyStringToNull()];
36+
return [new TrimAndEmptyStringToNull(), new ReplaceCommaWithDot()];
3637
}
3738

3839
public function array(): array
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Wrkflow\GetValue\Transformers;
6+
7+
use Wrkflow\GetValue\Contracts\TransformerContract;
8+
use Wrkflow\GetValue\GetValue;
9+
10+
class ReplaceCommaWithDot implements TransformerContract
11+
{
12+
public function beforeValidation(mixed $value, string $key): bool
13+
{
14+
return true;
15+
}
16+
17+
public function transform(mixed $value, string $key, GetValue $getValue): mixed
18+
{
19+
if (is_string($value)) {
20+
return str_replace(',', '.', $value);
21+
}
22+
23+
return $value;
24+
}
25+
}

tests/Strategies/DefaultTransformerStrategyTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Wrkflow\GetValue\Strategies\DefaultTransformerStrategy;
9+
use Wrkflow\GetValue\Transformers\ReplaceCommaWithDot;
910
use Wrkflow\GetValue\Transformers\TransformToBool;
1011
use Wrkflow\GetValue\Transformers\TrimAndEmptyStringToNull;
1112

@@ -17,7 +18,7 @@ public function testNoStrategyIsEmpty(): void
1718
$this->assertEquals([new TransformToBool()], $strategy->bool());
1819
$this->assertEquals([new TrimAndEmptyStringToNull()], $strategy->array());
1920
$this->assertEquals([new TrimAndEmptyStringToNull()], $strategy->dateTime());
20-
$this->assertEquals([new TrimAndEmptyStringToNull()], $strategy->float());
21+
$this->assertEquals([new TrimAndEmptyStringToNull(), new ReplaceCommaWithDot()], $strategy->float());
2122
$this->assertEquals([new TrimAndEmptyStringToNull()], $strategy->string());
2223
$this->assertEquals([new TrimAndEmptyStringToNull()], $strategy->int());
2324
$this->assertEquals([new TrimAndEmptyStringToNull()], $strategy->xml());

tests/Transformers/AbstractTransformerTestCase.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public function data(): array
5656
*/
5757
public function testTransform(TransformerExpectationEntity $entity): void
5858
{
59-
$this->assertValue($this->getTransformer(), $entity);
59+
$transformer = $this->getTransformer();
60+
$this->assertValue($transformer, $entity);
61+
$this->assertEquals($entity->expectBeforeValidation, $transformer->beforeValidation($entity->value, 'test'));
6062
}
6163

6264
public function assertValue(TransformerContract $transformer, TransformerExpectationEntity $entity): void
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Wrkflow\GetValueTests\Transformers;
6+
7+
use Wrkflow\GetValue\Contracts\TransformerContract;
8+
use Wrkflow\GetValue\DataHolders\ArrayData;
9+
use Wrkflow\GetValue\GetValue;
10+
use Wrkflow\GetValue\Transformers\ReplaceCommaWithDot;
11+
12+
class ReplaceCommaWithDotTest extends AbstractTransformerTestCase
13+
{
14+
public function dataToTest(): array
15+
{
16+
// Expects before validation to be set true to ensure numeric rule will succeed
17+
return [
18+
[new TransformerExpectationEntity(value: '', expectedValue: '', expectBeforeValidation: true)],
19+
[new TransformerExpectationEntity(value: ' ', expectedValue: ' ', expectBeforeValidation: true)],
20+
[new TransformerExpectationEntity(value: ' asd ', expectedValue: ' asd ', expectBeforeValidation: true)],
21+
[new TransformerExpectationEntity(value: '200', expectedValue: '200', expectBeforeValidation: true)],
22+
[new TransformerExpectationEntity(value: '200.01', expectedValue: '200.01', expectBeforeValidation: true)],
23+
[new TransformerExpectationEntity(value: '200,01', expectedValue: '200.01', expectBeforeValidation: true)],
24+
[
25+
new TransformerExpectationEntity(
26+
value: 'test, something',
27+
expectedValue: 'test. something',
28+
expectBeforeValidation: true
29+
),
30+
],
31+
];
32+
}
33+
34+
public function testDocumentationExample(): void
35+
{
36+
$data = new GetValue(new ArrayData([
37+
'key' => '1200,50',
38+
]));
39+
$value = $data->getFloat('key');
40+
$this->assertEquals(1200.50, $value);
41+
$string = $data->getString('key', transformers: [new ReplaceCommaWithDot()]);
42+
$this->assertEquals('1200.50', $string);
43+
}
44+
45+
protected function getTransformer(): TransformerContract
46+
{
47+
return new ReplaceCommaWithDot();
48+
}
49+
}

tests/Transformers/TransformToBoolTest.php

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,37 @@ class TransformToBoolTest extends AbstractTransformerTestCase
1212
public function dataToTest(): array
1313
{
1414
return [
15-
[new TransformerExpectationEntity(value: 'yes', expectedValue: true)],
16-
[new TransformerExpectationEntity(value: 'true', expectedValue: true)],
17-
[new TransformerExpectationEntity(value: true, expectedValue: true)],
18-
[new TransformerExpectationEntity(value: 1, expectedValue: true)],
19-
[new TransformerExpectationEntity(value: '1', expectedValue: true)],
20-
[new TransformerExpectationEntity(value: 'no', expectedValue: false)],
21-
[new TransformerExpectationEntity(value: 'false', expectedValue: false)],
22-
[new TransformerExpectationEntity(value: false, expectedValue: false)],
23-
[new TransformerExpectationEntity(value: 0, expectedValue: false)],
24-
[new TransformerExpectationEntity(value: '0', expectedValue: false)],
15+
[new TransformerExpectationEntity(value: 'yes', expectedValue: true, expectBeforeValidation: true)],
16+
[new TransformerExpectationEntity(value: 'true', expectedValue: true, expectBeforeValidation: true)],
17+
[new TransformerExpectationEntity(value: true, expectedValue: true, expectBeforeValidation: true)],
18+
[new TransformerExpectationEntity(value: 1, expectedValue: true, expectBeforeValidation: true)],
19+
[new TransformerExpectationEntity(value: '1', expectedValue: true, expectBeforeValidation: true)],
20+
[new TransformerExpectationEntity(value: 'no', expectedValue: false, expectBeforeValidation: true)],
21+
[new TransformerExpectationEntity(value: 'false', expectedValue: false, expectBeforeValidation: true)],
22+
[new TransformerExpectationEntity(value: false, expectedValue: false, expectBeforeValidation: true)],
23+
[new TransformerExpectationEntity(value: 0, expectedValue: false, expectBeforeValidation: true)],
24+
[new TransformerExpectationEntity(value: '0', expectedValue: false, expectBeforeValidation: true)],
2525
// Do not alter if diff value (rule validation will kick in).
26-
[new TransformerExpectationEntity(value: '', expectedValue: '')],
27-
[new TransformerExpectationEntity(value: ' ', expectedValue: ' ')],
28-
[new TransformerExpectationEntity(value: ' yes ', expectedValue: ' yes ')],
29-
[new TransformerExpectationEntity(value: 'no mix', expectedValue: 'no mix')],
30-
[new TransformerExpectationEntity(value: 'yes mix', expectedValue: 'yes mix')],
31-
[new TransformerExpectationEntity(value: '2', expectedValue: '2')],
32-
[new TransformerExpectationEntity(value: '1 2 3', expectedValue: '1 2 3')],
33-
[new TransformerExpectationEntity(value: '0 1 2 3', expectedValue: '0 1 2 3')],
26+
[new TransformerExpectationEntity(value: '', expectedValue: '', expectBeforeValidation: true)],
27+
[new TransformerExpectationEntity(value: ' ', expectedValue: ' ', expectBeforeValidation: true)],
28+
[new TransformerExpectationEntity(value: ' yes ', expectedValue: ' yes ', expectBeforeValidation: true)],
29+
[new TransformerExpectationEntity(value: 'no mix', expectedValue: 'no mix', expectBeforeValidation: true)],
30+
[
31+
new TransformerExpectationEntity(
32+
value: 'yes mix',
33+
expectedValue: 'yes mix',
34+
expectBeforeValidation: true
35+
),
36+
],
37+
[new TransformerExpectationEntity(value: '2', expectedValue: '2', expectBeforeValidation: true)],
38+
[new TransformerExpectationEntity(value: '1 2 3', expectedValue: '1 2 3', expectBeforeValidation: true)],
39+
[
40+
new TransformerExpectationEntity(
41+
value: '0 1 2 3',
42+
expectedValue: '0 1 2 3',
43+
expectBeforeValidation: true
44+
),
45+
],
3446
];
3547
}
3648

tests/Transformers/TransformerExpectationEntity.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public function __construct(
1717
public readonly mixed $value,
1818
public readonly mixed $expectedValue,
1919
mixed $expectedValueBeforeValidation = null,
20-
public readonly ?string $expectException = null
20+
public readonly ?string $expectException = null,
21+
public readonly bool $expectBeforeValidation = false,
2122
) {
2223
$this->expectedValueBeforeValidation = $expectedValueBeforeValidation ?? $expectedValue;
2324
}

tests/Transformers/TrimAndEmptyStringToNullTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@ class TrimAndEmptyStringToNullTest extends AbstractTransformerTestCase
1212
public function dataToTest(): array
1313
{
1414
return [
15-
[new TransformerExpectationEntity(value: '', expectedValue: null)],
16-
[new TransformerExpectationEntity(value: ' ', expectedValue: null)],
17-
[new TransformerExpectationEntity(value: ' asd ', expectedValue: 'asd')],
18-
[new TransformerExpectationEntity(value: 'asd ', expectedValue: 'asd')],
19-
[new TransformerExpectationEntity(value: 'asd mix', expectedValue: 'asd mix')],
15+
[new TransformerExpectationEntity(value: '', expectedValue: null, expectBeforeValidation: true)],
16+
[new TransformerExpectationEntity(value: ' ', expectedValue: null, expectBeforeValidation: true)],
17+
[new TransformerExpectationEntity(value: ' asd ', expectedValue: 'asd', expectBeforeValidation: true)],
18+
[new TransformerExpectationEntity(value: 'asd ', expectedValue: 'asd', expectBeforeValidation: true)],
19+
[
20+
new TransformerExpectationEntity(
21+
value: 'asd mix',
22+
expectedValue: 'asd mix',
23+
expectBeforeValidation: true
24+
),
25+
],
2026
];
2127
}
2228

0 commit comments

Comments
 (0)