Completed
Pull Request — master (#12)
by
unknown
02:44
created

DecoderManager::hasDecoder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonReference;
4
5
use League\JsonReference\Decoder\JsonDecoder;
6
use League\JsonReference\Decoder\YamlDecoder;
7
8
final class DecoderManager
9
{    
0 ignored issues
show
Coding Style introduced by
The opening class brace should be on a newline by itself.
Loading history...
10
    /**
11
     * @var ParserInterface[]
12
     */
13
    private $decoders = [];
14
15
    /**
16
     * @var bool
17
     */
18
    private $ignoreUnknownExtension = [];
19
20
    /**
21
     * @param DecoderInterface[] $decoders
22
     */
23 104
    public function __construct(array $decoders = [], $ignoreUnknownExtension = true)
24
    {
25 104
        if (empty($decoders)) {
26 102
            $this->registerDefaultDecoder();
27 51
        }
28
        
29 104
        foreach ($decoders as $extension => $decoder) {
30 2
            $this->registerDecoder($extension, $decoder);
31 52
        }
32
33 104
        $this->ignoreUnknownExtension = $ignoreUnknownExtension;
34 104
    }
35
36
    /**
37
     * Register a DecoderInterface for the given extension.
38
     *
39
     * @param DecoderInterface $decoder
40
     */
41 104
    public function registerDecoder($extension, DecoderInterface $decoder)
42
    {
43 104
        $this->decoders[$extension] = $decoder;
44 104
    }
45
46
    /**
47
     * Get all registered decoders, keyed by the extensions they are registered to decode schemas for.
48
     *
49
     * @return DecoderInterface[]
50
     */
51 2
    public function getDecoders()
52
    {
53 2
        return $this->decoders;
54
    }
55
56
    /**
57
     * Set to true to use default decoder for unknown file extensions
58
     * 
59
     * @param bool
60
     */
61
    public function setIgnoreUnknownExtension($ignoreUnknownExtension)
62
    {
63
        $this->ignoreUnknownExtension = $ignoreUnknownExtension;
64
    }
65
66
    /**
67
     * Get the decoder for the given extension.
68
     *
69
     * @param string $extension
70
     *
71
     * @return DecoderInterface
72
     * @throws \InvalidArgumentException
73
     */
74 58
    public function getDecoder($extension)
75
    {
76 58
        if (!$this->hasDecoder($extension)) 
77 29
        {
78 4
            if($this->ignoreUnknownExtension) $extension = 'json';
79 2
            else throw new \InvalidArgumentException(sprintf('A decoder is not registered for the extension "%s"', $extension));
80 1
        }
81
82 56
        return $this->decoders[$extension];
83
    }
84
    
85
    /**
86
     * @param string $extension
87
     *
88
     * @return bool
89
     */
90 60
    public function hasDecoder($extension)
91
    {
92 60
        return isset($this->decoders[$extension]);
93
    }
94
95
    /**
96
     * Register the default decoder.
97
     */
98 102
    private function registerDefaultDecoder()
99
    {
100 102
        $this->registerDecoder('json', new JsonDecoder());
101 102
        $this->registerDecoder('yaml', new YamlDecoder());
102 102
        $this->registerDecoder('yml', $this->decoders['yaml']);
103 102
    }
104
}
105