Completed
Pull Request — master (#140)
by Simone
01:44
created

AllowedProperties::check()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 14
cts 14
cp 1
rs 8.5906
c 0
b 0
f 0
cc 6
eloc 13
nc 5
nop 1
crap 6
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 AllowedProperties implements Validator
18
{
19 27
    public function check(Resource $resource)
20
    {
21 27
        $allowed = array_merge(
22 27
            $resource->allowed(),
23 27
            $resource->mandatory()
24
        );
25
26 27
        foreach ($resource->properties() as $key => $value) {
27 19
            if (!in_array($key, $allowed)) {
28 3
                foreach ($allowed as $kk => $vv) {
29 3
                    if (!is_numeric($kk) && $resource->hasProperty($vv['when']['property'])) {
30 2
                        return;
31
                    }
32
                }
33
34 1
                throw new \Sensorario\Resources\Exceptions\NotAllowedKeyException(
35 1
                    "Key `" . get_class($resource)
36 1
                    . "::\$$key` with value `" . $value
37 1
                    . "` is not allowed"
38
                );
39
            }
40
        }
41 24
    }
42
}
43