|
1
|
|
|
<?php |
|
2
|
|
|
namespace Workana\AsyncJobs\Doctrine; |
|
3
|
|
|
|
|
4
|
|
|
use Bernard\Normalizer\AbstractAggregateNormalizerAware; |
|
5
|
|
|
use Workana\AsyncJobs\Util\ClassUtils; |
|
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
7
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
|
8
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Carlos Frutos <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class QueueableEntityNormalizer extends AbstractAggregateNormalizerAware implements NormalizerInterface, DenormalizerInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var EntityManagerInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $entityManager; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* QueueableEntityNormalizer constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param EntityManagerInterface $entityManager |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(EntityManagerInterface $entityManager) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->entityManager = $entityManager; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
public function normalize($object, $format = null, array $context = array()) |
|
34
|
|
|
{ |
|
35
|
|
|
return [ |
|
36
|
|
|
'class' => ClassUtils::getRealClass($object), |
|
37
|
|
|
'id' => $object->getId(), |
|
38
|
|
|
]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function supportsNormalization($data, $format = null) |
|
45
|
|
|
{ |
|
46
|
|
|
return ($data instanceof QueueableEntityInterface); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
public function denormalize($data, $class, $format = null, array $context = array()) |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->entityManager->getReference($data['class'], $data['id']); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
|
61
|
|
|
{ |
|
62
|
|
|
return is_a($type, QueueableEntityInterface::class, true); |
|
63
|
|
|
} |
|
64
|
|
|
} |