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