Completed
Pull Request — master (#4)
by Matt
05:13
created

JsonSchemaScopeResolver::resolve()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 5
nop 3
dl 0
loc 17
ccs 13
cts 13
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonReference\ScopeResolver;
4
5
use League\JsonReference\ScopeResolverInterface;
6
use function League\JsonReference\pointer;
7
use function League\JsonReference\pointer_push;
8
use function League\JsonReference\resolve_uri;
9
10
final class JsonSchemaScopeResolver implements ScopeResolverInterface
11
{
12
    const KEYWORD_DRAFT_4 = 'id';
13
    const KEYWORD_DRAFT_6 = '$id';
14
15
    /**
16
     * @var string
17
     */
18
    private $keyword;
19
20
    /**
21
     * @param string $keyword
22
     */
23 2
    public function __construct($keyword)
24
    {
25 2
        $this->keyword = $keyword;
26 2
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 2
    public function resolve($schema, $currentPointer, $currentScope)
32
    {
33 2
        $pointer     = pointer($schema);
34 2
        $currentPath = '';
35
36 2
        foreach (explode('/', $currentPointer) as $segment) {
37 2
            if (!empty($segment)) {
38 2
                $currentPath = pointer_push($currentPath, $segment);
39 1
            }
40 2
            $id = $pointer->getRaw($currentPath . '/' . $this->keyword);
41 2
            if (is_string($id)) {
42 2
                $currentScope = resolve_uri($id, $currentScope);
43 1
            }
44 1
        }
45
46 2
        return $currentScope;
47
    }
48
}
49