Completed
Push — master ( 5927eb...1e13db )
by
unknown
04:04
created

ArrayLoader::__construct()   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 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace League\JsonGuard\Loaders;
4
5
use League\JsonGuard;
6
use League\JsonGuard\Exceptions\SchemaLoadingException;
7
use League\JsonGuard\Loader;
8
9
class ArrayLoader implements Loader
10
{
11
    /**
12
     * @var array
13
     */
14
    private $schemas;
15
16
    /**
17
     * @param array $schemas A map of schemas where path => schema.  The schema should be a string or the
18
     *                       object resulting from a json_decode call.
19
     */
20 70
    public function __construct(array $schemas)
21
    {
22 70
        $this->schemas = $schemas;
23 70
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 16
    public function load($path)
29
    {
30 16
        if (!array_key_exists($path, $this->schemas)) {
31 6
            throw SchemaLoadingException::notFound($path);
32
        }
33
34 12
        $schema = $this->schemas[$path];
35
36 12
        if (is_string($schema)) {
37 8
            return JsonGuard\json_decode($schema);
38 6
        } elseif (is_object($schema)) {
39 4
            return $schema;
40
        } else {
41 2
            throw SchemaLoadingException::create($path);
42
        }
43
    }
44
}
45