1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebTheory\Saveyour\Controller\Builder; |
4
|
|
|
|
5
|
|
|
use WebTheory\Saveyour\Contracts\Controller\FormFieldControllerInterface; |
6
|
|
|
use WebTheory\Saveyour\Contracts\Data\FieldDataManagerInterface; |
7
|
|
|
use WebTheory\Saveyour\Contracts\Field\FormFieldInterface; |
8
|
|
|
use WebTheory\Saveyour\Contracts\Formatting\DataFormatterInterface; |
9
|
|
|
use WebTheory\Saveyour\Contracts\Validation\ValidatorInterface; |
10
|
|
|
use WebTheory\Saveyour\Controller\FormFieldController; |
11
|
|
|
|
12
|
|
|
class FormFieldControllerBuilder |
13
|
|
|
{ |
14
|
|
|
protected ?string $requestVar = null; |
15
|
|
|
|
16
|
|
|
protected ?FormFieldInterface $formField = null; |
17
|
|
|
|
18
|
|
|
protected ?FieldDataManagerInterface $dataManager = null; |
19
|
|
|
|
20
|
|
|
protected ?DataFormatterInterface $formatter = null; |
21
|
|
|
|
22
|
|
|
protected ?ValidatorInterface $validator = null; |
23
|
|
|
|
24
|
|
|
protected ?bool $isPermittedToProcess = true; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var null|string[] |
28
|
|
|
*/ |
29
|
|
|
protected ?array $mustAwait = null; |
30
|
|
|
|
31
|
51 |
|
public function __construct(string $requestVar = null) |
32
|
|
|
{ |
33
|
51 |
|
$this->requestVar = $requestVar ?? $this->requestVar; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
6 |
|
public function requestVar(string $requestVar): FormFieldControllerBuilder |
40
|
|
|
{ |
41
|
6 |
|
$this->requestVar = $requestVar; |
42
|
|
|
|
43
|
6 |
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
9 |
|
public function formField(?FormFieldInterface $formField): FormFieldControllerBuilder |
50
|
|
|
{ |
51
|
9 |
|
$this->formField = $formField; |
52
|
|
|
|
53
|
9 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
9 |
|
public function dataManager(?FieldDataManagerInterface $dataManager): FormFieldControllerBuilder |
60
|
|
|
{ |
61
|
9 |
|
$this->dataManager = $dataManager; |
62
|
|
|
|
63
|
9 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
9 |
|
public function validator(?ValidatorInterface $validator): FormFieldControllerBuilder |
70
|
|
|
{ |
71
|
9 |
|
$this->validator = $validator; |
72
|
|
|
|
73
|
9 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
9 |
|
public function formatter(?DataFormatterInterface $formatter): FormFieldControllerBuilder |
80
|
|
|
{ |
81
|
9 |
|
$this->formatter = $formatter; |
82
|
|
|
|
83
|
9 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
6 |
|
public function canProcess(bool $processingDisabled): FormFieldControllerBuilder |
90
|
|
|
{ |
91
|
6 |
|
$this->isPermittedToProcess = $processingDisabled; |
92
|
|
|
|
93
|
6 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
12 |
|
public function awaitAll(array $fields): FormFieldControllerBuilder |
100
|
|
|
{ |
101
|
12 |
|
array_map([$this, 'await'], $fields); |
102
|
|
|
|
103
|
12 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
15 |
|
public function await(string $field): FormFieldControllerBuilder |
110
|
|
|
{ |
111
|
15 |
|
$this->mustAwait[] = $field; |
112
|
|
|
|
113
|
15 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
24 |
|
public function get(): FormFieldControllerInterface |
117
|
|
|
{ |
118
|
24 |
|
return new FormFieldController( |
119
|
24 |
|
$this->requestVar, |
|
|
|
|
120
|
24 |
|
$this->formField, |
121
|
24 |
|
$this->dataManager, |
122
|
24 |
|
$this->validator, |
123
|
24 |
|
$this->formatter, |
124
|
24 |
|
$this->isPermittedToProcess, |
125
|
24 |
|
$this->mustAwait, |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
3 |
|
public static function for(string $requestVar): FormFieldControllerBuilder |
130
|
|
|
{ |
131
|
3 |
|
return new static($requestVar); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|