1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the xAPI package. |
5
|
|
|
* |
6
|
|
|
* (c) Christian Flothmann <[email protected]> |
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 Xabbuh\XApi\Serializer\Symfony\Normalizer; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Serializer\Exception\LogicException; |
15
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
16
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
17
|
|
|
use Symfony\Component\Serializer\SerializerAwareInterface; |
18
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Christian Flothmann <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
abstract class Normalizer implements DenormalizerInterface, NormalizerInterface, SerializerAwareInterface |
24
|
|
|
{ |
25
|
|
|
private $serializer; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function setSerializer(SerializerInterface $serializer) |
31
|
|
|
{ |
32
|
|
|
$this->serializer = $serializer; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function normalizeAttribute($value, $format = null, array $context = array()) |
36
|
|
|
{ |
37
|
|
|
if (!$this->serializer instanceof NormalizerInterface) { |
38
|
|
|
throw new LogicException('Cannot normalize attribute because the injected serializer is not a normalizer'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $this->serializer->normalize($value, $format, $context); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function denormalizeData($data, $type, $format = null, array $context = array()) |
45
|
|
|
{ |
46
|
|
|
if (!$this->serializer instanceof DenormalizerInterface) { |
47
|
|
|
throw new LogicException('Cannot denormalize because the injected serializer is not a denormalizer'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->serializer->denormalize($data, $type, $format, $context); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|