ReferableNormalizer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 39
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A normalize() 0 10 2
A supportsNormalization() 0 4 2
1
<?php
2
3
namespace Pim\Bundle\CustomEntityBundle\Normalizer;
4
5
use Pim\Bundle\CustomEntityBundle\Entity\AbstractCustomEntity;
6
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
7
8
/**
9
 * Normalizes referable entities
10
 *
11
 * @author    Antoine Guigan <[email protected]>
12
 * @copyright 2013 Akeneo SAS (http://www.akeneo.com)
13
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
14
 */
15
class ReferableNormalizer implements NormalizerInterface
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $allowedformats;
21
22
    /**
23
     * Constructor
24
     *
25
     * @param array $allowedformats
26
     */
27
    public function __construct(array $allowedformats)
28
    {
29
        $this->allowedformats = $allowedformats;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function normalize($object, $format = null, array $context = array())
36
    {
37
        if (array_key_exists('field_name', $context)) {
38
            return [
39
                $context['field_name'] => $object->getReference(),
40
            ];
41
        } else {
42
            throw new \LogicException(sprintf('No normalizer found for object of class "%s"', get_class($object)));
43
        }
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function supportsNormalization($data, $format = null)
50
    {
51
        return $data instanceof AbstractCustomEntity && in_array($format, $this->allowedformats);
52
    }
53
}
54