|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of sensorario/resources repository |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Simone Gentili <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sensorario\Resources\Validators\Validators; |
|
13
|
|
|
|
|
14
|
|
|
use Sensorario\Resources\Resource; |
|
15
|
|
|
use Sensorario\Resources\Validators\Interfaces\Validator; |
|
16
|
|
|
|
|
17
|
|
|
final class MandatoryConditional implements Validator |
|
18
|
|
|
{ |
|
19
|
32 |
|
public function check(Resource $resource) |
|
20
|
|
|
{ |
|
21
|
32 |
|
foreach ($resource->mandatory() as $key => $value) { |
|
22
|
19 |
|
if (isset($value['when']['has_value'])) { |
|
23
|
4 |
|
$name = $value['when']['property']; |
|
24
|
4 |
|
$value = $value['when']['has_value']; |
|
25
|
|
|
|
|
26
|
4 |
|
$this->ensurePropertyKey($resource, $value, $name, $key); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
30 |
|
} |
|
30
|
|
|
|
|
31
|
4 |
|
public function ensurePropertyNameHasKey(Resource $resource, $value, $name, $key) |
|
32
|
|
|
{ |
|
33
|
4 |
|
if ($resource->get($name) === $value && $resource->hasNotProperty($key)) { |
|
34
|
2 |
|
throw new \Sensorario\Resources\Exceptions\PropertyException( |
|
35
|
2 |
|
'When property `' . $name . '` ' |
|
36
|
2 |
|
. 'has value ' . '`' . $value . '` ' |
|
37
|
2 |
|
. 'also `' . $key . '` is mandatory' |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
3 |
|
} |
|
41
|
|
|
|
|
42
|
4 |
|
public function ensurePropertyKey($resource, $value, $name, $key) |
|
43
|
|
|
{ |
|
44
|
4 |
|
if (is_array($value)) { |
|
45
|
3 |
|
foreach ($value as $item) { |
|
46
|
3 |
|
$this->ensurePropertyNameHasKey($resource, $item, $name, $key); |
|
47
|
|
|
} |
|
48
|
|
|
} else { |
|
49
|
1 |
|
$this->ensurePropertyNameHasKey($resource, $value, $name, $key); |
|
50
|
|
|
} |
|
51
|
2 |
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|