1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) 2019, Wesley O. Nichols |
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 Wesnick\WorkflowBundle\Serializer; |
13
|
|
|
|
14
|
|
|
use ApiPlatform\Core\JsonLd\Serializer\ItemNormalizer; |
15
|
|
|
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; |
16
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; |
17
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
18
|
|
|
use Symfony\Component\Serializer\SerializerAwareInterface; |
19
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
20
|
|
|
use Wesnick\WorkflowBundle\Model\PotentialActionInterface; |
21
|
|
|
use Wesnick\WorkflowBundle\WorkflowActionGenerator; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class WorkflowNormalizer. |
25
|
|
|
* |
26
|
|
|
* @author Wesley O. Nichols <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class WorkflowNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface, ContextAwareDenormalizerInterface, SerializerAwareInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var ItemNormalizer |
32
|
|
|
*/ |
33
|
|
|
private $decorated; |
34
|
|
|
private $customNormalizer; |
35
|
|
|
private $workflowActions; |
36
|
|
|
|
37
|
|
|
public function __construct( |
38
|
|
|
NormalizerInterface $decorated, |
39
|
|
|
NormalizerInterface $customNormalizer, |
40
|
|
|
WorkflowActionGenerator $workflowActions |
41
|
|
|
) { |
42
|
|
|
$this->decorated = $decorated; |
|
|
|
|
43
|
|
|
$this->customNormalizer = $customNormalizer; |
44
|
|
|
$this->workflowActions = $workflowActions; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function supportsNormalization($data, $format = null): bool |
51
|
|
|
{ |
52
|
|
|
return $this->decorated->supportsNormalization($data, $format); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function hasCacheableSupportsMethod(): bool |
59
|
|
|
{ |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function normalize($object, $format = null, array $context = []) |
64
|
|
|
{ |
65
|
|
|
if ($object instanceof PotentialActionInterface) { |
66
|
|
|
$actions = $this->workflowActions->getActionsForSubject($object); |
67
|
|
|
foreach ($actions as $action) { |
68
|
|
|
$object->addPotentialAction($action); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->decorated->normalize($object, $format, $context); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function supportsDenormalization($data, $type, $format = null, array $context = []) |
76
|
|
|
{ |
77
|
|
|
return $this->decorated->supportsDenormalization($data, $type, $format, $context); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function denormalize($data, $class, $format = null, array $context = []) |
81
|
|
|
{ |
82
|
|
|
return $this->decorated->denormalize($data, $class, $format, $context); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function setSerializer(SerializerInterface $serializer) |
86
|
|
|
{ |
87
|
|
|
$this->decorated->setSerializer($serializer); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.