AbstractRangeConstraint::normalize()   B
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 25
c 0
b 0
f 0
ccs 15
cts 15
cp 1
rs 8.439
cc 5
eloc 13
nc 7
nop 3
crap 5
1
<?php
2
3
/*
4
 * This file is part of the JVal package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace JVal\Constraint;
11
12
use JVal\Constraint;
13
use JVal\Context;
14
use JVal\Exception\Constraint\InvalidTypeException;
15
use JVal\Exception\Constraint\MissingKeywordException;
16
use JVal\Types;
17
use JVal\Walker;
18
use stdClass;
19
20
/**
21
 * Base class for constraints based on a numeric limit.
22
 */
23
abstract class AbstractRangeConstraint implements Constraint
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 357
    public function supports($type)
29
    {
30
        return $type === Types::TYPE_INTEGER
31 357
            || $type === Types::TYPE_NUMBER;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 81
    public function normalize(stdClass $schema, Context $context, Walker $walker)
38
    {
39 81
        $property = $this->keywords()[0];
40 81
        $secondaryProperty = $this->keywords()[1];
41
42 81
        if (!property_exists($schema, $property)) {
43 2
            throw new MissingKeywordException($context, $property);
44
        }
45
46 79
        if (!property_exists($schema, $secondaryProperty)) {
47 33
            $schema->{$secondaryProperty} = false;
48 33
        }
49
50 79
        if (!Types::isA($schema->{$property}, Types::TYPE_NUMBER)) {
51 2
            $context->enterNode($property);
52
53 2
            throw new InvalidTypeException($context, Types::TYPE_NUMBER);
54
        }
55
56 77
        if (!is_bool($schema->{$secondaryProperty})) {
57 2
            $context->enterNode($secondaryProperty);
58
59 2
            throw new InvalidTypeException($context, Types::TYPE_BOOLEAN);
60
        }
61 75
    }
62
}
63