@@ 21-65 (lines=45) @@ | ||
18 | * |
|
19 | * @author Mohamed Amine Fattouch <[email protected]> |
|
20 | */ |
|
21 | class DecoderProvider extends ContainerAwareTrait implements DecoderProviderInterface |
|
22 | { |
|
23 | /** |
|
24 | * @var array |
|
25 | */ |
|
26 | protected $decoders; |
|
27 | ||
28 | /** |
|
29 | * The constructor class. |
|
30 | * |
|
31 | * @param array $decoders List of decoders (default empty). |
|
32 | */ |
|
33 | public function __construct(array $decoders = []) |
|
34 | { |
|
35 | $this->decoders = $decoders; |
|
36 | } |
|
37 | ||
38 | /** |
|
39 | * @param string $format The format of decoder. |
|
40 | * |
|
41 | * @throws \InvalidArgumentException |
|
42 | * |
|
43 | * @return DecoderInterface |
|
44 | */ |
|
45 | public function getDecoder($format) |
|
46 | { |
|
47 | if ($this->supports($format) === false) { |
|
48 | throw new \InvalidArgumentException(sprintf("Format '%s' is not supported.", $format)); |
|
49 | } |
|
50 | ||
51 | return $this->container->get($this->decoders[$format]); |
|
52 | } |
|
53 | ||
54 | /** |
|
55 | * Tests whether a decoder for a given format exists. |
|
56 | * |
|
57 | * @param string $format The format. |
|
58 | * |
|
59 | * @return boolean |
|
60 | */ |
|
61 | protected function supports($format) |
|
62 | { |
|
63 | return isset($this->decoders[$format]); |
|
64 | } |
|
65 | } |
|
66 |
@@ 21-65 (lines=45) @@ | ||
18 | * |
|
19 | * @author Mohamed Amine Fattouch <[email protected]> |
|
20 | */ |
|
21 | class EncoderProvider extends ContainerAwareTrait implements EncoderProviderInterface |
|
22 | { |
|
23 | /** |
|
24 | * @var array |
|
25 | */ |
|
26 | protected $encoders; |
|
27 | ||
28 | /** |
|
29 | * The constructor class. |
|
30 | * |
|
31 | * @param array $encoders List of encoders (default empty). |
|
32 | */ |
|
33 | public function __construct(array $encoders = []) |
|
34 | { |
|
35 | $this->encoders = $encoders; |
|
36 | } |
|
37 | ||
38 | /** |
|
39 | * @param string $format The format of encoder. |
|
40 | * |
|
41 | * @throws \InvalidArgumentException |
|
42 | * |
|
43 | * @return EncoderInterface |
|
44 | */ |
|
45 | public function getEncoder($format) |
|
46 | { |
|
47 | if ($this->supports($format) === false) { |
|
48 | throw new \InvalidArgumentException(sprintf("Format '%s' is not supported.", $format)); |
|
49 | } |
|
50 | ||
51 | return $this->container->get($this->encoders[$format]); |
|
52 | } |
|
53 | ||
54 | /** |
|
55 | * Tests whether a encoder for a given format exists. |
|
56 | * |
|
57 | * @param string $format The format. |
|
58 | * |
|
59 | * @return boolean |
|
60 | */ |
|
61 | protected function supports($format) |
|
62 | { |
|
63 | return isset($this->encoders[$format]); |
|
64 | } |
|
65 | } |
|
66 |