Passed
Push — master ( 66162c...998eef )
by y
01:39
created

CustomValues::getCurrentOptionName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
3
namespace Helix\Asana\Task;
4
5
use ArrayAccess;
6
use ArrayIterator;
7
use Countable;
8
use Helix\Asana\Base\Data;
9
use Helix\Asana\CustomField;
10
use Helix\Asana\Task;
11
use IteratorAggregate;
12
use Traversable;
13
14
/**
15
 * Custom field value adapter for tasks.
16
 *
17
 * Field access is by GID.
18
 *
19
 * Enum values are set by option GID.
20
 */
21
class CustomValues extends Data implements ArrayAccess, Countable, IteratorAggregate {
22
23
    /**
24
     * `[ field name => gid ]`
25
     *
26
     * @var string[]
27
     */
28
    protected $gids = [];
29
30
    /**
31
     * `[ enum gid => option gid => option name ]`
32
     *
33
     * @var string[][]
34
     */
35
    protected $optionNames = [];
36
37
    /**
38
     * @var Task
39
     */
40
    protected $task;
41
42
    public function __construct (Task $task, array $fields) {
43
        parent::__construct($task);
44
        $this->task = $task;
45
        foreach ($fields as $field) {
46
            $gid = $this->gids[$field['name']] = $field['gid'];
47
            if ($field['type'] === CustomField::TYPE_ENUM) {
48
                $this->optionNames[$gid] = array_column($field['enum_options'], 'name', 'gid');
49
                $this->data[$gid] = $field['enum_value']['gid'];
50
            }
51
            elseif ($field['type'] === CustomField::TYPE_TEXT) {
52
                $this->data[$gid] = $field['text_value'];
53
            }
54
            elseif ($field['type'] === CustomField::TYPE_NUMBER) {
55
                $this->data[$gid] = $field['number_value'];
56
            }
57
        }
58
    }
59
60
    /**
61
     * @return int
62
     */
63
    public function count () {
64
        return count($this->data);
65
    }
66
67
    /**
68
     * @param string $enumGid
69
     * @return null|string
70
     */
71
    public function getCurrentOptionName (string $enumGid): ?string {
72
        return $this->optionNames[$enumGid][$this[$enumGid]] ?? null;
73
    }
74
75
    /**
76
     * @param string $fieldName
77
     * @return null|string
78
     */
79
    public function getGid (string $fieldName) {
80
        return $this->gids[$fieldName] ?? null;
81
    }
82
83
    /**
84
     * Field GIDs, keyed by name.
85
     *
86
     * @return string[]
87
     */
88
    public function getGids () {
89
        return $this->gids;
90
    }
91
92
    /**
93
     * @return Traversable
94
     */
95
    public function getIterator () {
96
        return new ArrayIterator($this->data);
97
    }
98
99
    /**
100
     * @param string $fieldGid
101
     * @return null|string
102
     */
103
    public function getName (string $fieldGid): ?string {
104
        return array_search($fieldGid, $this->gids) ?: null;
105
    }
106
107
    /**
108
     * Field names, keyed by GID.
109
     *
110
     * @return string[]
111
     */
112
    public function getNames () {
113
        return array_flip($this->gids);
114
    }
115
116
    /**
117
     * @param string $enumGid
118
     * @param string $optionName
119
     * @return null|string
120
     */
121
    public function getOptionGid (string $enumGid, string $optionName) {
122
        return array_search($optionName, $this->optionNames[$enumGid]) ?: null;
123
    }
124
125
    /**
126
     * Enum option GIDs, keyed by name.
127
     *
128
     * @param string $enumGid
129
     * @return string[]
130
     */
131
    public function getOptionGids (string $enumGid) {
132
        return array_flip($this->optionNames[$enumGid]);
133
    }
134
135
    /**
136
     * @param string $enumGid
137
     * @param string $optionGid
138
     * @return null|string
139
     */
140
    public function getOptionName (string $enumGid, string $optionGid): ?string {
141
        return $this->optionNames[$enumGid][$optionGid] ?? null;
142
    }
143
144
    /**
145
     * Enum option names, keyed by GID.
146
     *
147
     * @param string $enumGid
148
     * @return string[]
149
     */
150
    public function getOptionNames (string $enumGid) {
151
        return $this->optionNames[$enumGid];
152
    }
153
154
    /**
155
     * @param string $fieldGid
156
     * @return null|number|string
157
     */
158
    public function getValue (string $fieldGid) {
159
        return $this->data[$fieldGid] ?? null;
160
    }
161
162
    /**
163
     * @param string $fieldGid
164
     * @return bool
165
     */
166
    public function offsetExists ($fieldGid) {
167
        return array_key_exists($fieldGid, $this->data);
168
    }
169
170
    /**
171
     * @param string $fieldGid
172
     * @return null|number|string
173
     */
174
    public function offsetGet ($fieldGid) {
175
        return $this->getValue($fieldGid);
176
    }
177
178
    /**
179
     * @param string $fieldGid
180
     * @param null|number|string $value
181
     */
182
    public function offsetSet ($fieldGid, $value) {
183
        $this->setValue($fieldGid, $value);
184
    }
185
186
    /**
187
     * Custom fields cannot be "removed" through this. This only sets them to `null`.
188
     *
189
     * @param string $fieldGid
190
     */
191
    public function offsetUnset ($fieldGid) {
192
        $this->setValue($fieldGid, null);
193
    }
194
195
    /**
196
     * @param string $fieldGid
197
     * @param null|number|string $value
198
     * @return $this
199
     */
200
    public function setValue (string $fieldGid, $value) {
201
        $this->data[$fieldGid] = $value;
202
        $this->diff[$fieldGid] = true;
203
        $this->task->diff['custom_fields'] = true;
204
        return $this;
205
    }
206
}