Passed
Push — master ( 15b43b...52c16b )
by y
01:45
created

FieldEntries::getDiff()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
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
    /**
90
     * @param $fieldGid
91
     * @return FieldEntry
92
     */
93
    public function getField ($fieldGid) {
94
        return $this->data[$fieldGid];
95
    }
96
97
    /**
98
     * @param string $name
99
     * @return string
100
     */
101
    public function getGid (string $name): string {
102
        return $this->gids[$name];
103
    }
104
105
    /**
106
     * @return string[]
107
     */
108
    public function getGids () {
109
        return $this->gids;
110
    }
111
112
    /**
113
     * @return Generator
114
     */
115
    public function getIterator () {
116
        foreach ($this->data as $gid => $field) {
117
            yield $gid => $field->getValue();
118
        }
119
    }
120
121
    public function getName (string $fieldGid) {
122
        return $this->names[$fieldGid];
123
    }
124
125
    /**
126
     * @return string[]
127
     */
128
    public function getNames () {
129
        return $this->names;
130
    }
131
132
    /**
133
     * @return array
134
     */
135
    public function getValues () {
136
        return iterator_to_array($this);
137
    }
138
139
    /**
140
     * @param string $offset
141
     * @return bool
142
     */
143
    public function offsetExists ($offset) {
144
        return isset($this->data[$this->_toGid($offset)]);
145
    }
146
147
    /**
148
     * @param string $offset
149
     * @return null|number|string
150
     */
151
    public function offsetGet ($offset) {
152
        return $this->data[$this->_toGid($offset)]->getValue();
153
    }
154
155
    /**
156
     * @param string $offset
157
     * @param null|number|string $value
158
     */
159
    public function offsetSet ($offset, $value) {
160
        $offset = $this->_toGid($offset);
161
        $this->data[$offset]->setValue($value);
162
        $this->diff[$offset] = true;
163
        $this->task->diff['custom_fields'] = true;
164
    }
165
166
    /**
167
     * Field entries cannot be "removed" through this. This only sets them to `null`.
168
     *
169
     * @param string $offset
170
     */
171
    public function offsetUnset ($offset) {
172
        $this->offsetSet($offset, null);
173
    }
174
175
    public function toArray (bool $diff = false): array {
176
        if ($diff) {
177
            return array_map(function(FieldEntry $entry) {
178
                return $entry->getValue();
179
            }, array_intersect_key($this->data, $this->diff));
180
        }
181
        return parent::toArray();
182
    }
183
184
}