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

FieldEntry::_setData()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 4
nop 1
dl 0
loc 21
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace Helix\Asana\Task;
4
5
use Helix\Asana\Base\Data;
6
use Helix\Asana\CustomField;
7
8
/**
9
 * A task's custom field entry.
10
 *
11
 * Enum values are set by option GID (recommended) or name.
12
 *
13
 * @method string   getGid      ()
14
 * @method string   getName     ()
15
 * @method null|int getPrecison ()
16
 * @method string   getType     ()
17
 */
18
class FieldEntry extends Data {
19
20
    /**
21
     * @var array
22
     */
23
    protected $data = [];
24
25
    /**
26
     * Strips Asana's beefy data array down to what we need.
27
     *
28
     * @param array $data
29
     */
30
    protected function _setData (array $data): void {
31
        if (isset($data['resource_subtype'])) {
32
            $tiny = array_intersect_key($data, array_flip([
33
                'gid',
34
                'name',
35
                'type',
36
                'precision',
37
                'enum_options',
38
                "{$data['type']}_value"
39
            ]));
40
            if (isset($tiny['enum_options'])) {
41
                $tiny['enum_options'] = array_map(function(array $option) {
42
                    return ['gid' => $option['gid'], 'name' => $option['name']];
43
                }, $tiny['enum_options']);
44
                if (isset($tiny['enum_value'])) {
45
                    $tiny['enum_value'] = ['gid' => $tiny['enum_value']['gid']];
46
                }
47
            }
48
            $data = $tiny;
49
        }
50
        parent::_setData($data);
51
    }
52
53
    /**
54
     * @param null|string $value
55
     * @return null|string
56
     */
57
    protected function _toEnumOptionGid ($value) {
58
        return $this->getEnumOptionValues()[$value] ?? $value;
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    public function getCurrentOptionName () {
65
        return $this->getEnumOptionNames()[$this->getValue()];
66
    }
67
68
    /**
69
     * Option names keyed by GID.
70
     *
71
     * @return string[]
72
     */
73
    public function getEnumOptionNames () {
74
        static $names = []; // shared
75
        $gid = $this->data['gid'];
76
        return $names[$gid] ?? $names[$gid] = array_column($this->data['enum_options'], 'name', 'gid');
77
    }
78
79
    /**
80
     * Option GIDs keyed by name.
81
     *
82
     * @return string[]
83
     */
84
    public function getEnumOptionValues () {
85
        static $values = []; // shared
86
        $gid = $this->data['gid'];
87
        return $values[$gid] ?? $values[$gid] = array_column($this->data['enum_options'], 'gid', 'name');
88
    }
89
90
    /**
91
     * @return null|number|string
92
     */
93
    public function getValue () {
94
        $type = $this->data['type'];
95
        if ($type === CustomField::TYPE_ENUM) {
96
            return $this->data['enum_value']['gid'] ?? null;
97
        }
98
        return $this->data["{$type}_value"];
99
    }
100
101
    /**
102
     * @return bool
103
     */
104
    final public function isEnum (): bool {
105
        return $this->getType() === CustomField::TYPE_ENUM;
106
    }
107
108
    /**
109
     * @return bool
110
     */
111
    final public function isNumber (): bool {
112
        return $this->getType() === CustomField::TYPE_NUMBER;
113
    }
114
115
    /**
116
     * @return bool
117
     */
118
    final public function isText (): bool {
119
        return $this->getType() === CustomField::TYPE_TEXT;
120
    }
121
122
    /**
123
     * @param null|number|string $value
124
     * @return $this
125
     */
126
    public function setValue ($value) {
127
        $type = $this->data['type'];
128
        $this->diff["{$type}_value"] = true;
129
        if ($type === CustomField::TYPE_ENUM) {
130
            $this->data['enum_value']['gid'] = $this->_toEnumOptionGid($value);
131
            return $this;
132
        }
133
        $this->data["{$type}_value"] = $value;
134
        return $this;
135
    }
136
137
}