Dereferencer   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 7

Complexity

Total Complexity 18

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Test Coverage

Coverage 78.26%

Importance

Changes 0
Metric Value
dl 0
loc 175
ccs 36
cts 46
cp 0.7826
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 7

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A draft4() 0 4 1
A draft6() 0 4 1
A dereference() 0 7 2
A prepareArguments() 0 10 2
A getLoaderManager() 0 4 1
A setLoaderManager() 0 6 1
A getScopeResolver() 0 4 1
A setScopeResolver() 0 6 1
A getReferenceSerializer() 0 4 1
A setReferenceSerializer() 0 6 1
A crawl() 0 12 2
A loadExternalRef() 0 5 1
1
<?php
2
3
namespace League\JsonReference;
4
5
use League\JsonReference\ReferenceSerializer\SafeReferenceSerializer;
6
use League\JsonReference\ScopeResolver\JsonSchemaScopeResolver;
7
use League\JsonReference\ScopeResolver\NullScopeResolver;
8
9
final class Dereferencer implements DereferencerInterface
10
{
11
    /**
12
     * @var LoaderManager
13
     */
14
    private $loaderManager;
15
16
    /**
17
     * @var ScopeResolverInterface
18
     */
19
    private $scopeResolver;
20
21
    /**
22
     * @var ReferenceSerializerInterface
23
     */
24
    private $referenceSerializer;
25
26
    /**
27
     * Create a new Dereferencer.
28
     *
29
     * @param ScopeResolverInterface                             $scopeResolver
30
     * @param \League\JsonReference\ReferenceSerializerInterface $referenceSerializer
31
     */
32 54
    public function __construct(
33
        ScopeResolverInterface $scopeResolver = null,
34
        ReferenceSerializerInterface $referenceSerializer = null
35
    ) {
36 54
        $this->scopeResolver       = $scopeResolver ?: new NullScopeResolver();
37 54
        $this->referenceSerializer = $referenceSerializer ?: new SafeReferenceSerializer();
38 54
        $this->loaderManager       = new LoaderManager();
39
40 54
        Reference::setDereferencerInstance($this);
41 54
    }
42
43
    /**
44
     * Create a new dereferencer configured for dereferencing JSON Schema Draft4 schemas.
45
     *
46
     * @return \League\JsonReference\Dereferencer
47
     */
48 2
    public static function draft4()
49
    {
50 2
        return new self(new JsonSchemaScopeResolver(JsonSchemaScopeResolver::KEYWORD_DRAFT_4));
51
    }
52
53
    /**
54
     * Create a new dereferencer configured for dereferencing JSON Schema Draft6 schemas.
55
     *
56
     * @return \League\JsonReference\Dereferencer
57
     */
58
    public static function draft6()
59
    {
60
        return new self(new JsonSchemaScopeResolver(JsonSchemaScopeResolver::KEYWORD_DRAFT_6));
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function dereference($schema, $uri = '')
67
    {
68 52
        return $this->crawl($schema, $uri, function ($schema, $pointer, $ref, $scope) {
69 48
            $resolved = new Reference($ref, $scope, is_internal_ref($ref) ? $schema : null);
70 48
            return merge_ref($schema, $resolved, $pointer);
71 52
        });
72
    }
73
74
    /**
75
     * @param object|string $schema
76
     * @param string        $uri
77
     *
78
     * @return array
79
     */
80 52
    private function prepareArguments($schema, $uri)
81
    {
82 52
        if (is_string($schema)) {
83 48
            $uri    = $schema;
84 48
            $schema = resolve_fragment($uri, $this->loadExternalRef($uri));
85 44
            $uri    = strip_fragment($uri);
86 22
        }
87
88 50
        return [$schema, $uri];
89
    }
90
91
    /**
92
     * @return LoaderManager
93
     */
94 10
    public function getLoaderManager()
95
    {
96 10
        return $this->loaderManager;
97
    }
98
99
    /**
100
     * @param \League\JsonReference\LoaderManager $loaderManager
101
     *
102
     * @return \League\JsonReference\Dereferencer
103
     */
104
    public function setLoaderManager(LoaderManager $loaderManager)
105
    {
106
        $this->loaderManager = $loaderManager;
107
108
        return $this;
109
    }
110
111
    /**
112
     * @return \League\JsonReference\ScopeResolverInterface
113
     */
114
    public function getScopeResolver()
115
    {
116
        return $this->scopeResolver;
117
    }
118
119
    /**
120
     * @param \League\JsonReference\ScopeResolverInterface $scopeResolver
121
     *
122
     * @return \League\JsonReference\Dereferencer
123
     */
124
    public function setScopeResolver(ScopeResolverInterface $scopeResolver)
125
    {
126
        $this->scopeResolver = $scopeResolver;
127
128
        return $this;
129
    }
130
131
    /**
132
     * @return \League\JsonReference\ReferenceSerializerInterface
133
     */
134 16
    public function getReferenceSerializer()
135
    {
136 16
        return $this->referenceSerializer;
137
    }
138
139
    /**
140
     * @param \League\JsonReference\ReferenceSerializerInterface $referenceSerializer
141
     *
142
     * @return \League\JsonReference\Dereferencer
143
     */
144 8
    public function setReferenceSerializer(ReferenceSerializerInterface $referenceSerializer)
145
    {
146 8
        $this->referenceSerializer = $referenceSerializer;
147
148 8
        return $this;
149
    }
150
151
    /**
152
     * @param string|object $schema
153
     * @param string        $uri
154
     * @param callable      $resolver
155
     *
156
     * @return object
157
     */
158 52
    private function crawl($schema, $uri, callable $resolver)
159
    {
160 52
        list($schema, $uri) = $this->prepareArguments($schema, $uri);
161
162 50
        foreach (schema_extract($schema, 'League\JsonReference\is_ref') as $pointer => $ref) {
163 48
            $scope = $this->scopeResolver->resolve($schema, $pointer, $uri);
164
165 48
            $schema = $resolver($schema, $pointer, $ref, $scope);
166 24
        }
167
168 48
        return $schema;
169
    }
170
171
    /**
172
     * Load an external ref and return the JSON object.
173
     *
174
     * @param string $reference
175
     *
176
     * @return object
177
     */
178 48
    private function loadExternalRef($reference)
179
    {
180 48
        list($prefix, $path) = parse_external_ref($reference);
181 46
        return $this->loaderManager->getLoader($prefix)->load($path);
182
    }
183
}
184