1
|
|
|
<?php |
2
|
|
|
namespace rtens\domin\delivery\web; |
3
|
|
|
|
4
|
|
|
use rtens\domin\Action; |
5
|
|
|
use rtens\domin\Parameter; |
6
|
|
|
use watoki\curir\delivery\WebRequest; |
7
|
|
|
use watoki\reflect\type\ClassType; |
8
|
|
|
|
9
|
|
|
class ActionForm { |
10
|
|
|
|
11
|
|
|
/** @var array */ |
12
|
|
|
private $model; |
13
|
|
|
|
14
|
|
|
/** @var array|Element[] */ |
15
|
|
|
private $headElements = []; |
16
|
|
|
|
17
|
|
|
public function __construct(WebRequest $request, WebApplication $app, Action $action, $actionId) { |
18
|
|
|
$actionParameter = new Parameter($actionId, new ClassType(get_class($action))); |
19
|
|
|
$actionField = $this->getActionField($actionParameter, $app); |
20
|
|
|
|
21
|
|
|
$this->model = $actionField->render($actionParameter, $this->readParameters($request, $app, $action)); |
|
|
|
|
22
|
|
|
$this->headElements = $actionField->headElements($actionParameter); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function getModel() { |
26
|
|
|
return $this->model; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getHeadElements() { |
30
|
|
|
return $this->headElements; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Parameter $actionParameter |
35
|
|
|
* @param WebApplication $app |
36
|
|
|
* @return WebField |
37
|
|
|
* @throws \Exception |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
private function getActionField(Parameter $actionParameter, WebApplication $app) { |
|
|
|
|
40
|
|
|
$actionField = $app->fields->getField($actionParameter); |
41
|
|
|
if ($actionField instanceof WebField) { |
42
|
|
|
return $actionField; |
43
|
|
|
} |
44
|
|
|
throw new \Exception(get_class($actionField) . " must implement WebField"); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function readParameters(WebRequest $request, WebApplication $app, Action $action) { |
48
|
|
|
$reader = new RequestParameterReader($request); |
49
|
|
|
$values = []; |
50
|
|
|
|
51
|
|
|
foreach ($action->parameters() as $parameter) { |
52
|
|
|
if ($reader->has($parameter)) { |
53
|
|
|
$field = $app->fields->getField($parameter); |
54
|
|
|
$values[$parameter->getName()] = $field->inflate($parameter, $reader->read($parameter)); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
return $values; |
58
|
|
|
} |
59
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..