Skip to content

Commit 8b5d5bb

Browse files
committed
feat: allow abstract value to parse value before it is accepted
1 parent 265e0a8 commit 8b5d5bb

File tree

4 files changed

+63
-7
lines changed

4 files changed

+63
-7
lines changed

CHANGELOG.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ All notable changes to this project will be documented in this file. This projec
99

1010
- Minimum PHP version is now 8.2.
1111
- Added generics to the value interface, abstract class and trait. This will be breaking if you are running PHPStan.
12-
- **BREAKING**: The abstract value and value trait are now both readonly. This is because value objects should always be immutable.
12+
- **BREAKING**: The abstract value and value trait are now both readonly. This is because value objects should always be
13+
immutable.
14+
- **BREAKING**: The abstract value constructor is now final. If you need to parse the provided value before it is
15+
accepted, overload the `parse()` method in your class. Alternatively, don't extend the `AbstractValue` and use the
16+
`ValueTrait` instead.
1317

1418
## [2.2.0] - 2022-03-04
1519

@@ -51,10 +55,10 @@ This package now requires PHP 7.1 or above.
5155
## Added
5256

5357
- The following methods have been added to value objects:
54-
- `toString` for fluently casting the object to a string.
55-
- `isAny` to check if the value is one of any number of provided values.
56-
- `isEmpty` to check if the value is empty.
57-
- `isNotEmpty` to check if the value is not empty.
58+
- `toString` for fluently casting the object to a string.
59+
- `isAny` to check if the value is one of any number of provided values.
60+
- `isEmpty` to check if the value is empty.
61+
- `isNotEmpty` to check if the value is not empty.
5862

5963
## Deprecated
6064

@@ -65,8 +69,8 @@ This package now requires PHP 7.1 or above.
6569
### Added
6670

6771
- The following static methods have been added to both abstract classes:
68-
- `create` which is a fluent constructor.
69-
- `cast` which ensures the provided value is an instance of `static`.
72+
- `create` which is a fluent constructor.
73+
- `cast` which ensures the provided value is an instance of `static`.
7074

7175
## [1.0.0] - 2017-01-30
7276

src/AbstractValue.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,26 @@ public static function nullable(mixed $value): ?static
108108
*/
109109
final public function __construct(mixed $value)
110110
{
111+
$value = $this->parse($value);
112+
111113
if ($this->notAcceptable($value)) {
112114
throw new ValueException('Expecting a valid value.');
113115
}
114116

115117
$this->value = $value;
116118
}
117119

120+
/**
121+
* Allow the value to be parsed before it is accepted.
122+
*
123+
* @param mixed $value
124+
* @return mixed
125+
*/
126+
protected function parse(mixed $value): mixed
127+
{
128+
return $value;
129+
}
130+
118131
/**
119132
* Is the value not acceptable?
120133
*

tests/IntegerMultiplierValue.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2025 Cloud Creativity Limited
5+
*
6+
* Use of this source code is governed by an MIT-style
7+
* license that can be found in the LICENSE file or at
8+
* https://opensource.org/licenses/MIT.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace CloudCreativity\Utils\Value\Tests;
14+
15+
use CloudCreativity\Utils\Value\AbstractValue;
16+
17+
/**
18+
* @extends AbstractValue<int>
19+
*/
20+
final readonly class IntegerMultiplierValue extends AbstractValue
21+
{
22+
protected function parse(mixed $value): mixed
23+
{
24+
return is_int($value) ? $value * 10 : $value;
25+
}
26+
27+
protected function accept(mixed $value): bool
28+
{
29+
return is_int($value);
30+
}
31+
}

tests/Unit/ValueTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace CloudCreativity\Utils\Value\Tests\Unit;
1414

15+
use CloudCreativity\Utils\Value\Tests\IntegerMultiplierValue;
1516
use CloudCreativity\Utils\Value\Tests\IntegerValue;
1617
use CloudCreativity\Utils\Value\Tests\StringValue;
1718
use CloudCreativity\Utils\Value\ValueException;
@@ -114,4 +115,11 @@ public function testNotEmpty(): void
114115
$this->assertTrue($value->isNotEmpty());
115116
$this->assertFalse($value->isEmpty());
116117
}
118+
119+
public function testItCanSetParser(): void
120+
{
121+
$value = new IntegerMultiplierValue(2);
122+
123+
$this->assertSame(20, $value->get());
124+
}
117125
}

0 commit comments

Comments
 (0)