1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Relationship.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
|
|
|
* Relationship |
25
|
|
|
* |
26
|
|
|
* @package iPublikuj:JsonAPIClient! |
27
|
|
|
* @subpackage Objects |
28
|
|
|
* |
29
|
|
|
* @author Adam Kadlec <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class Relationship extends StandardObject implements IRelationship |
32
|
|
|
{ |
33
|
|
|
use TMetaMember; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function getData() |
39
|
|
|
{ |
40
|
|
|
if ($this->isHasMany()) { |
41
|
|
|
return $this->getIdentifiers(); |
42
|
|
|
|
43
|
|
|
} elseif (!$this->isHasOne()) { |
44
|
|
|
throw new Exceptions\RuntimeException('No data member or data member is not a valid relationship.'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->hasIdentifier() ? $this->getIdentifier() : NULL; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function getIdentifier() : ?IResourceIdentifier |
54
|
|
|
{ |
55
|
|
|
if (!$this->isHasOne()) { |
56
|
|
|
throw new Exceptions\RuntimeException('No data member or data member is not a valid has-one relationship.'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$data = $this->{self::DATA}; |
60
|
|
|
|
61
|
|
|
if (!$data) { |
62
|
|
|
throw new Exceptions\RuntimeException('No resource identifier - relationship is empty.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return new ResourceIdentifier($data); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function hasIdentifier() : bool |
72
|
|
|
{ |
73
|
|
|
return is_object($this->{self::DATA}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function isHasOne() : bool |
80
|
|
|
{ |
81
|
|
|
if (!$this->has(self::DATA)) { |
82
|
|
|
return FALSE; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$data = $this->{self::DATA}; |
86
|
|
|
|
87
|
|
|
return is_null($data) || is_object($data); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function getIdentifiers() : IResourceIdentifierCollection |
94
|
|
|
{ |
95
|
|
|
if (!$this->isHasMany()) { |
96
|
|
|
throw new Exceptions\RuntimeException('No data member of data member is not a valid has-many relationship.'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return ResourceIdentifierCollection::create($this->{self::DATA}); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function isHasMany() : bool |
106
|
|
|
{ |
107
|
|
|
return is_array($this->{self::DATA}); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|