FieldEntries::getGids()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
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 (recommended) or name.
16
 */
17
class FieldEntries extends Data implements ArrayAccess, Countable, IteratorAggregate {
18
19
    /**
20
     * Field entries, keyed by gid.
21
     *
22
     * @var FieldEntry[]
23
     */
24
    protected $data = [];
25
26
    /**
27
     * GIDs, keyed by field name.
28
     *
29
     * @var string[]
30
     */
31
    protected $gids = [];
32
33
    /**
34
     * Field names, keyed by GID.
35
     *
36
     * @var string[]
37
     */
38
    protected $names = [];
39
40
    /**
41
     * @var Task
42
     */
43
    protected $task;
44
45
    /**
46
     * `[ field gid => type ]`
47
     *
48
     * @var string[]
49
     */
50
    protected $types = [];
51
52
    /**
53
     * @param Task $task
54
     * @param array $data
55
     */
56
    public function __construct (Task $task, array $data = []) {
57
        $this->task = $task;
58
        parent::__construct($task, $data);
59
    }
60
61
    /**
62
     * @param string $gid
63
     * @param mixed $unused
64
     * @internal called by an entry
65
     */
66
    final public function __set (string $gid, $unused): void {
67
        $this->diff[$gid] = true;
68
        $this->task->diff['custom_fields'] = true;
69
    }
70
71
    /**
72
     * @param mixed $unused
73
     * @internal called by the task
74
     */
75
    final public function __unset ($unused): void {
76
        $this->diff = [];
77
        foreach ($this->data as $entry) {
78
            $entry->diff = [];
79
        }
80
    }
81
82
    /**
83
     * @param string $i
84
     * @param array $data
85
     */
86
    protected function _setField (string $i, $data): void {
87
        /** @var FieldEntry $entry */
88
        $entry = $this->api->factory($this, FieldEntry::class, $data);
89
        $gid = $entry->getGid();
90
        $name = $entry->getName();
91
        $this->data[$gid] = $entry;
92
        $this->gids[$name] = $gid;
93
        $this->names[$gid] = $name;
94
    }
95
96
    /**
97
     * @param string $offset
98
     * @return string
99
     */
100
    protected function _toGid (string $offset): string {
101
        return $this->gids[$offset] ?? $offset;
102
    }
103
104
    /**
105
     * @return int
106
     */
107
    final public function count (): int {
108
        return count($this->data);
109
    }
110
111
    /**
112
     * @param $fieldGid
113
     * @return FieldEntry
114
     */
115
    public function getField ($fieldGid) {
116
        return $this->data[$fieldGid];
117
    }
118
119
    /**
120
     * @param string $name
121
     * @return string
122
     */
123
    final public function getGid (string $name): string {
124
        return $this->gids[$name];
125
    }
126
127
    /**
128
     * @return string[]
129
     */
130
    final public function getGids () {
131
        return $this->gids;
132
    }
133
134
    /**
135
     * @return Generator
136
     */
137
    public function getIterator () {
138
        foreach ($this->data as $gid => $field) {
139
            yield $gid => $field->getValue();
140
        }
141
    }
142
143
    /**
144
     * @param string $fieldGid
145
     * @return string
146
     */
147
    final public function getName (string $fieldGid): string {
148
        return $this->names[$fieldGid];
149
    }
150
151
    /**
152
     * @return string[]
153
     */
154
    final public function getNames () {
155
        return $this->names;
156
    }
157
158
    /**
159
     * @return array
160
     */
161
    final public function getValues (): array {
162
        return iterator_to_array($this);
163
    }
164
165
    /**
166
     * @param string $offset
167
     * @return bool
168
     */
169
    final public function offsetExists ($offset): bool {
170
        return isset($this->data[$this->_toGid($offset)]);
171
    }
172
173
    /**
174
     * @param string $offset
175
     * @return null|number|string
176
     */
177
    final public function offsetGet ($offset) {
178
        return $this->data[$this->_toGid($offset)]->getValue();
179
    }
180
181
    /**
182
     * @param string $offset
183
     * @param null|number|string $value
184
     */
185
    final public function offsetSet ($offset, $value): void {
186
        $this->data[$this->_toGid($offset)]->setValue($value);
187
    }
188
189
    /**
190
     * Field entries cannot be "removed" through this. This only sets them to `null`.
191
     *
192
     * @param string $offset
193
     */
194
    final public function offsetUnset ($offset): void {
195
        $this->offsetSet($offset, null);
196
    }
197
198
    public function toArray (bool $diff = false): array {
199
        if ($diff) {
200
            return array_map(function(FieldEntry $entry) {
201
                return $entry->getValue();
202
            }, array_intersect_key($this->data, $this->diff));
203
        }
204
        return parent::toArray();
205
    }
206
207
}