Passed
Push — master ( 3c0c31...b6d585 )
by y
02:02
created

FieldEntries::_setField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
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
    public function __construct (Task $task, array $data = []) {
53
        $this->task = $task;
54
        parent::__construct($task, $data);
55
    }
56
57
    /**
58
     * @param string $i
59
     * @param array $data
60
     */
61
    protected function _setField (string $i, $data): void {
62
        /** @var FieldEntry $entry */
63
        $entry = $this->api->factory($this, FieldEntry::class, $data);
64
        $gid = $entry->getGid();
65
        $name = $entry->getName();
66
        $this->data[$gid] = $entry;
67
        $this->gids[$name] = $gid;
68
        $this->names[$gid] = $name;
69
    }
70
71
    /**
72
     * @param $offset
73
     * @return null|string
74
     */
75
    protected function _toGid (string $offset) {
76
        if (isset($this->names[$offset])) {
77
            return $offset;
78
        }
79
        return $this->gids[$offset];
80
    }
81
82
    /**
83
     * @return int
84
     */
85
    public function count () {
86
        return count($this->data);
87
    }
88
89
    public function getDiff (): array {
90
        return array_map(function(FieldEntry $value) {
91
            return $value->getValue();
92
        }, array_intersect_key($this->data, $this->diff));
93
    }
94
95
    /**
96
     * @param $fieldGid
97
     * @return FieldEntry
98
     */
99
    public function getField ($fieldGid) {
100
        return $this->data[$fieldGid];
101
    }
102
103
    /**
104
     * @param string $name
105
     * @return string
106
     */
107
    public function getGid (string $name): string {
108
        return $this->gids[$name];
109
    }
110
111
    /**
112
     * @return string[]
113
     */
114
    public function getGids () {
115
        return $this->gids;
116
    }
117
118
    /**
119
     * @return Generator
120
     */
121
    public function getIterator () {
122
        foreach ($this->data as $gid => $field) {
123
            yield $gid => $field->getValue();
124
        }
125
    }
126
127
    public function getName (string $fieldGid) {
128
        return $this->names[$fieldGid];
129
    }
130
131
    /**
132
     * @return string[]
133
     */
134
    public function getNames () {
135
        return $this->names;
136
    }
137
138
    /**
139
     * @return array
140
     */
141
    public function getValues () {
142
        return iterator_to_array($this);
143
    }
144
145
    /**
146
     * @param string $offset
147
     * @return bool
148
     */
149
    public function offsetExists ($offset) {
150
        return isset($this->data[$this->_toGid($offset)]);
151
    }
152
153
    /**
154
     * @param string $offset
155
     * @return null|number|string
156
     */
157
    public function offsetGet ($offset) {
158
        return $this->data[$this->_toGid($offset)]->getValue();
159
    }
160
161
    /**
162
     * @param string $offset
163
     * @param null|number|string $value
164
     */
165
    public function offsetSet ($offset, $value) {
166
        $offset = $this->_toGid($offset);
167
        $this->data[$offset]->setValue($value);
168
        $this->diff[$offset] = true;
169
        $this->task->diff['custom_fields'] = true;
170
    }
171
172
    /**
173
     * Field entries cannot be "removed" through this. This only sets them to `null`.
174
     *
175
     * @param string $offset
176
     */
177
    public function offsetUnset ($offset) {
178
        $this->offsetSet($offset, null);
179
    }
180
181
}