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\ORM\EntityManagerInterface; |
14
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
15
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
16
|
|
|
use Ynlo\GraphQLBundle\Model\ID; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class IDToNodeTransformer |
20
|
|
|
*/ |
21
|
|
|
class DataWithIdToNodeTransformer implements DataTransformerInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var EntityManagerInterface |
25
|
|
|
*/ |
26
|
|
|
private $em; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Endpoint |
30
|
|
|
*/ |
31
|
|
|
protected $endpoint; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* IDToNodeTransformer constructor. |
35
|
|
|
* |
36
|
|
|
* @param EntityManagerInterface $em |
37
|
|
|
* @param Endpoint $endpoint |
38
|
|
|
*/ |
39
|
|
|
public function __construct(EntityManagerInterface $em, Endpoint $endpoint) |
40
|
|
|
{ |
41
|
|
|
$this->em = $em; |
42
|
|
|
$this->endpoint = $endpoint; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function transform($data) |
49
|
|
|
{ |
50
|
|
|
if (is_array($data) && isset($data['id'])) { |
51
|
|
|
$id = ID::createFromString($data['id']); |
52
|
|
|
if ($this->endpoint->hasType($id->getNodeType())) { |
53
|
|
|
$class = $this->endpoint->getClassForType($id->getNodeType()); |
54
|
|
|
if ($class) { |
|
|
|
|
55
|
|
|
return $this->em->getRepository($class)->find($id->getDatabaseId()); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return null; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function reverseTransform($data) |
67
|
|
|
{ |
68
|
|
|
return $data; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: