|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebTheory\Saveyour\Processor\Abstracts; |
|
4
|
|
|
|
|
5
|
|
|
use WebTheory\Saveyour\Contracts\Processor\FormDataProcessorInterface; |
|
6
|
|
|
use WebTheory\Saveyour\Contracts\Report\ProcessedFieldReportInterface; |
|
7
|
|
|
use WebTheory\Saveyour\Contracts\Report\ProcessedInputReportInterface; |
|
8
|
|
|
|
|
9
|
|
|
abstract class AbstractFormDataProcessor implements FormDataProcessorInterface |
|
10
|
|
|
{ |
|
11
|
|
|
protected string $name; |
|
12
|
|
|
|
|
13
|
|
|
protected ?array $fields = null; |
|
14
|
|
|
|
|
15
|
9 |
|
public function __construct(string $name, ?array $fields) |
|
16
|
|
|
{ |
|
17
|
9 |
|
$this->name = $name; |
|
18
|
9 |
|
$this->setFields($fields); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function getName(): string |
|
22
|
|
|
{ |
|
23
|
|
|
return $this->name; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function getFields(): ?array |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->fields; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
9 |
|
protected function setFields(?array $fields): void |
|
32
|
|
|
{ |
|
33
|
9 |
|
if (empty($fields)) { |
|
34
|
|
|
$this->fields = $fields; |
|
35
|
|
|
} else { |
|
36
|
9 |
|
foreach ($fields as $field => $param) { |
|
37
|
9 |
|
$this->addField($field, $param); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
9 |
|
protected function addField(string $id, string $field): void |
|
43
|
|
|
{ |
|
44
|
9 |
|
$this->fields[$id] = $field; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param array<string,ProcessedFieldReportInterface> $results |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function hasReasonToProcess(array $results): bool |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->hasNoFields() || $this->valueUpdated($results); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function hasNoFields(): bool |
|
56
|
|
|
{ |
|
57
|
|
|
return is_array($fields = $this->getFields()) && empty($fields); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param array<string,ProcessedFieldReportInterface> $results |
|
62
|
|
|
*/ |
|
63
|
9 |
|
protected function valueUpdated(array $results): bool |
|
64
|
|
|
{ |
|
65
|
9 |
|
foreach ($results as $result) { |
|
66
|
9 |
|
if ($result->updateSuccessful()) { |
|
67
|
9 |
|
return true; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param array<string,ProcessedInputReportInterface> $results |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function allFieldsPresent(array $results): bool |
|
78
|
|
|
{ |
|
79
|
|
|
foreach ($this->fields ?? array_keys($results) as $field) { |
|
80
|
|
|
$field = $results[$field] ?? null; |
|
81
|
|
|
|
|
82
|
|
|
if (!$field || !$this->fieldIsPresentAndValid($field)) { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return true; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected function fieldIsPresentAndValid(ProcessedInputReportInterface $report): bool |
|
91
|
|
|
{ |
|
92
|
|
|
return $report->requestVarPresent() && $report->validationStatus(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param array<string,ProcessedInputReportInterface> $results |
|
97
|
|
|
*/ |
|
98
|
9 |
|
protected function extractValues(array $results): array |
|
99
|
|
|
{ |
|
100
|
9 |
|
$values = []; |
|
101
|
9 |
|
$keys = array_flip($this->fields); |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
9 |
|
foreach ($results as $id => $report) { |
|
104
|
9 |
|
$values[$keys[$id]] = $report->rawInputValue(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
9 |
|
return $values; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param array<string,ProcessedInputReportInterface> $results |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function mappedValues(array $results) |
|
114
|
|
|
{ |
|
115
|
|
|
$values = []; |
|
116
|
|
|
|
|
117
|
|
|
foreach ($results as $field => $report) { |
|
118
|
|
|
$values[$field] = $report->rawInputValue(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $values; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|