-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstructorHandler.php
125 lines (110 loc) · 3.27 KB
/
ConstructorHandler.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* PHP version 8.1
*
* @package Saschati\ValueObject\Scope\Handlers
*/
namespace Saschati\ValueObject\Scope\Handlers;
use Closure;
use yii\db\ActiveRecordInterface;
use function array_map;
/**
* Class ConstructorHandler
*
* This handler creates an object with constructor values that can be properties of the ActiveRecord model,
* a "resolver" must be provided for data normalization.
*
* @method void resolver(object|null $type, ActiveRecordInterface $model, AbstractHandler $handler) A mandatory
* function that must map values from the type to the ActiveRecord model.
* @method boolean nullIf(ActiveRecordInterface $model, AbstractHandler $handler) If the method returns true then null
* will be passed to the specified property.
*
* Example:
* 'attribute' => [
* 'scope' => TypeScope::CONSTRUCTOR,
* 'type' => SomeClass::class,
* 'params' => [
* 'attribute1',
* 'attribute2',
* 'no attribute value',
* ...
* ],
* 'resolver' => static function (SomeEntity|null $type, ActiveRecordInterface $model, ValueInterface $handler) {
* $model->attribute1 = $type->getProperty1Value();
* ...
* },
* 'nullIf' => static function (ActiveRecordInterface $model, ValueInterface $handler) {
* return $model->attribute1 === null;
* },
* ]
*/
class ConstructorHandler extends AbstractHandler
{
/**
* @param ActiveRecordInterface $model
* @param string $attribute
* @param string $class
* @param array $params
* @param Closure $resolver
* @param Closure|null $nullIf
*/
public function __construct(
private readonly ActiveRecordInterface $model,
private readonly string $attribute,
private readonly string $class,
private readonly array $params,
private readonly Closure $resolver,
private readonly ?Closure $nullIf = null,
) {
}
/**
* @return void
*/
public function cast(): void
{
$class = $this->class;
$model = $this->model;
$nullIf = $this->nullIf;
if ($nullIf !== null && $nullIf($model, $this) === true) {
$this->setValue(null);
return;
}
$instance = new $class(
...array_map(
function (string $attribute) use ($model) {
if ($this->isVirtual($attribute) === true) {
return $this->bug->get($attribute);
}
if ($model->hasAttribute($attribute) === false) {
return $attribute;
}
return $model->{$attribute};
},
$this->params
)
);
$this->setValue($instance);
}
/**
* @return void
*/
public function normalize(): void
{
$resolver = $this->resolver;
$resolver($this->getValue(), $this->getModel(), $this);
}
/**
* @return ActiveRecordInterface
*/
protected function getModel(): ActiveRecordInterface
{
return $this->model;
}
/**
* @return string
*/
protected function getProperty(): string
{
return $this->attribute;
}
}