|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace midcom\datamanager\validation; |
|
7
|
|
|
|
|
8
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
|
9
|
|
|
use midcom\datamanager\storage\container\container; |
|
10
|
|
|
|
|
11
|
|
|
class callback |
|
12
|
|
|
{ |
|
13
|
|
|
private array $validation; |
|
14
|
|
|
|
|
15
|
19 |
|
public function __construct(array $validation) |
|
16
|
|
|
{ |
|
17
|
19 |
|
$this->validation = $validation; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
8 |
|
public function validate($payload, ExecutionContextInterface $context) |
|
21
|
|
|
{ |
|
22
|
8 |
|
foreach ($this->validation as $entry) { |
|
23
|
8 |
|
if (!empty($entry['callback'])) { |
|
24
|
8 |
|
$form = $context->getObject(); |
|
25
|
8 |
|
$result = $entry['callback']($this->to_array($form->getData())); |
|
26
|
8 |
|
if (is_array($result)) { |
|
27
|
|
|
$this->process_result($context, $result); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
private function process_result(ExecutionContextInterface $context, $result, array $path = []) |
|
34
|
|
|
{ |
|
35
|
|
|
if (is_string($result)) { |
|
36
|
|
|
$context |
|
37
|
|
|
->buildViolation($result) |
|
38
|
|
|
->atPath('[' . implode('][', $path) . ']') |
|
39
|
|
|
->addViolation(); |
|
40
|
|
|
} else { |
|
41
|
|
|
foreach ($result as $field => $message) { |
|
42
|
|
|
$childpath = $path; |
|
43
|
|
|
$childpath[] = $field; |
|
44
|
|
|
$this->process_result($context, $message, $childpath); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param container|array $container |
|
51
|
|
|
* @return array |
|
52
|
|
|
*/ |
|
53
|
8 |
|
private function to_array($container) : array |
|
54
|
|
|
{ |
|
55
|
8 |
|
if (is_array($container)) { |
|
56
|
|
|
// This is a newly added subform, not yet saved, so it's already in view format |
|
57
|
|
|
return $container; |
|
58
|
|
|
} |
|
59
|
8 |
|
$data = []; |
|
60
|
|
|
|
|
61
|
8 |
|
foreach ($container as $field => $value) { |
|
62
|
8 |
|
$data[$field] = $value->get_value(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
8 |
|
return $data; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|