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

JsonSchemaScopeResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolve() 0 17 4
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