1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helix\Asana\Base; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A resource with a GID. |
7
|
|
|
* |
8
|
|
|
* @method string getGid () |
9
|
|
|
* @method bool hasGid () |
10
|
|
|
* @method string getResourceType () |
11
|
|
|
*/ |
12
|
|
|
abstract class AbstractEntity extends Data { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* GET/PUT/DELETE path. |
16
|
|
|
* |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
|
|
abstract public function __toString (): string; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Maps lazy-loaded / reloadable mapped {@see Data} to their proper expanded field expression. |
23
|
|
|
* |
24
|
|
|
* If not set here, the field name is used alone. |
25
|
|
|
* |
26
|
|
|
* @var string[] |
27
|
|
|
*/ |
28
|
|
|
protected static $optFields = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Shortcut to update the cache. |
32
|
|
|
*/ |
33
|
|
|
protected function _cache (): void { |
34
|
|
|
$this->api->getCache()->add($this); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Lazy-loads missing fields. |
39
|
|
|
* |
40
|
|
|
* @param string $key |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
protected function _get (string $key) { |
44
|
|
|
if (!array_key_exists($key, $this->data) and isset($this->data['gid'])) { // can't use hasGid(), inf. loop |
45
|
|
|
$this->reload($key); |
46
|
|
|
} |
47
|
|
|
return parent::_get($key); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Adds entities, forcing and flagging a diff if needed. |
52
|
|
|
* |
53
|
|
|
* @param string $key |
54
|
|
|
* @param AbstractEntity[] $entities |
55
|
|
|
* @param bool $force |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
|
|
protected function _merge (string $key, array $entities, $force = false) { |
59
|
|
|
if ($force or isset($this->data[$key])) { |
60
|
|
|
foreach ($entities as $entity) { |
61
|
|
|
$this->data[$key][] = $entity; |
62
|
|
|
} |
63
|
|
|
$this->data[$key] = array_values(array_unique($this->data[$key])); |
64
|
|
|
if ($force) { |
65
|
|
|
$this->diff[$key] = true; |
66
|
|
|
} |
67
|
|
|
else { |
68
|
|
|
$this->_cache(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Removes sub-entities without flagging a diff. |
76
|
|
|
* |
77
|
|
|
* @param string $key |
78
|
|
|
* @param AbstractEntity[] $entities |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
protected function _remove (string $key, array $entities) { |
82
|
|
|
if (isset($this->data[$key])) { |
83
|
|
|
$this->data[$key] = array_values(array_diff($this->data[$key], $entities)); |
84
|
|
|
$this->_cache(); |
85
|
|
|
} |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Cache keys for the entity. |
91
|
|
|
* |
92
|
|
|
* @return string[] |
93
|
|
|
*/ |
94
|
|
|
public function getCacheKeys () { |
95
|
|
|
return [$this->getGid(), (string)$this]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Reloads a specific field, or the whole entity. |
100
|
|
|
* |
101
|
|
|
* @param string $key |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function reload (string $key = null) { |
105
|
|
|
if (isset($key)) { |
106
|
|
|
$value = $this->api->get($this, [], ['fields' => static::$optFields[$key] ?? $key])[$key] ?? null; |
107
|
|
|
$this->_setMapped($key, $value); |
108
|
|
|
} |
109
|
|
|
else { |
110
|
|
|
$this->_setData($this->api->get($this, [], ['expand' => 'this'])); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
$this->_cache(); |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |