Validator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 35
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A validate() 0 6 1
A serializeErrors() 0 12 1
1
<?php
2
/**
3
 * This source file is proprietary and part of Rebilly.
4
 *
5
 * (c) Rebilly SRL
6
 *     Rebilly Ltd.
7
 *     Rebilly Inc.
8
 *
9
 * @see https://www.rebilly.com
10
 */
11
12
namespace Rebilly\OpenAPI\JsonSchema;
13
14
use JsonSchema\Constraints\Constraint;
15
use JsonSchema\Constraints\Factory;
16
use JsonSchema\Constraints\SchemaConstraint;
17
use JsonSchema\Entity\JsonPointer;
18
use stdClass;
19
20
/**
21
 * JSON Schema validator facade.
22
 */
23
final class Validator
24
{
25
    private $validator;
26
27
    private $context;
28
29 25
    public function __construct(string $context)
30
    {
31 25
        $factory = new Factory(null, null, Constraint::CHECK_MODE_VALIDATE_SCHEMA);
32 25
        $factory->setConstraintClass('request body', SchemaConstraint::class);
33 25
        $factory->setConstraintClass('response body', SchemaConstraint::class);
34 25
        $this->validator = $factory->createInstanceFor($context);
35 25
        $this->context = $context;
36
    }
37
38 19
    public function validate($value, stdClass $schema, JsonPointer $path = null): array
39
    {
40 19
        $this->validator->check($value, $schema, $path);
41
42 19
        return $this->validator->getErrors();
43
    }
44
45 13
    public static function serializeErrors(array $errors): string
46
    {
47 13
        return "\n" . implode(
48 13
            "\n",
49 13
            array_map(
50
                function ($error) {
51 13
                    return "[{$error['property']}] {$error['message']}";
52 13
                },
53 13
                $errors
54
            )
55
        );
56
    }
57
}
58