Skip to content

Commit d3b1dcb

Browse files
committed
feat!: refactor tree node API
The class `\CuyZ\Valinor\Mapper\Tree\Node` has been refactored to remove access to unwanted methods that were not supposed to be part of the public API. Below are a list of all changes: - New methods `$node->sourceFilled()` and `$node->sourceValue()` allow accessing the source value. - The method `$node->value()` has been renamed to `$node->mappedValue()` and will throw an exception if the node is not value. - The method `$node->type()` now returns a string. - The methods `$message->name()`, `$message->path()`, `$message->type()` and `$message->value()` have been deprecated in favor of the new method `$message->node()`. - The message parameter `{original_value}` has been deprecated in favor of `{source_value}`.
1 parent 316d919 commit d3b1dcb

39 files changed

Lines changed: 761 additions & 454 deletions

docs/pages/message-customization.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ on the original message.
1212
| `{node_name}` | name of the node to which the message is bound |
1313
| `{node_path}` | path of the node to which the message is bound |
1414
| `{node_type}` | type of the node to which the message is bound |
15-
| `{original_value}` | the source value that was given to the node |
15+
| `{source_value}` | the source value that was given to the node |
1616
| `{original_message}` | the original message before being customized |
1717

1818
Usage:
@@ -46,9 +46,9 @@ try {
4646
} catch (\CuyZ\Valinor\Mapper\MappingError $error) {
4747
$message = $error->node()->messages()[0];
4848

49-
if (is_numeric($message->value())) {
49+
if (is_numeric($message->node()->mappedValue())) {
5050
$message = $message->withBody(
51-
'Invalid amount {original_value, number, currency}'
51+
'Invalid amount {source_value, number, currency}'
5252
);
5353
}
5454

@@ -133,7 +133,7 @@ In any case, the content can contain placeholders as described
133133
'Some message content' => 'New content / previous: {original_message}',
134134

135135
// Will match if the given message is an instance of `SomeError`
136-
SomeError::class => 'New content / value: {original_value}',
136+
SomeError::class => 'New content / value: {source_value}',
137137

138138
// A callback can be used to get access to the message instance
139139
OtherError::class => function (NodeMessage $message): string {

src/Mapper/Tree/Builder/ArrayNodeBuilder.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66

77
use CuyZ\Valinor\Mapper\Tree\Exception\InvalidTraversableKey;
88
use CuyZ\Valinor\Mapper\Tree\Exception\SourceMustBeIterable;
9-
use CuyZ\Valinor\Mapper\Tree\Node;
109
use CuyZ\Valinor\Mapper\Tree\Shell;
1110
use CuyZ\Valinor\Type\CompositeTraversableType;
12-
1311
use CuyZ\Valinor\Type\Types\ArrayType;
14-
1512
use CuyZ\Valinor\Type\Types\IterableType;
1613
use CuyZ\Valinor\Type\Types\NonEmptyArrayType;
1714

@@ -28,15 +25,15 @@ public function __construct(bool $flexible)
2825
$this->flexible = $flexible;
2926
}
3027

31-
public function build(Shell $shell, RootNodeBuilder $rootBuilder): Node
28+
public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
3229
{
3330
$type = $shell->type();
3431
$value = $shell->hasValue() ? $shell->value() : null;
3532

3633
assert($type instanceof ArrayType || $type instanceof NonEmptyArrayType || $type instanceof IterableType);
3734

3835
if (null === $value && $this->flexible) {
39-
return Node::branch($shell, [], []);
36+
return TreeNode::branch($shell, [], []);
4037
}
4138

4239
if (! is_array($value)) {
@@ -46,11 +43,11 @@ public function build(Shell $shell, RootNodeBuilder $rootBuilder): Node
4643
$children = $this->children($type, $shell, $rootBuilder);
4744
$array = $this->buildArray($children);
4845

49-
return Node::branch($shell, $array, $children);
46+
return TreeNode::branch($shell, $array, $children);
5047
}
5148

5249
/**
53-
* @return array<Node>
50+
* @return array<TreeNode>
5451
*/
5552
private function children(CompositeTraversableType $type, Shell $shell, RootNodeBuilder $rootBuilder): array
5653
{
@@ -74,7 +71,7 @@ private function children(CompositeTraversableType $type, Shell $shell, RootNode
7471
}
7572

7673
/**
77-
* @param array<Node> $children
74+
* @param array<TreeNode> $children
7875
* @return mixed[]|null
7976
*/
8077
private function buildArray(array $children): ?array

src/Mapper/Tree/Builder/CasterNodeBuilder.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace CuyZ\Valinor\Mapper\Tree\Builder;
66

77
use CuyZ\Valinor\Mapper\Tree\Exception\NoCasterForType;
8-
use CuyZ\Valinor\Mapper\Tree\Node;
98
use CuyZ\Valinor\Mapper\Tree\Shell;
109

1110
/** @internal */
@@ -22,7 +21,7 @@ public function __construct(array $builders)
2221
$this->builders = $builders;
2322
}
2423

25-
public function build(Shell $shell, RootNodeBuilder $rootBuilder): Node
24+
public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
2625
{
2726
$type = $shell->type();
2827

src/Mapper/Tree/Builder/CasterProxyNodeBuilder.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace CuyZ\Valinor\Mapper\Tree\Builder;
66

7-
use CuyZ\Valinor\Mapper\Tree\Node;
87
use CuyZ\Valinor\Mapper\Tree\Shell;
98

109
/** @internal */
@@ -17,13 +16,13 @@ public function __construct(NodeBuilder $delegate)
1716
$this->delegate = $delegate;
1817
}
1918

20-
public function build(Shell $shell, RootNodeBuilder $rootBuilder): Node
19+
public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
2120
{
2221
if ($shell->hasValue()) {
2322
$value = $shell->value();
2423

2524
if ($shell->type()->accepts($value)) {
26-
return Node::leaf($shell, $value);
25+
return TreeNode::leaf($shell, $value);
2726
}
2827
}
2928

src/Mapper/Tree/Builder/ClassNodeBuilder.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use CuyZ\Valinor\Mapper\Object\FilteredObjectBuilder;
1111
use CuyZ\Valinor\Mapper\Object\ObjectBuilder;
1212
use CuyZ\Valinor\Mapper\Tree\Exception\UnexpectedArrayKeysForClass;
13-
use CuyZ\Valinor\Mapper\Tree\Node;
1413
use CuyZ\Valinor\Mapper\Tree\Shell;
1514
use CuyZ\Valinor\Type\Type;
1615
use CuyZ\Valinor\Type\Types\ClassType;
@@ -41,7 +40,7 @@ public function __construct(
4140
$this->flexible = $flexible;
4241
}
4342

44-
public function build(Shell $shell, RootNodeBuilder $rootBuilder): Node
43+
public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
4544
{
4645
$classTypes = $this->classTypes($shell->type());
4746

@@ -70,7 +69,7 @@ public function build(Shell $shell, RootNodeBuilder $rootBuilder): Node
7069

7170
$object = $this->buildObject($builder, $children);
7271

73-
$node = Node::branch($shell, $object, $children);
72+
$node = TreeNode::branch($shell, $object, $children);
7473

7574
if (! $this->flexible) {
7675
$node = $this->checkForUnexpectedKeys($arguments, $node);
@@ -109,7 +108,7 @@ private function builder(Shell $shell, ClassType ...$classTypes): ObjectBuilder
109108
}
110109

111110
/**
112-
* @param Node[] $children
111+
* @param TreeNode[] $children
113112
*/
114113
private function buildObject(ObjectBuilder $builder, array $children): ?object
115114
{
@@ -126,7 +125,7 @@ private function buildObject(ObjectBuilder $builder, array $children): ?object
126125
return $builder->build($arguments);
127126
}
128127

129-
private function checkForUnexpectedKeys(FilledArguments $arguments, Node $node): Node
128+
private function checkForUnexpectedKeys(FilledArguments $arguments, TreeNode $node): TreeNode
130129
{
131130
$superfluousKeys = $arguments->superfluousKeys();
132131

src/Mapper/Tree/Builder/EnumNodeBuilder.php

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

77
use BackedEnum;
88
use CuyZ\Valinor\Mapper\Tree\Exception\InvalidEnumValue;
9-
use CuyZ\Valinor\Mapper\Tree\Node;
109
use CuyZ\Valinor\Mapper\Tree\Shell;
1110
use CuyZ\Valinor\Type\Types\EnumType;
1211
use Stringable;
@@ -28,7 +27,7 @@ public function __construct(bool $flexible)
2827
$this->flexible = $flexible;
2928
}
3029

31-
public function build(Shell $shell, RootNodeBuilder $rootBuilder): Node
30+
public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
3231
{
3332
$type = $shell->type();
3433
$value = $shell->value();
@@ -37,7 +36,7 @@ public function build(Shell $shell, RootNodeBuilder $rootBuilder): Node
3736

3837
foreach ($type->className()::cases() as $case) {
3938
if ($this->valueMatchesEnumCase($value, $case)) {
40-
return Node::leaf($shell, $case);
39+
return TreeNode::leaf($shell, $case);
4140
}
4241
}
4342

src/Mapper/Tree/Builder/ErrorCatcherNodeBuilder.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use CuyZ\Valinor\Mapper\Tree\Message\ErrorMessage;
88
use CuyZ\Valinor\Mapper\Tree\Message\Message;
99
use CuyZ\Valinor\Mapper\Tree\Message\UserlandError;
10-
use CuyZ\Valinor\Mapper\Tree\Node;
1110
use CuyZ\Valinor\Mapper\Tree\Shell;
1211
use Throwable;
1312

@@ -28,7 +27,7 @@ public function __construct(NodeBuilder $delegate, callable $exceptionFilter)
2827
$this->exceptionFilter = $exceptionFilter;
2928
}
3029

31-
public function build(Shell $shell, RootNodeBuilder $rootBuilder): Node
30+
public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
3231
{
3332
try {
3433
return $this->delegate->build($shell, $rootBuilder);
@@ -37,7 +36,7 @@ public function build(Shell $shell, RootNodeBuilder $rootBuilder): Node
3736
$exception = ($this->exceptionFilter)($exception->previous());
3837
}
3938

40-
return Node::error($shell, $exception);
39+
return TreeNode::error($shell, $exception);
4140
}
4241
}
4342
}

src/Mapper/Tree/Builder/InterfaceNodeBuilder.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use CuyZ\Valinor\Mapper\Object\FilledArguments;
1111
use CuyZ\Valinor\Mapper\Tree\Exception\ObjectImplementationCallbackError;
1212
use CuyZ\Valinor\Mapper\Tree\Message\UserlandError;
13-
use CuyZ\Valinor\Mapper\Tree\Node;
1413
use CuyZ\Valinor\Mapper\Tree\Shell;
1514
use CuyZ\Valinor\Type\Types\ClassType;
1615
use CuyZ\Valinor\Type\Types\InterfaceType;
@@ -44,7 +43,7 @@ public function __construct(
4443
$this->flexible = $flexible;
4544
}
4645

47-
public function build(Shell $shell, RootNodeBuilder $rootBuilder): Node
46+
public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
4847
{
4948
$type = $shell->type();
5049

@@ -63,7 +62,7 @@ public function build(Shell $shell, RootNodeBuilder $rootBuilder): Node
6362

6463
foreach ($children as $child) {
6564
if (! $child->isValid()) {
66-
return Node::branch($shell, null, $children);
65+
return TreeNode::branch($shell, null, $children);
6766
}
6867

6968
$values[] = $child->value();
@@ -82,7 +81,7 @@ public function build(Shell $shell, RootNodeBuilder $rootBuilder): Node
8281
}
8382

8483
/**
85-
* @return Node[]
84+
* @return TreeNode[]
8685
*/
8786
private function children(Shell $shell, FilledArguments $arguments, RootNodeBuilder $rootBuilder): array
8887
{

src/Mapper/Tree/Builder/IterableNodeBuilder.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace CuyZ\Valinor\Mapper\Tree\Builder;
66

7-
use CuyZ\Valinor\Mapper\Tree\Node;
87
use CuyZ\Valinor\Mapper\Tree\Shell;
98

109
use function is_array;
@@ -21,7 +20,7 @@ public function __construct(NodeBuilder $delegate)
2120
$this->delegate = $delegate;
2221
}
2322

24-
public function build(Shell $shell, RootNodeBuilder $rootBuilder): Node
23+
public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
2524
{
2625
if ($shell->hasValue()) {
2726
$value = $shell->value();

src/Mapper/Tree/Builder/ListNodeBuilder.php

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

77
use CuyZ\Valinor\Mapper\Tree\Exception\InvalidListKey;
88
use CuyZ\Valinor\Mapper\Tree\Exception\SourceMustBeIterable;
9-
use CuyZ\Valinor\Mapper\Tree\Node;
109
use CuyZ\Valinor\Mapper\Tree\Shell;
1110
use CuyZ\Valinor\Type\CompositeTraversableType;
1211
use CuyZ\Valinor\Type\Types\ListType;
@@ -25,15 +24,15 @@ public function __construct(bool $flexible)
2524
$this->flexible = $flexible;
2625
}
2726

28-
public function build(Shell $shell, RootNodeBuilder $rootBuilder): Node
27+
public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
2928
{
3029
$type = $shell->type();
3130
$value = $shell->hasValue() ? $shell->value() : null;
3231

3332
assert($type instanceof ListType || $type instanceof NonEmptyListType);
3433

3534
if (null === $value && $this->flexible) {
36-
return Node::branch($shell, [], []);
35+
return TreeNode::branch($shell, [], []);
3736
}
3837

3938
if (! is_array($value)) {
@@ -43,11 +42,11 @@ public function build(Shell $shell, RootNodeBuilder $rootBuilder): Node
4342
$children = $this->children($type, $shell, $rootBuilder);
4443
$array = $this->buildArray($children);
4544

46-
return Node::branch($shell, $array, $children);
45+
return TreeNode::branch($shell, $array, $children);
4746
}
4847

4948
/**
50-
* @return array<Node>
49+
* @return array<TreeNode>
5150
*/
5251
private function children(CompositeTraversableType $type, Shell $shell, RootNodeBuilder $rootBuilder): array
5352
{
@@ -64,7 +63,7 @@ private function children(CompositeTraversableType $type, Shell $shell, RootNode
6463
$children[$expected] = $rootBuilder->build($child->withValue($value));
6564
} else {
6665
$child = $shell->child((string)$key, $subType);
67-
$children[$key] = Node::error($child, new InvalidListKey($key, $expected));
66+
$children[$key] = TreeNode::error($child, new InvalidListKey($key, $expected));
6867
}
6968

7069
$expected++;
@@ -74,7 +73,7 @@ private function children(CompositeTraversableType $type, Shell $shell, RootNode
7473
}
7574

7675
/**
77-
* @param array<Node> $children
76+
* @param array<TreeNode> $children
7877
* @return mixed[]|null
7978
*/
8079
private function buildArray(array $children): ?array

0 commit comments

Comments
 (0)