TypeConstraint::check()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 16
rs 10
cc 3
nc 4
nop 4
1
<?php
2
3
/*
4
 * This file is part of the OpenapiBundle package.
5
 *
6
 * (c) Niels Nijens <[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 Nijens\OpenapiBundle\Json\Schema\Constraint;
13
14
use JsonSchema\Constraints\TypeConstraint as BaseTypeConstraint;
15
use JsonSchema\Entity\JsonPointer;
16
17
/**
18
 * Extends the type check with the nullable property from the OpenAPI specification.
19
 *
20
 * @author Niels Nijens <[email protected]>
21
 */
22
class TypeConstraint extends BaseTypeConstraint
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function check(&$value = null, $schema = null, ?JsonPointer $path = null, $i = null): void
28
    {
29
        $type = $schema->type ?? null;
30
        $nullable = $schema->nullable ?? false;
31
32
        if (is_array($type) === false) {
33
            $type = [$type];
34
        }
35
36
        if ($nullable === true) {
37
            $type[] = 'null';
38
39
            $schema->type = $type;
40
        }
41
42
        parent::check($value, $schema, $path, $i);
43
    }
44
}
45