1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ResourceIdentifier.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license https://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec <[email protected]> |
8
|
|
|
* @package iPublikuj:JsonAPIClient! |
9
|
|
|
* @subpackage Objects |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 05.05.18 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\JsonAPIClient\Objects; |
18
|
|
|
|
19
|
|
|
use CloudCreativity\Utils\Object\StandardObject; |
20
|
|
|
|
21
|
|
|
use IPub\JsonAPIClient\Exceptions; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Resource identifier object |
25
|
|
|
* |
26
|
|
|
* @package iPublikuj:JsonAPIClient! |
27
|
|
|
* @subpackage Objects |
28
|
|
|
* |
29
|
|
|
* @author Adam Kadlec <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class ResourceIdentifier extends StandardObject implements IResourceIdentifier |
32
|
|
|
{ |
33
|
|
|
use TIdentifiable; |
34
|
|
|
use TMetaMember; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $type |
38
|
|
|
* @param string $id |
39
|
|
|
* |
40
|
|
|
* @return ResourceIdentifier |
41
|
|
|
*/ |
42
|
|
|
public static function create(string $type, string $id) : IResourceIdentifier |
43
|
|
|
{ |
44
|
|
|
$identifier = new self(); |
45
|
|
|
|
46
|
|
|
$identifier->set(self::TYPE, $type) |
47
|
|
|
->set(self::ID, $id); |
48
|
|
|
|
49
|
|
|
return $identifier; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function isType($typeOrTypes) : bool |
56
|
|
|
{ |
57
|
|
|
return in_array($this->get(self::TYPE), (array) $typeOrTypes, TRUE); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function mapType(array $map) |
64
|
|
|
{ |
65
|
|
|
$type = $this->getType(); |
66
|
|
|
|
67
|
|
|
if (array_key_exists($type, $map)) { |
68
|
|
|
return $map[$type]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
throw new Exceptions\RuntimeException(sprintf('Type "%s" is not in the supplied map.', $type)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function isComplete() : bool |
78
|
|
|
{ |
79
|
|
|
return $this->hasType() && $this->hasId(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function isSame(IResourceIdentifier $identifier) : bool |
86
|
|
|
{ |
87
|
|
|
return $this->getType() === $identifier->getType() && |
88
|
|
|
$this->getId() === $identifier->getId(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function toString() : string |
95
|
|
|
{ |
96
|
|
|
return sprintf('%s:%s', $this->getType(), $this->getId()); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|