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

ArrayLoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 36
ccs 12
cts 12
cp 1
rs 10
c 1
b 0
f 1
wmc 5
lcom 1
cbo 1

2 Methods

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