|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FOSRestBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace FOS\RestBundle\Decoder; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Container\ContainerInterface as PsrContainerInterface; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Provides encoders through the Symfony DIC. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Igor Wiedler <[email protected]> |
|
21
|
|
|
* |
|
22
|
|
|
* @final since 2.8 |
|
23
|
|
|
*/ |
|
24
|
|
|
class ContainerDecoderProvider implements DecoderProviderInterface |
|
25
|
|
|
{ |
|
26
|
|
|
private $container; |
|
27
|
|
|
private $decoders; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param PsrContainerInterface $container The container from which the actual decoders are retrieved |
|
33
|
|
|
* @param array $decoders List of key (format) value (service ids) of decoders |
|
34
|
|
|
*/ |
|
35
|
48 |
|
public function __construct($container, array $decoders) |
|
36
|
|
|
{ |
|
37
|
48 |
|
if (!$container instanceof PsrContainerInterface && !$container instanceof ContainerInterface) { |
|
38
|
|
|
throw new \InvalidArgumentException(sprintf('The container must be an instance of %s or %s (%s given).', PsrContainerInterface::class, ContainerInterface::class, is_object($container) ? get_class($container) : gettype($container))); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
48 |
|
$this->container = $container; |
|
42
|
48 |
|
$this->decoders = $decoders; |
|
43
|
48 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
11 |
|
public function supports($format) |
|
49
|
|
|
{ |
|
50
|
11 |
|
return isset($this->decoders[$format]); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
8 |
|
public function getDecoder($format) |
|
57
|
|
|
{ |
|
58
|
8 |
|
if (!$this->supports($format)) { |
|
59
|
|
|
throw new \InvalidArgumentException(sprintf("Format '%s' is not supported by ContainerDecoderProvider.", $format)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
8 |
|
return $this->container->get($this->decoders[$format]); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|