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
|
|
|
* All subclasses must redeclare this to match their types. |
16
|
|
|
* |
17
|
|
|
* This is also the key used to wrap the instance singularly for API calls. |
18
|
|
|
*/ |
19
|
|
|
const TYPE = ''; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* All subclasses must redeclare this to match their REST directory (without container). |
23
|
|
|
* |
24
|
|
|
* This is also the key used to wrap instance lists for API calls. |
25
|
|
|
*/ |
26
|
|
|
const DIR = ''; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Api|Data $caller |
30
|
|
|
* @param string $id |
31
|
|
|
* @param array $query |
32
|
|
|
* @return null|static |
33
|
|
|
*/ |
34
|
|
|
public static function load ($caller, string $id, array $query = []) { |
35
|
|
|
return self::_getApi($caller)->load($caller, static::class, self::DIR . '/' . $id, $query); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param Api|Data $caller |
40
|
|
|
* @param string $path |
41
|
|
|
* @param array $query |
42
|
|
|
* @return static[] |
43
|
|
|
*/ |
44
|
|
|
public static function loadAll ($caller, string $path, array $query = []) { |
45
|
|
|
return self::_getApi($caller)->loadAll($caller, static::class, $path, $query); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param AbstractEntity $entity |
50
|
|
|
* @return bool |
51
|
|
|
* @internal pool |
52
|
|
|
*/ |
53
|
|
|
final public function __merge (self $entity): bool { |
54
|
|
|
return false; // todo |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function __toString (): string { |
61
|
|
|
$path = static::DIR . '/' . $this->getId(); |
62
|
|
|
if ($container = $this->_container()) { |
|
|
|
|
63
|
|
|
return "{$container}/{$path}"; |
64
|
|
|
} |
65
|
|
|
return $path; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The container/owner object, if any. |
70
|
|
|
* |
71
|
|
|
* @return null|AbstractEntity |
72
|
|
|
*/ |
73
|
|
|
protected function _container () { |
74
|
|
|
return null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Lazy-loads missing fields. |
79
|
|
|
* |
80
|
|
|
* @param string $field |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
protected function _get (string $field) { |
84
|
|
|
if (!array_key_exists($field, $this->data) and $this->hasId()) { |
85
|
|
|
$this->_reload($field); |
86
|
|
|
} |
87
|
|
|
return parent::_get($field); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function _onDelete (): void { |
91
|
|
|
$this->pool->remove(...$this->getPoolKeys()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function _onSave (): void { |
95
|
|
|
$this->pool->add($this); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $field |
100
|
|
|
*/ |
101
|
|
|
protected function _reload (string $field): void { |
102
|
|
|
assert($this->hasId()); |
103
|
|
|
$remote = $this->api->get($this, ['fields' => $field]); |
104
|
|
|
$this->_setField($field, $remote[static::TYPE][$field]); |
105
|
|
|
$this->pool->add($this); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return null|string |
110
|
|
|
*/ |
111
|
|
|
final public function getId (): ?string { |
112
|
|
|
return $this->data['id'] ?? null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string[] |
117
|
|
|
*/ |
118
|
|
|
public function getPoolKeys () { |
119
|
|
|
return [$this->getId(), (string)$this]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
final public function hasId (): bool { |
126
|
|
|
return isset($this->data['id']); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Fully reloads the entity from Shopify. |
131
|
|
|
* |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
public function reload () { |
135
|
|
|
assert($this->hasId()); |
136
|
|
|
$remote = $this->api->get($this); |
137
|
|
|
if (!isset($remote[static::TYPE]['id'])) { // deleted? |
138
|
|
|
$this->pool->remove(...$this->getPoolKeys()); |
139
|
|
|
throw new RuntimeException("{$this} was deleted upstream."); |
140
|
|
|
} |
141
|
|
|
$this->_setData($remote[static::TYPE]); |
142
|
|
|
$this->pool->add($this); |
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
} |
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.