Completed
Push — master ( 0c0165...044fcf )
by Simone
01:52
created

MandatoryConditional::ensurePropertyKey()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 4
crap 3
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