Passed
Push — master ( 0658f3...f968a7 )
by y
02:18
created

FieldEntries::offsetUnset()   A

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
nc 1
nop 1
dl 0
loc 2
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
    protected const MAP = [
20
        '*' => FieldEntry::class
21
    ];
22
23
    /**
24
     * Field entries, keyed by gid.
25
     *
26
     * @var FieldEntry[]
27
     */
28
    protected $data = [];
29
30
    /**
31
     * GIDs, keyed by field name.
32
     *
33
     * @var string[]
34
     */
35
    protected $gids = [];
36
37
    /**
38
     * Field names, keyed by GID.
39
     *
40
     * @var string[]
41
     */
42
    protected $names = [];
43
44
    /**
45
     * @var Task
46
     */
47
    protected $task;
48
49
    /**
50
     * `[ field gid => type ]`
51
     *
52
     * @var string[]
53
     */
54
    protected $types = [];
55
56
    public function __construct (Task $task, array $data = []) {
57
        $this->task = $task;
58
        parent::__construct($task, $data);
59
    }
60
61
    /**
62
     * @param $offset
63
     * @return null|string
64
     */
65
    protected function _toGid (string $offset) {
66
        if (isset($this->names[$offset])) {
67
            return $offset;
68
        }
69
        return $this->gids[$offset];
70
    }
71
72
    /**
73
     * @param array[] $values
74
     */
75
    protected function _setData (array $values): void {
76
        // ensure data is keyed by field gid
77
        $values = array_combine(array_column($values, 'gid'), $values);
78
79
        $this->gids = array_column($values, 'gid', 'name');
1 ignored issue
show
Bug introduced by
It seems like $values can also be of type false; however, parameter $input of array_column() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        $this->gids = array_column(/** @scrutinizer ignore-type */ $values, 'gid', 'name');
Loading history...
80
        $this->names = array_flip($this->gids);
81
82
        parent::_setData($values);
1 ignored issue
show
Bug introduced by
It seems like $values can also be of type false; however, parameter $data of Helix\Asana\Base\Data::_setData() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

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