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\Model; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ID are formed with base64 representation of the Types and real database ID |
15
|
|
|
* in order to create a unique and global identifier for each resource |
16
|
|
|
* |
17
|
|
|
* @see https://facebook.github.io/relay/docs/graphql-object-identification.html |
18
|
|
|
*/ |
19
|
|
|
class ID |
20
|
|
|
{ |
21
|
|
|
private const DIVIDER = ':'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var mixed |
25
|
|
|
*/ |
26
|
|
|
protected $databaseId; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $nodeType; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* ID constructor. |
35
|
|
|
* |
36
|
|
|
* @param string $nodeType |
37
|
|
|
* @param mixed $databaseId |
38
|
|
|
*/ |
39
|
1 |
|
public function __construct($nodeType, $databaseId) |
40
|
|
|
{ |
41
|
1 |
|
$this->databaseId = $databaseId; |
42
|
1 |
|
$this->nodeType = $nodeType; |
43
|
1 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $nodeType |
47
|
|
|
* @param mixed $databaseId |
48
|
|
|
* |
49
|
|
|
* @return ID |
50
|
|
|
*/ |
51
|
|
|
public static function create($nodeType, $databaseId): ID |
52
|
|
|
{ |
53
|
|
|
return new self($nodeType, $databaseId); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $globalIdentifier |
58
|
|
|
* |
59
|
|
|
* @return ID |
60
|
|
|
*/ |
61
|
1 |
|
public static function createFromString($globalIdentifier) |
62
|
|
|
{ |
63
|
1 |
|
$typeAndUser = base64_decode($globalIdentifier); |
64
|
1 |
|
if (strpos($typeAndUser, self::DIVIDER) > 1) { |
65
|
1 |
|
list($nodeType, $databaseId) = explode(self::DIVIDER, $typeAndUser); |
66
|
|
|
} else { |
67
|
|
|
$nodeType = null; |
68
|
|
|
$databaseId = null; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return new self($nodeType, $databaseId); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $nodeType |
76
|
|
|
* @param mixed $databaseId |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
11 |
|
public static function encode($nodeType, $databaseId) |
81
|
|
|
{ |
82
|
11 |
|
return base64_encode(sprintf('%s%s%s', $nodeType, self::DIVIDER, $databaseId)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
1 |
|
public function getDatabaseId() |
89
|
|
|
{ |
90
|
1 |
|
return $this->databaseId; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
1 |
|
public function getNodeType(): ?string |
97
|
|
|
{ |
98
|
1 |
|
return $this->nodeType; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function __toString() |
105
|
|
|
{ |
106
|
|
|
return self::encode($this->getNodeType(), $this->getDatabaseId()); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|