Passed
Pull Request — master (#69)
by Viacheslav
02:25
created

Preloaded::setSchemaData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Swaggest\JsonSchema\RemoteRef;
4
5
use Swaggest\JsonSchema\Context;
6
use Swaggest\JsonSchema\RefResolver;
7
use Swaggest\JsonSchema\RemoteRefProvider;
8
use Swaggest\JsonSchema\Schema;
9
10
class Preloaded implements RemoteRefProvider
11
{
12
    private $storage;
13
14
    private $schemaFiles;
15
16 23
    public function __construct()
17
    {
18 23
        $this->schemaFiles = array(
19
            'http://json-schema.org/draft-04/schema' => __DIR__ . '/../../spec/json-schema.json',
20
            'http://json-schema.org/draft-06/schema' => __DIR__ . '/../../spec/json-schema-draft6.json',
21
            'http://json-schema.org/draft-07/schema' => __DIR__ . '/../../spec/json-schema-draft7.json',
22
        );
23 23
    }
24
25 123
    public function getSchemaData($url)
26
    {
27 123
        if (isset($this->storage[$url])) {
28 120
            return $this->storage[$url];
29 5
        } elseif (isset($this->schemaFiles[$url])) {
30 4
            $this->storage[$url] = json_decode(file_get_contents($this->schemaFiles[$url]));
31 4
            return $this->storage[$url];
32
        }
33 1
        return false;
34
    }
35
36
    /**
37
     * @param RefResolver $refResolver
38
     * @param Context|null $options
39
     * @throws \Swaggest\JsonSchema\Exception
40
     */
41 334
    public function populateSchemas(RefResolver $refResolver, Context $options = null)
42
    {
43 334
        if ($options === null) {
44 1
            $options = new Context();
45 1
            $options->refResolver = $refResolver;
46 1
            $options->version = Schema::VERSION_AUTO;
47
        }
48
49 334
        $prev = $refResolver->getResolutionScope();
50 334
        foreach ($this->storage as $url => $schemaData) {
51 334
            $refResolver->setupResolutionScope($url, $schemaData);
52 334
            $refResolver->setResolutionScope($url);
53 334
            $refResolver->preProcessReferences($schemaData, $options);
54
        }
55 334
        $refResolver->setResolutionScope($prev);
56 334
    }
57
58 17
    public function setSchemaData($url, $schemaData)
59
    {
60 17
        $this->storage[rtrim($url, '#')] = $schemaData;
61 17
        return $this;
62
    }
63
64
    public function setSchemaFile($url, $path)
65
    {
66
        $this->schemaFiles[rtrim($url, '#')] = $path;
67
        return $this;
68
    }
69
70
71
}