1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helix\Asana\Task; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use Countable; |
7
|
|
|
use Generator; |
8
|
|
|
use Helix\Asana\Base\Data; |
9
|
|
|
use Helix\Asana\Task; |
10
|
|
|
use IteratorAggregate; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Custom field value adapter for tasks. |
14
|
|
|
* |
15
|
|
|
* Field access is by GID. |
16
|
|
|
* |
17
|
|
|
* Enum values are set by option GID. |
18
|
|
|
*/ |
19
|
|
|
class FieldEntries extends Data implements ArrayAccess, Countable, IteratorAggregate { |
20
|
|
|
|
21
|
|
|
protected const MAP = [ |
22
|
|
|
'*' => FieldEntry::class |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var FieldEntry[] |
27
|
|
|
*/ |
28
|
|
|
protected $data = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Task |
32
|
|
|
*/ |
33
|
|
|
protected $task; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* `[ field gid => type ]` |
37
|
|
|
* |
38
|
|
|
* @var string[] |
39
|
|
|
*/ |
40
|
|
|
protected $types = []; |
41
|
|
|
|
42
|
|
|
public function __construct (Task $task, array $data = []) { |
43
|
|
|
$this->task = $task; |
44
|
|
|
parent::__construct($task, $data); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array[] $values |
49
|
|
|
*/ |
50
|
|
|
protected function _setData (array $values): void { |
51
|
|
|
// ensure data is keyed by field gid |
52
|
|
|
$values = array_combine(array_column($values, 'gid'), $values); |
53
|
|
|
parent::_setData($values); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return int |
58
|
|
|
*/ |
59
|
|
|
public function count () { |
60
|
|
|
return count($this->data); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getDiff (): array { |
64
|
|
|
return array_map(function(FieldEntry $value) { |
65
|
|
|
return $value->getValue(); |
66
|
|
|
}, array_intersect_key($this->data, $this->diff)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $fieldGid |
71
|
|
|
* @return FieldEntry |
72
|
|
|
*/ |
73
|
|
|
public function getField ($fieldGid) { |
74
|
|
|
return $this->data[$fieldGid]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return Generator |
79
|
|
|
*/ |
80
|
|
|
public function getIterator () { |
81
|
|
|
foreach ($this->data as $gid => $field) { |
82
|
|
|
yield $gid => $field->getValue(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $fieldGid |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function offsetExists ($fieldGid) { |
91
|
|
|
return array_key_exists($fieldGid, $this->data); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $fieldGid |
96
|
|
|
* @return null|number|string |
97
|
|
|
*/ |
98
|
|
|
public function offsetGet ($fieldGid) { |
99
|
|
|
return $this->data[$fieldGid]->getValue(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $fieldGid |
104
|
|
|
* @param null|number|string $value |
105
|
|
|
*/ |
106
|
|
|
public function offsetSet ($fieldGid, $value) { |
107
|
|
|
$this->data[$fieldGid]->setValue($value); |
108
|
|
|
$this->diff[$fieldGid] = true; |
109
|
|
|
$this->task->diff['custom_fields'] = true; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Custom fields cannot be "removed" through this. This only sets them to `null`. |
114
|
|
|
* |
115
|
|
|
* @param string $fieldGid |
116
|
|
|
*/ |
117
|
|
|
public function offsetUnset ($fieldGid) { |
118
|
|
|
$this->offsetSet($fieldGid, null); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |