1
|
|
|
<?php |
2
|
|
|
namespace rtens\domin\delivery\web\fields; |
3
|
|
|
|
4
|
|
|
use rtens\domin\delivery\web\Element; |
5
|
|
|
use rtens\domin\delivery\web\WebField; |
6
|
|
|
use rtens\domin\Parameter; |
7
|
|
|
|
8
|
|
|
class AdapterField implements WebField { |
9
|
|
|
|
10
|
|
|
/** @var WebField */ |
11
|
|
|
private $field; |
12
|
|
|
|
13
|
|
|
/** @var callable */ |
14
|
|
|
private $handles; |
15
|
|
|
|
16
|
|
|
/** @var callable */ |
17
|
|
|
private $afterInflate; |
18
|
|
|
|
19
|
|
|
/** @var callable */ |
20
|
|
|
private $beforeRender; |
21
|
|
|
|
22
|
|
|
/** @var callable */ |
23
|
|
|
private $afterHeadElements; |
24
|
|
|
|
25
|
|
|
/** @var callable */ |
26
|
|
|
private $transformParameter; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param WebField $field |
30
|
|
|
*/ |
31
|
|
|
public function __construct(WebField $field) { |
32
|
|
|
$this->field = $field; |
33
|
|
|
|
34
|
|
|
$this->handles = function (Parameter $parameter) { |
35
|
|
|
return $this->field->handles($parameter); |
36
|
|
|
}; |
37
|
|
|
$this->beforeRender = function ($value) { |
38
|
|
|
return $value; |
39
|
|
|
}; |
40
|
|
|
$this->afterInflate = function ($value) { |
41
|
|
|
return $value; |
42
|
|
|
}; |
43
|
|
|
$this->afterHeadElements = function ($elements) { |
44
|
|
|
return $elements; |
45
|
|
|
}; |
46
|
|
|
$this->transformParameter = function ($parameter) { |
47
|
|
|
return $parameter; |
48
|
|
|
}; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param Parameter $parameter |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
public function handles(Parameter $parameter) { |
56
|
|
|
return call_user_func($this->handles, $parameter); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Parameter $parameter |
61
|
|
|
* @param string $serialized |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
public function inflate(Parameter $parameter, $serialized) { |
65
|
|
|
$parameter = call_user_func($this->transformParameter, $parameter); |
66
|
|
|
$inflated = $this->field->inflate($parameter, $serialized); |
67
|
|
|
|
68
|
|
|
return call_user_func($this->afterInflate, $inflated); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Parameter $parameter |
73
|
|
|
* @param string $value |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function render(Parameter $parameter, $value) { |
77
|
|
|
$parameter = call_user_func($this->transformParameter, $parameter); |
78
|
|
|
$value = call_user_func($this->beforeRender, $value); |
79
|
|
|
|
80
|
|
|
return $this->field->render($parameter, $value); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Parameter $parameter |
85
|
|
|
* @return array|Element[] |
86
|
|
|
*/ |
87
|
|
|
public function headElements(Parameter $parameter) { |
88
|
|
|
$parameter = call_user_func($this->transformParameter, $parameter); |
89
|
|
|
$elements = $this->field->headElements($parameter); |
90
|
|
|
|
91
|
|
|
return call_user_func($this->afterHeadElements, $elements); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param callable $handles |
96
|
|
|
* @return AdapterField |
97
|
|
|
*/ |
98
|
|
|
public function setHandles($handles) { |
99
|
|
|
$this->handles = $handles; |
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $name |
105
|
|
|
* @return static |
106
|
|
|
*/ |
107
|
|
|
public function setHandlesParameterName($name) { |
108
|
|
|
$this->setHandles(function (Parameter $parameter) use ($name) { |
109
|
|
|
return $parameter->getName() == $name; |
110
|
|
|
}); |
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param callable $afterInflate |
116
|
|
|
* @return AdapterField |
117
|
|
|
*/ |
118
|
|
|
public function setAfterInflate($afterInflate) { |
119
|
|
|
$this->afterInflate = $afterInflate; |
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param callable $beforeRender |
125
|
|
|
* @return AdapterField |
126
|
|
|
*/ |
127
|
|
|
public function setBeforeRender($beforeRender) { |
128
|
|
|
$this->beforeRender = $beforeRender; |
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param callable $transformParameter |
134
|
|
|
* @return AdapterField |
135
|
|
|
*/ |
136
|
|
|
public function setTransformParameter($transformParameter) { |
137
|
|
|
$this->transformParameter = $transformParameter; |
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param callable $afterHeadElements |
143
|
|
|
* @return AdapterField |
144
|
|
|
*/ |
145
|
|
|
public function setAfterHeadElements($afterHeadElements) { |
146
|
|
|
$this->afterHeadElements = $afterHeadElements; |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
} |