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 values. |
10
|
|
|
* |
11
|
|
|
* @method string getName() |
12
|
|
|
* @method null|int getPrecison() |
13
|
|
|
* @method string getType() |
14
|
|
|
*/ |
15
|
|
|
class FieldValue extends Data { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $data = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This can be either a Task or an Event, but we only need the generic type. |
24
|
|
|
* |
25
|
|
|
* @var Data |
26
|
|
|
*/ |
27
|
|
|
protected $owner; |
28
|
|
|
|
29
|
|
|
public function __construct (Data $owner, array $data = []) { |
30
|
|
|
$this->owner = $owner; |
31
|
|
|
parent::__construct($owner, $data); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public function getCurrentOptionName () { |
38
|
|
|
return $this->getEnumOptionNames()[$this->getValue()]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return mixed |
43
|
|
|
*/ |
44
|
|
|
public function getCustomFieldGid () { |
45
|
|
|
return $this->data['gid']; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Option names keyed by GID. |
50
|
|
|
* |
51
|
|
|
* @return string[] |
52
|
|
|
*/ |
53
|
|
|
public function getEnumOptionNames () { |
54
|
|
|
static $names = []; // shared |
55
|
|
|
$gid = $this->data['gid']; |
56
|
|
|
return $names[$gid] ?? $names[$gid] = array_column($this->data['enum_options'], 'name', 'gid'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Option GIDs keyed by name. |
61
|
|
|
* |
62
|
|
|
* @return string[] |
63
|
|
|
*/ |
64
|
|
|
public function getEnumOptionValues () { |
65
|
|
|
static $values = []; // shared |
66
|
|
|
$gid = $this->data['gid']; |
67
|
|
|
return $values[$gid] ?? $values[$gid] = array_column($this->data['enum_options'], 'gid', 'name'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return null|number|string |
72
|
|
|
*/ |
73
|
|
|
public function getValue () { |
74
|
|
|
$type = $this->data['type']; |
75
|
|
|
if ($type === CustomField::TYPE_ENUM) { |
76
|
|
|
return $this->data['enum_value']['gid'] ?? null; |
77
|
|
|
} |
78
|
|
|
return $this->data["{$type}_value"]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
final public function isEnum (): bool { |
82
|
|
|
return $this->getType() === CustomField::TYPE_ENUM; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
final public function isNumber (): bool { |
86
|
|
|
return $this->getType() === CustomField::TYPE_NUMBER; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
final public function isText (): bool { |
90
|
|
|
return $this->getType() === CustomField::TYPE_TEXT; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param null|number|string $value |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function setValue ($value) { |
98
|
|
|
$type = $this->data['type']; |
99
|
|
|
$this->diff["{$type}_value"] = true; |
100
|
|
|
if ($type === CustomField::TYPE_ENUM) { |
101
|
|
|
$this->data['enum_value']['gid'] = $value; |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
$this->data["{$type}_value"] = $value; |
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Strips Asana's beefy data array down to what we need. |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
public function toArray (): array { |
114
|
|
|
// only strip if needed. |
115
|
|
|
if (!isset($this->data['resource_subtype'])) { |
116
|
|
|
return $this->data; |
117
|
|
|
} |
118
|
|
|
$tiny = array_intersect_key($this->data, array_flip([ |
119
|
|
|
'gid', |
120
|
|
|
'name', |
121
|
|
|
'type', |
122
|
|
|
'precision', |
123
|
|
|
'enum_options', |
124
|
|
|
"{$this->data['type']}_value" |
125
|
|
|
])); |
126
|
|
|
if (isset($tiny['enum_options'])) { |
127
|
|
|
$tiny['enum_options'] = array_map(function(array $option) { |
128
|
|
|
return ['gid' => $option['gid'], 'name' => $option['name']]; |
129
|
|
|
}, $tiny['enum_options']); |
130
|
|
|
if (isset($tiny['enum_value'])) { |
131
|
|
|
$tiny['enum_value'] = ['gid' => $tiny['enum_value']['gid']]; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
return $tiny; |
135
|
|
|
} |
136
|
|
|
} |