Passed
Push — master ( d25a5e...bcdfe5 )
by y
01:48
created

CustomValues::getValue()   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 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
    public function __construct (Task $task, array $fields) {
38
        parent::__construct($task);
39
        foreach ($fields as $field) {
40
            $gid = $this->gids[$field['name']] = $field['gid'];
41
            if ($field['type'] === CustomField::TYPE_ENUM) {
42
                $this->optionNames[$gid] = array_column($field['enum_options'], 'name', 'gid');
43
                $this->data[$gid] = $field['enum_value']['gid'];
44
            }
45
            elseif ($field['type'] === CustomField::TYPE_TEXT) {
46
                $this->data[$gid] = $field['text_value'];
47
            }
48
            elseif ($field['type'] === CustomField::TYPE_NUMBER) {
49
                $this->data[$gid] = $field['number_value'];
50
            }
51
        }
52
    }
53
54
    /**
55
     * @return int
56
     */
57
    public function count () {
58
        return count($this->data);
59
    }
60
61
    /**
62
     * @param string $fieldName
63
     * @return null|string
64
     */
65
    public function getGid (string $fieldName) {
66
        return $this->gids[$fieldName] ?? null;
67
    }
68
69
    /**
70
     * Field GIDs, keyed by name.
71
     *
72
     * @return string[]
73
     */
74
    public function getGids () {
75
        return $this->gids;
76
    }
77
78
    /**
79
     * @return Traversable
80
     */
81
    public function getIterator () {
82
        return new ArrayIterator($this->data);
83
    }
84
85
    /**
86
     * @param string $fieldGid
87
     * @return null|string
88
     */
89
    public function getName (string $fieldGid): ?string {
90
        return array_search($fieldGid, $this->gids) ?: null;
91
    }
92
93
    /**
94
     * Field names, keyed by GID.
95
     *
96
     * @return string[]
97
     */
98
    public function getNames () {
99
        return array_flip($this->gids);
100
    }
101
102
    /**
103
     * @param string $enumGid
104
     * @param string $optionName
105
     * @return null|string
106
     */
107
    public function getOptionGid (string $enumGid, string $optionName) {
108
        return array_search($optionName, $this->optionNames[$enumGid]) ?: null;
109
    }
110
111
    /**
112
     * Enum option GIDs, keyed by name.
113
     *
114
     * @param string $enumGid
115
     * @return string[]
116
     */
117
    public function getOptionGids (string $enumGid) {
118
        return array_flip($this->optionNames[$enumGid]);
119
    }
120
121
    /**
122
     * @param string $enumGid
123
     * @param string $optionGid
124
     * @return null|string
125
     */
126
    public function getOptionName (string $enumGid, string $optionGid): ?string {
127
        return $this->optionNames[$enumGid][$optionGid] ?? null;
128
    }
129
130
    /**
131
     * Enum option names, keyed by GID.
132
     *
133
     * @param string $enumGid
134
     * @return string[]
135
     */
136
    public function getOptionNames (string $enumGid) {
137
        return $this->optionNames[$enumGid];
138
    }
139
140
    /**
141
     * @param string $fieldGid
142
     * @return null|number|string
143
     */
144
    public function getValue (string $fieldGid) {
145
        return $this->data[$fieldGid] ?? null;
146
    }
147
148
    /**
149
     * @param string $fieldGid
150
     * @return bool
151
     */
152
    public function offsetExists ($fieldGid) {
153
        return array_key_exists($fieldGid, $this->data);
154
    }
155
156
    /**
157
     * @param string $fieldGid
158
     * @return null|number|string
159
     */
160
    public function offsetGet ($fieldGid) {
161
        return $this->getValue($fieldGid);
162
    }
163
164
    /**
165
     * @param string $fieldGid
166
     * @param null|number|string $value
167
     */
168
    public function offsetSet ($fieldGid, $value) {
169
        $this->setValue($fieldGid, $value);
170
    }
171
172
    /**
173
     * Custom fields cannot be "removed" through this. This only sets them to `null`.
174
     *
175
     * @param string $fieldGid
176
     */
177
    public function offsetUnset ($fieldGid) {
178
        $this->setValue($fieldGid, null);
179
    }
180
181
    /**
182
     * @param string $fieldGid
183
     * @param null|number|string $value
184
     * @return $this
185
     */
186
    public function setValue (string $fieldGid, $value) {
187
        $this->data[$fieldGid] = $value;
188
        $this->diff[$fieldGid] = true;
189
        return $this;
190
    }
191
}