Completed
Pull Request — master (#12)
by
unknown
03:59
created

DecoderManager::setIgnoreUnknownExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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