|
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
|
5 |
|
public function __construct(ObjectRepository $repository = null, $findBy = null) |
|
41
|
|
|
{ |
|
42
|
5 |
|
$this->repository = $repository; |
|
43
|
5 |
|
$this->findBy = (array) $findBy; |
|
44
|
5 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Transforms an object (issue) to a string (number). |
|
48
|
|
|
* |
|
49
|
|
|
* @param NodeInterface|NodeInterface[] $node |
|
50
|
|
|
* |
|
51
|
|
|
* @return string|string[] |
|
52
|
|
|
*/ |
|
53
|
2 |
|
public function transform($node) |
|
54
|
|
|
{ |
|
55
|
2 |
|
if (!$node) { |
|
56
|
2 |
|
return $node; |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
if (\is_array($node) || $node instanceof \Traversable) { |
|
60
|
1 |
|
$ids = []; |
|
61
|
|
|
/** @var array $node */ |
|
62
|
1 |
|
foreach ($node as $n) { |
|
63
|
1 |
|
$ids[] = $this->transform($n); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
return $ids; |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
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
|
3 |
|
public function reverseTransform($globalId) |
|
82
|
|
|
{ |
|
83
|
3 |
|
if (!$globalId || \is_object($globalId)) { |
|
84
|
1 |
|
return $globalId; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
3 |
|
if (\is_array($globalId)) { |
|
88
|
1 |
|
$nodes = []; |
|
89
|
|
|
/** @var array $globalId */ |
|
90
|
1 |
|
foreach ($globalId as $id) { |
|
91
|
1 |
|
$nodes[] = $this->reverseTransform($id); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
return $nodes; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
3 |
|
$node = IDEncoder::decode($globalId); |
|
98
|
|
|
|
|
99
|
3 |
|
if (null === $node && $this->repository && $this->findBy) { |
|
|
|
|
|
|
100
|
1 |
|
foreach ($this->findBy as $findBy) { |
|
101
|
1 |
|
$node = $this->repository->findOneBy([$findBy => $globalId]); |
|
102
|
1 |
|
if ($node) { |
|
103
|
1 |
|
break; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
3 |
|
if (null === $node) { |
|
109
|
|
|
// causes a validation error |
|
110
|
|
|
// this message is not shown to the user |
|
111
|
|
|
// see the invalid_message option |
|
112
|
1 |
|
throw new TransformationFailedException( |
|
113
|
1 |
|
sprintf( |
|
114
|
1 |
|
'An node with id "%s" does not exist!', |
|
115
|
1 |
|
$globalId |
|
116
|
|
|
) |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
2 |
|
return $node; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|