1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TreeHouse\Model\Config\Validator\Constraints; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Validator\Constraint; |
6
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
7
|
|
|
use Symfony\Component\Validator\Exception\InvalidArgumentException; |
8
|
|
|
use TreeHouse\Model\Config\Config; |
9
|
|
|
|
10
|
|
|
class ConfigValueValidator extends ConstraintValidator |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Config |
14
|
|
|
*/ |
15
|
|
|
protected $config; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param Config $config |
19
|
|
|
*/ |
20
|
18 |
|
public function __construct(Config $config) |
21
|
|
|
{ |
22
|
18 |
|
$this->config = $config; |
23
|
18 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param integer|integer[] $value |
27
|
|
|
* @param Constraint $constraint |
28
|
|
|
* |
29
|
|
|
* @throws InvalidArgumentException |
30
|
|
|
*/ |
31
|
18 |
|
public function validate($value, Constraint $constraint) |
32
|
|
|
{ |
33
|
|
|
/* @var $constraint ConfigValue */ |
34
|
18 |
|
$name = $constraint->name; |
35
|
|
|
|
36
|
|
|
// config must exist |
37
|
18 |
|
if (!$this->config->hasFieldConfig($name)) { |
38
|
2 |
|
throw new InvalidArgumentException(sprintf('There is no config for the field "%s"', $name)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// value could be null |
42
|
16 |
|
if (null === $value) { |
43
|
2 |
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// check if field is multivalued |
47
|
14 |
|
$multiValued = $this->config->isMultiValued($name); |
48
|
|
|
|
49
|
|
|
// if it's multivalued, an array must be given |
50
|
14 |
|
if ($multiValued) { |
51
|
6 |
|
if (!is_array($value)) { |
52
|
2 |
|
$this->context->addViolation($constraint->arrayMessage); |
53
|
|
|
|
54
|
2 |
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// enforce array |
58
|
4 |
|
foreach ($value as $key) { |
59
|
4 |
|
$this->validateFieldConfig($constraint, $name, $key); |
60
|
4 |
|
} |
61
|
4 |
|
} else { |
62
|
8 |
|
$this->validateFieldConfig($constraint, $name, $value); |
|
|
|
|
63
|
|
|
} |
64
|
12 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param ConfigValue $constraint |
68
|
|
|
* @param string $name |
69
|
|
|
* @param integer $value |
70
|
|
|
*/ |
71
|
12 |
|
protected function validateFieldConfig(ConfigValue $constraint, $name, $value) |
72
|
|
|
{ |
73
|
|
|
// if it's single valued, value must be numeric |
74
|
12 |
|
if (!is_numeric($value)) { |
75
|
2 |
|
$this->context->addViolation($constraint->numericMessage, ['{{ value }}' => var_export($value, true)]); |
76
|
|
|
|
77
|
2 |
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// enforce array |
81
|
10 |
|
if (!$this->config->hasFieldConfigKey($name, intval($value))) { |
82
|
4 |
|
$this->context->addViolation($constraint->message, ['{{ value }}' => $value]); |
83
|
4 |
|
} |
84
|
10 |
|
} |
85
|
|
|
} |
86
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.