|
1
|
|
|
<?php |
|
2
|
|
|
/******************************************************************************* |
|
3
|
|
|
* This file is part of the GraphQL Bundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) YnloUltratech <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
******************************************************************************/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Form\DataTransformer; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\Common\Util\ClassUtils; |
|
14
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
15
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
|
16
|
|
|
use Symfony\Component\Form\Exception\TransformationFailedException; |
|
17
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionManager; |
|
18
|
|
|
use Ynlo\GraphQLBundle\Model\ID; |
|
19
|
|
|
use Ynlo\GraphQLBundle\Model\NodeInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class IDToNodeTransformer |
|
23
|
|
|
*/ |
|
24
|
|
|
class IDToNodeTransformer implements DataTransformerInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var EntityManagerInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $em; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var DefinitionManager |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $dm; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* IDToNodeTransformer constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param EntityManagerInterface $em |
|
40
|
|
|
* @param DefinitionManager $definitionManager |
|
41
|
|
|
*/ |
|
42
|
4 |
|
public function __construct(EntityManagerInterface $em, DefinitionManager $definitionManager) |
|
43
|
|
|
{ |
|
44
|
4 |
|
$this->em = $em; |
|
45
|
4 |
|
$this->dm = $definitionManager; |
|
46
|
4 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Transforms an object (issue) to a string (number). |
|
50
|
|
|
* |
|
51
|
|
|
* @param NodeInterface|null $node |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
4 |
|
public function transform($node) |
|
56
|
|
|
{ |
|
57
|
4 |
|
if (null === $node) { |
|
58
|
4 |
|
return ''; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$nodeType = $this->dm->getTypeForClass(ClassUtils::getRealClass($node)); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
return ID::encode($nodeType, $node->getId()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Transforms a string (id) to an object (node). |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $globalId |
|
70
|
|
|
* |
|
71
|
|
|
* @return NodeInterface|null |
|
72
|
|
|
* |
|
73
|
|
|
* @throws TransformationFailedException if object (issue) is not found. |
|
74
|
|
|
*/ |
|
75
|
4 |
|
public function reverseTransform($globalId) |
|
76
|
|
|
{ |
|
77
|
4 |
|
if (!$globalId) { |
|
78
|
|
|
return null; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
4 |
|
$id = ID::createFromString($globalId); |
|
82
|
|
|
|
|
83
|
4 |
|
if (!$id || !$id->getNodeType() || !$this->dm->hasType($id->getNodeType())) { |
|
|
|
|
|
|
84
|
|
|
return null; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
4 |
|
$node = $this->em |
|
88
|
4 |
|
->getRepository($this->dm->getClassForType($id->getNodeType())) |
|
89
|
4 |
|
->find($id->getDatabaseId()); |
|
90
|
|
|
|
|
91
|
4 |
|
if (null === $node) { |
|
92
|
|
|
// causes a validation error |
|
93
|
|
|
// this message is not shown to the user |
|
94
|
|
|
// see the invalid_message option |
|
95
|
|
|
throw new TransformationFailedException( |
|
96
|
|
|
sprintf( |
|
97
|
|
|
'An node with id "%s" does not exist!', |
|
98
|
|
|
$id |
|
99
|
|
|
) |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
4 |
|
return $node; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|