Completed
Pull Request — master (#12)
by
unknown
14:43
created

DecoderManager::setIgnoreUnknownExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
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
    public function __construct(array $decoders = [], $ignoreUnknownExtension = true)
26
    {
27
        if (empty($decoders)) {
28
            $this->registerDefaultDecoder();
29
        }
30
        
31
        foreach ($decoders as $extension => $decoder) {
32
            $this->registerDecoder($extension, $decoder);
33
        }
34
35
        $this->ignoreUnknownExtension = $ignoreUnknownExtension;
36
    }
37
38
    /**
39
     * Register a DecoderInterface for the given extension.
40
     *
41
     * @param DecoderInterface $decoder
42
     */
43
    public function registerDecoder($extension, DecoderInterface $decoder)
44
    {
45
        $this->decoders[$extension] = $decoder;
46
    }
47
48
    /**
49
     * Get all registered decoders, keyed by the extensions they are registered to decode schemas for.
50
     *
51
     * @return DecoderInterface[]
52
     */
53
    public function getDecoders()
54
    {
55
        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
    public function getDecoder($extension)
77
    {
78
        if (!$this->hasDecoder($extension)) {
79
            if($this->ignoreUnknownExtension) { $extension = 'json';
80
            } else { throw new \InvalidArgumentException(sprintf('A decoder is not registered for the extension "%s"', $extension));
81
            }
82
        }
83
84
        return $this->decoders[$extension];
85
    }
86
    
87
    /**
88
     * @param string $extension
89
     *
90
     * @return bool
91
     */
92
    public function hasDecoder($extension)
93
    {
94
        return isset($this->decoders[$extension]);
95
    }
96
97
    /**
98
     * Register the default decoder.
99
     */
100
    private function registerDefaultDecoder()
101
    {
102
        $this->registerDecoder('json', new JsonDecoder());
103
        $this->registerDecoder('yaml', new YamlDecoder());
104
        $this->registerDecoder('yml', $this->decoders['yaml']);
105
    }
106
}
107