Passed
Push — master ( 638409...86ecbf )
by
unknown
04:13 queued 01:51
created

EnumValidator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 28
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A validate() 0 4 1
A validateValue() 0 12 4
1
<?php
2
namespace Fathomminds\Rest\Schema\TypeValidators;
3
4
use Fathomminds\Rest\Exceptions\RestException;
5
6
class EnumValidator extends StdTypeValidator
7
{
8
    protected $validValues = [];
9
10 1
    public function __construct($params = [])
11
    {
12 1
        parent::__construct($params);
13 1
        $this->validValues = isset($params['validValues']) ? $params['validValues'] : [];
14 1
    }
15
16 1
    public function validate($value)
17
    {
18 1
        $this->validateValue($value);
19 1
    }
20
21 1
    private function validateValue($value)
22
    {
23 1
        if (!(in_array($value, $this->validValues, true) || ($value === null && $this->nullable))) {
24 1
            throw new RestException(
25 1
                'Value is not in valid value list',
26
                [
27 1
                    'value' => $value,
28 1
                    'validValues' => $this->validValues,
29
                ]
30
            );
31
        }
32 1
    }
33
}
34