|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebTheory\Saveyour\Processors; |
|
4
|
|
|
|
|
5
|
|
|
use WebTheory\Saveyour\Contracts\FieldOperationCacheInterface; |
|
6
|
|
|
use WebTheory\Saveyour\Contracts\FormDataProcessorInterface; |
|
7
|
|
|
|
|
8
|
|
|
abstract class AbstractFormDataProcessor implements FormDataProcessorInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var array |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $fields = []; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Get the value of fields |
|
17
|
|
|
* |
|
18
|
|
|
* @return mixed |
|
19
|
|
|
*/ |
|
20
|
6 |
|
public function getFields(): array |
|
21
|
|
|
{ |
|
22
|
6 |
|
return $this->fields; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
3 |
|
public function getField(string $field) |
|
29
|
|
|
{ |
|
30
|
3 |
|
return $this->fields[$field]; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Set the value of fields |
|
35
|
|
|
* |
|
36
|
|
|
* @param mixed $fields |
|
37
|
|
|
* |
|
38
|
|
|
* @return self |
|
39
|
|
|
*/ |
|
40
|
6 |
|
public function setFields(array $fields) |
|
41
|
|
|
{ |
|
42
|
6 |
|
foreach ($fields as $field => $param) { |
|
43
|
6 |
|
$this->addField($field, $param); |
|
44
|
|
|
} |
|
45
|
6 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $id |
|
49
|
|
|
* @param string $field |
|
50
|
|
|
*/ |
|
51
|
12 |
|
public function addField(string $id, string $field) |
|
52
|
|
|
{ |
|
53
|
12 |
|
$this->fields[$id] = $field; |
|
54
|
|
|
|
|
55
|
12 |
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param FieldOperationCacheInterface[] $results |
|
60
|
|
|
*/ |
|
61
|
12 |
|
protected function valueUpdated(array $results): bool |
|
62
|
|
|
{ |
|
63
|
12 |
|
foreach ($results as $result) { |
|
64
|
|
|
|
|
65
|
12 |
|
if (true === $result->updateSuccessful()) { |
|
66
|
6 |
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
9 |
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param FieldOperationCacheInterface[] $results |
|
75
|
|
|
*/ |
|
76
|
9 |
|
protected function allFieldsPresent(array $results): bool |
|
77
|
|
|
{ |
|
78
|
9 |
|
foreach ($this->fields as $field) { |
|
79
|
9 |
|
if (!isset($results[$field])) { |
|
80
|
6 |
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
9 |
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param FieldOperationCacheInterface[] $results |
|
89
|
|
|
*/ |
|
90
|
12 |
|
protected function extractValues(array $results): array |
|
91
|
|
|
{ |
|
92
|
12 |
|
$values = []; |
|
93
|
12 |
|
$keys = array_flip($this->fields); |
|
94
|
|
|
|
|
95
|
12 |
|
foreach ($results as $id => $cache) { |
|
96
|
|
|
|
|
97
|
12 |
|
$values[$keys[$id]] = $cache->sanitizedInputValue(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
12 |
|
return $values; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|