Passed
Push — master ( e1698c...dba318 )
by y
02:23
created

CustomValues::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Helix\Asana\CustomField;
4
5
use ArrayAccess;
6
use Helix\Asana\Base\Data;
7
use Helix\Asana\CustomField;
8
9
/**
10
 * Custom field value adapter for entities.
11
 */
12
class CustomValues extends Data implements ArrayAccess {
13
14
    /**
15
     * @var string[][]
16
     */
17
    protected $enumNames = [];
18
19
    public function __construct ($caller, array $data = []) {
20
        parent::__construct($caller);
21
        foreach ($data as $field) {
22
            $gid = $field['gid'];
23
            if ($field['type'] === CustomField::TYPE_ENUM) {
24
                $this->enumNames[$gid] = array_column($field['enum_options'], 'name', 'gid');
25
                $this->data[$gid] = $field['enum_value']['gid'];
26
            }
27
            elseif ($field['type'] === CustomField::TYPE_TEXT) {
28
                $this->data[$gid] = $field['text_value'];
29
            }
30
            elseif ($field['type'] === CustomField::TYPE_NUMBER) {
31
                $this->data[$gid] = $field['number_value'];
32
            }
33
        }
34
    }
35
36
    /**
37
     * @param string $fieldGid
38
     * @param string $enumName
39
     * @return null|string
40
     */
41
    public function getEnumGid (string $fieldGid, string $enumName) {
42
        return array_search($enumName, $this->enumNames[$fieldGid]) ?: null;
43
    }
44
45
    /**
46
     * @param string $fieldGid
47
     * @param string $valueGid
48
     * @return string
49
     */
50
    public function getEnumName (string $fieldGid, string $valueGid) {
51
        return $this->enumNames[$fieldGid][$valueGid];
52
    }
53
54
    /**
55
     * @return string[][]
56
     */
57
    public function getEnumNames (): array {
58
        return $this->enumNames;
59
    }
60
61
    /**
62
     * @param string $fieldGid
63
     * @return null|number|string
64
     */
65
    public function getValue (string $fieldGid) {
66
        return $this->data[$fieldGid] ?? null;
67
    }
68
69
    /**
70
     * @param string $fieldGid
71
     * @return bool
72
     */
73
    public function offsetExists ($fieldGid) {
74
        return array_key_exists($fieldGid, $this->data);
75
    }
76
77
    /**
78
     * @param string $fieldGid
79
     * @return null|number|string
80
     */
81
    public function offsetGet ($fieldGid) {
82
        return $this->getValue($fieldGid);
83
    }
84
85
    /**
86
     * @param string $fieldGid
87
     * @param null|number|string $value
88
     */
89
    public function offsetSet ($fieldGid, $value) {
90
        $this->setValue($fieldGid, $value);
91
    }
92
93
    /**
94
     * @param string $fieldGid
95
     */
96
    public function offsetUnset ($fieldGid) {
97
        unset($this->data[$fieldGid]);
98
        $this->diff[$fieldGid] = true;
99
    }
100
101
    /**
102
     * @param string $fieldGid
103
     * @param null|number|string $value
104
     * @return $this
105
     */
106
    public function setValue (string $fieldGid, $value) {
107
        $this->data[$fieldGid] = $value;
108
        $this->diff[$fieldGid] = true;
109
        return $this;
110
    }
111
}