Passed
Push — master ( 9ec6c5...85fa92 )
by y
02:17
created

FieldEntry::getValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 10
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
 * @method string   getGid      ()
12
 * @method string   getName     ()
13
 * @method null|int getPrecison ()
14
 * @method string   getType     ()
15
 */
16
class FieldEntry extends Data {
17
18
    /**
19
     * @var array
20
     */
21
    protected $data = [];
22
23
    /**
24
     * @return string
25
     */
26
    public function getCurrentOptionName () {
27
        return $this->getEnumOptionNames()[$this->getValue()];
28
    }
29
30
    /**
31
     * Option names keyed by GID.
32
     *
33
     * @return string[]
34
     */
35
    public function getEnumOptionNames () {
36
        static $names = []; // shared
37
        $gid = $this->data['gid'];
38
        return $names[$gid] ?? $names[$gid] = array_column($this->data['enum_options'], 'name', 'gid');
39
    }
40
41
    /**
42
     * Option GIDs keyed by name.
43
     *
44
     * @return string[]
45
     */
46
    public function getEnumOptionValues () {
47
        static $values = []; // shared
48
        $gid = $this->data['gid'];
49
        return $values[$gid] ?? $values[$gid] = array_column($this->data['enum_options'], 'gid', 'name');
50
    }
51
52
    /**
53
     * @return null|number|string
54
     */
55
    public function getValue () {
56
        $type = $this->data['type'];
57
        if ($type === CustomField::TYPE_ENUM) {
58
            return $this->data['enum_value']['gid'] ?? null;
59
        }
60
        return $this->data["{$type}_value"];
61
    }
62
63
    final public function isEnum (): bool {
64
        return $this->getType() === CustomField::TYPE_ENUM;
65
    }
66
67
    final public function isNumber (): bool {
68
        return $this->getType() === CustomField::TYPE_NUMBER;
69
    }
70
71
    final public function isText (): bool {
72
        return $this->getType() === CustomField::TYPE_TEXT;
73
    }
74
75
    /**
76
     * @param null|number|string $value
77
     * @return $this
78
     */
79
    public function setValue ($value) {
80
        $type = $this->data['type'];
81
        $this->diff["{$type}_value"] = true;
82
        if ($type === CustomField::TYPE_ENUM) {
83
            $this->data['enum_value']['gid'] = $value;
84
            return $this;
85
        }
86
        $this->data["{$type}_value"] = $value;
87
        return $this;
88
    }
89
90
    /**
91
     * Strips Asana's beefy data array down to what we need.
92
     *
93
     * @return array
94
     */
95
    public function toArray (): array {
96
        // only strip if needed.
97
        if (!isset($this->data['resource_subtype'])) {
98
            return $this->data;
99
        }
100
        $tiny = array_intersect_key($this->data, array_flip([
101
            'gid',
102
            'name',
103
            'type',
104
            'precision',
105
            'enum_options',
106
            "{$this->data['type']}_value"
107
        ]));
108
        if (isset($tiny['enum_options'])) {
109
            $tiny['enum_options'] = array_map(function(array $option) {
110
                return ['gid' => $option['gid'], 'name' => $option['name']];
111
            }, $tiny['enum_options']);
112
            if (isset($tiny['enum_value'])) {
113
                $tiny['enum_value'] = ['gid' => $tiny['enum_value']['gid']];
114
            }
115
        }
116
        return $tiny;
117
    }
118
}