|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Helix\Shopify\Base; |
|
4
|
|
|
|
|
5
|
|
|
use Helix\Shopify\Api; |
|
6
|
|
|
use RuntimeException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* An entity with an ID that can be saved, updated, and deleted. |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class AbstractEntity extends Data |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* All subclasses must redeclare this to match their types. |
|
17
|
|
|
* |
|
18
|
|
|
* This is also the key used to wrap the instance singularly for API calls. |
|
19
|
|
|
*/ |
|
20
|
|
|
const TYPE = ''; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* All subclasses must redeclare this to match their REST directory (without container). |
|
24
|
|
|
* |
|
25
|
|
|
* This is also the key used to wrap instance lists for API calls. |
|
26
|
|
|
*/ |
|
27
|
|
|
const DIR = ''; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param Api|Data $caller |
|
31
|
|
|
* @param string $id |
|
32
|
|
|
* @param array $query |
|
33
|
|
|
* @return null|static |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function load($caller, string $id, array $query = []) |
|
36
|
|
|
{ |
|
37
|
|
|
return self::_getApi($caller)->load($caller, static::class, self::DIR . '/' . $id, $query); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param Api|Data $caller |
|
42
|
|
|
* @param string $path |
|
43
|
|
|
* @param array $query |
|
44
|
|
|
* @return static[] |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function loadAll($caller, string $path, array $query = []) |
|
47
|
|
|
{ |
|
48
|
|
|
return self::_getApi($caller)->loadAll($caller, static::class, $path, $query); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param AbstractEntity $entity |
|
53
|
|
|
* @return bool |
|
54
|
|
|
* @internal pool |
|
55
|
|
|
*/ |
|
56
|
|
|
final public function __merge(self $entity): bool |
|
57
|
|
|
{ |
|
58
|
|
|
return false; // todo |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __toString(): string |
|
65
|
|
|
{ |
|
66
|
|
|
$path = static::DIR . '/' . $this->getId(); |
|
67
|
|
|
if ($container = $this->_container()) { |
|
|
|
|
|
|
68
|
|
|
return "{$container}/{$path}"; |
|
69
|
|
|
} |
|
70
|
|
|
return $path; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* The container/owner object, if any. |
|
75
|
|
|
* |
|
76
|
|
|
* @return null|AbstractEntity |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function _container() |
|
79
|
|
|
{ |
|
80
|
|
|
return null; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Lazy-loads missing fields. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $field |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function _get(string $field, $default = null) |
|
90
|
|
|
{ |
|
91
|
|
|
if (!array_key_exists($field, $this->data) and $this->hasId()) { |
|
92
|
|
|
$this->_reload($field); |
|
93
|
|
|
} |
|
94
|
|
|
return parent::_get($field, $default); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function _onDelete(): void |
|
98
|
|
|
{ |
|
99
|
|
|
$this->pool->remove(...$this->getPoolKeys()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected function _onSave(): void |
|
103
|
|
|
{ |
|
104
|
|
|
$this->pool->add($this); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param string $field |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function _reload(string $field): void |
|
111
|
|
|
{ |
|
112
|
|
|
assert($this->hasId()); |
|
113
|
|
|
$remote = $this->api->get($this, ['fields' => $field]); |
|
114
|
|
|
$this->_setField($field, $remote[static::TYPE][$field]); |
|
115
|
|
|
$this->pool->add($this); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return null|string |
|
120
|
|
|
*/ |
|
121
|
|
|
final public function getId(): ?string |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->data['id'] ?? null; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return string[] |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getPoolKeys() |
|
130
|
|
|
{ |
|
131
|
|
|
return [$this->getId(), (string)$this]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
|
|
final public function hasId(): bool |
|
138
|
|
|
{ |
|
139
|
|
|
return isset($this->data['id']); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Fully reloads the entity from Shopify. |
|
144
|
|
|
* |
|
145
|
|
|
* @return $this |
|
146
|
|
|
*/ |
|
147
|
|
|
public function reload() |
|
148
|
|
|
{ |
|
149
|
|
|
assert($this->hasId()); |
|
150
|
|
|
$remote = $this->api->get($this); |
|
151
|
|
|
if (!isset($remote[static::TYPE]['id'])) { // deleted? |
|
152
|
|
|
$this->pool->remove(...$this->getPoolKeys()); |
|
153
|
|
|
throw new RuntimeException("{$this} was deleted upstream."); |
|
154
|
|
|
} |
|
155
|
|
|
$this->_setData($remote[static::TYPE]); |
|
156
|
|
|
$this->pool->add($this); |
|
157
|
|
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.