|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Helix\Asana\Base\AbstractEntity; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Helix\Asana\Base\AbstractEntity; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Adds helpers to entities with fields that have to mutated through `POST` after creation. |
|
10
|
|
|
* |
|
11
|
|
|
* @mixin AbstractEntity |
|
12
|
|
|
*/ |
|
13
|
|
|
trait PostMutatorTrait { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param string $addPath |
|
17
|
|
|
* @param array $data |
|
18
|
|
|
* @param string $field |
|
19
|
|
|
* @param array $diff |
|
20
|
|
|
* @return $this |
|
21
|
|
|
*/ |
|
22
|
|
|
private function _addWithPost (string $addPath, array $data, string $field, array $diff) { |
|
23
|
|
|
if ($this->hasGid()) { |
|
24
|
|
|
return $this->_setWithPost($addPath, $data, $field); |
|
25
|
|
|
} |
|
26
|
|
|
return $this->_set($field, array_merge($this->data[$field] ?? [], array_values($diff))); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $rmPath |
|
31
|
|
|
* @param array $data |
|
32
|
|
|
* @param string $field |
|
33
|
|
|
* @param array|Closure $diff An array to diff, or a filter closure. |
|
34
|
|
|
* @return $this |
|
35
|
|
|
*/ |
|
36
|
|
|
private function _removeWithPost (string $rmPath, array $data, string $field, $diff) { |
|
37
|
|
|
if ($this->hasGid()) { |
|
38
|
|
|
return $this->_setWithPost($rmPath, $data, $field); |
|
39
|
|
|
} |
|
40
|
|
|
elseif (is_array($diff)) { |
|
41
|
|
|
return $this->_set($field, array_values(array_diff($this->data[$field] ?? [], $diff))); |
|
42
|
|
|
} |
|
43
|
|
|
return $this->_set($field, array_values(array_filter($this->data[$field] ?? [], $diff))); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Sets/reloads data via `POST` for existing entities. Otherwise stages a value. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $path |
|
50
|
|
|
* @param array $data |
|
51
|
|
|
* @param string $field |
|
52
|
|
|
* @param mixed $value Ignored for existing entities. |
|
53
|
|
|
* @return $this |
|
54
|
|
|
* @internal |
|
55
|
|
|
*/ |
|
56
|
|
|
private function _setWithPost (string $path, array $data, string $field, $value = null) { |
|
57
|
|
|
if ($this->hasGid()) { |
|
58
|
|
|
/** @var array $remote */ |
|
59
|
|
|
$remote = $this->api->post($path, $data, ['fields' => static::OPT_FIELDS[$field] ?? $field]); |
|
60
|
|
|
if (array_key_exists($field, $this->data)) { |
|
61
|
|
|
$this->_setField($field, $remote[$field]); |
|
62
|
|
|
/** @var AbstractEntity $that */ |
|
63
|
|
|
$that = $this; |
|
64
|
|
|
$this->api->getPool()->add($that); |
|
65
|
|
|
} |
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
return $this->_set($field, $value); |
|
69
|
|
|
} |
|
70
|
|
|
} |