-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetter.php
53 lines (49 loc) · 1.45 KB
/
Setter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace Bdf\Form\PropertyAccess;
use Attribute;
/**
* Set the property value using the element value
*
* <code>
* // Use the child name as property name
* $builder->hydrator(new Setter());
*
* // The property name is "myProp"
* $builder->hydrator(new Setter('myProp'));
*
* // Apply a transformation to the value
* $builder->hydrator(new Setter(function ($value, ChildInterface $input) {
* return doTransform($value);
* }));
*
* // Define property name and transformer
* $builder->hydrator(new Setter('myProp', function ($value, ChildInterface $input) {
* return doTransform($value);
* }));
*
* // Use a custom accessor. $mode is equals to HydratorInterface::HYDRATION
* $builder->hydrator(new Setter(null, null, function ($entity, $value, $mode, Setter $setter) {
* return $entity->myCustomSetter($value);
* }));
* </code>
*
* @see ChildBuilder::setter()
*/
#[Attribute(Attribute::TARGET_PROPERTY)]
final class Setter extends AbstractAccessor implements HydratorInterface
{
/**
* {@inheritdoc}
*/
public function hydrate(&$target, $value): void
{
if ($this->transformer) {
$value = ($this->transformer)($value, $this->input);
}
if ($this->customAccessor !== null) {
($this->customAccessor)($target, $value, self::HYDRATION, $this);
} else {
$this->propertyAccessor->setValue($target, $this->prepareAccessorPath($target), $value);
}
}
}