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