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\Persistence\ObjectRepository; |
14
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
15
|
|
|
use Symfony\Component\Form\Exception\TransformationFailedException; |
16
|
|
|
use Ynlo\GraphQLBundle\Model\NodeInterface; |
17
|
|
|
use Ynlo\GraphQLBundle\Util\IDEncoder; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class IDToNodeTransformer |
21
|
|
|
*/ |
22
|
|
|
class IDToNodeTransformer implements DataTransformerInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ObjectRepository|null |
26
|
|
|
*/ |
27
|
|
|
protected $repository; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $findBy = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* IDToNodeTransformer constructor. |
36
|
|
|
* |
37
|
|
|
* @param ObjectRepository $repository |
38
|
|
|
* @param string|array $findBy |
39
|
|
|
*/ |
40
|
8 |
|
public function __construct(ObjectRepository $repository = null, $findBy = null) |
41
|
|
|
{ |
42
|
8 |
|
$this->repository = $repository; |
43
|
8 |
|
$this->findBy = (array) $findBy; |
44
|
8 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Transforms an object (issue) to a string (number). |
48
|
|
|
* |
49
|
|
|
* @param NodeInterface|NodeInterface[] $node |
50
|
|
|
* |
51
|
|
|
* @return string|string[] |
52
|
|
|
*/ |
53
|
8 |
|
public function transform($node) |
54
|
|
|
{ |
55
|
8 |
|
if (!$node) { |
56
|
8 |
|
return $node; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (\is_array($node) || $node instanceof \Traversable) { |
60
|
|
|
$ids = []; |
61
|
|
|
/** @var array $node */ |
62
|
|
|
foreach ($node as $n) { |
63
|
|
|
$ids[] = $this->transform($n); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $ids; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return IDEncoder::encode($node); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Transforms a string (id) to an object (node). |
74
|
|
|
* |
75
|
|
|
* @param string|string[]|mixed $globalId |
76
|
|
|
* |
77
|
|
|
* @return mixed |
78
|
|
|
* |
79
|
|
|
* @throws TransformationFailedException if object (issue) is not found. |
80
|
|
|
*/ |
81
|
6 |
|
public function reverseTransform($globalId) |
82
|
|
|
{ |
83
|
6 |
|
if (!$globalId || \is_object($globalId)) { |
84
|
|
|
return $globalId; |
85
|
|
|
} |
86
|
|
|
|
87
|
6 |
|
if (\is_array($globalId)) { |
88
|
2 |
|
$nodes = []; |
89
|
|
|
/** @var array $globalId */ |
90
|
2 |
|
foreach ($globalId as $id) { |
91
|
2 |
|
$nodes[] = $this->reverseTransform($id); |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
return $nodes; |
95
|
|
|
} |
96
|
|
|
|
97
|
6 |
|
$node = IDEncoder::decode($globalId); |
98
|
|
|
|
99
|
6 |
|
if (null === $node && $this->repository && $this->findBy) { |
|
|
|
|
100
|
|
|
foreach ($this->findBy as $findBy) { |
101
|
|
|
$node = $this->repository->findOneBy([$findBy => $globalId]); |
102
|
|
|
if ($node) { |
103
|
|
|
break; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
6 |
|
if (null === $node) { |
109
|
|
|
// causes a validation error |
110
|
|
|
// this message is not shown to the user |
111
|
|
|
// see the invalid_message option |
112
|
|
|
throw new TransformationFailedException( |
113
|
|
|
sprintf( |
114
|
|
|
'An node with id "%s" does not exist!', |
115
|
|
|
$globalId |
116
|
|
|
) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
6 |
|
return $node; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|