AllowedValues::check()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.7666
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
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 AllowedValues implements Validator
18
{
19 29
    public function check(Resource $resource)
20
    {
21 29
        foreach ($resource->properties() as $key => $value) {
22 20
            if (isset($resource->allowedValues()[$key])) {
23 4
                $allowedValues = $resource->allowedValues()[$key];
24 4
                if (!in_array($value, $allowedValues)) {
25 1
                    throw new \Sensorario\Resources\Exceptions\UnexpectedValueException(
26 1
                        'Value `' . $value . '` is not allowed '
27 1
                        . 'for key `' . $key . '`. '
28 20
                        . 'Allowed values are: ' . var_export($allowedValues, true)
29
                    );
30
                }
31
            }
32
        }
33 28
    }
34
}
35