1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/allejo/PhpPulse/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\DaPulse\Objects; |
9
|
|
|
|
10
|
|
|
use allejo\DaPulse\Exceptions\ColumnNotFoundException; |
11
|
|
|
use allejo\DaPulse\Exceptions\HttpException; |
12
|
|
|
use allejo\DaPulse\Exceptions\InvalidObjectException; |
13
|
|
|
use allejo\DaPulse\PulseColumn; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The base class used for column values belonging to a specified class |
17
|
|
|
* |
18
|
|
|
* @internal |
19
|
|
|
* @package allejo\DaPulse\Objects |
20
|
|
|
* @since 0.1.0 |
21
|
|
|
*/ |
22
|
|
|
abstract class PulseColumnValue extends ApiObject |
23
|
|
|
{ |
24
|
|
|
const API_PREFIX = "boards"; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The default return value for getValue() |
28
|
|
|
* |
29
|
|
|
* @internal |
30
|
|
|
*/ |
31
|
|
|
const DEFAULT_VALUE = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The ID of the parent board that this column's Pulse belongs to. |
35
|
|
|
* |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
protected $board_id; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The ID of the current column. This is a unique identifier when accessing columns through the API. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $column_id; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The ID of the Pulse this column value belongs to |
49
|
|
|
* |
50
|
|
|
* @var int |
51
|
|
|
*/ |
52
|
|
|
protected $pulse_id; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The value that this column has. The data type can be an integer, string, or DateTime depending on the column type |
56
|
|
|
* |
57
|
|
|
* @var mixed |
58
|
|
|
*/ |
59
|
|
|
protected $column_value; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* This constructor only accepts an array of the data regarding a specific column |
63
|
|
|
* |
64
|
|
|
* @internal |
65
|
|
|
* |
66
|
|
|
* @param array $array An associative array containing information regarding a column's value |
67
|
|
|
* |
68
|
|
|
* @since 0.1.0 |
69
|
|
|
* |
70
|
|
|
* @throws \InvalidArgumentException An ID is given when an instance of this object can only be created from an |
71
|
|
|
* array of existing data |
72
|
|
|
*/ |
73
|
42 |
|
public function __construct ($array) |
74
|
|
|
{ |
75
|
42 |
|
$this->arrayConstructionOnly = true; |
76
|
|
|
|
77
|
42 |
|
parent::__construct($array); |
78
|
42 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* This function must be overloaded by child classes solely calling this function to allow each implementation to |
82
|
|
|
* have its own phpDocs. |
83
|
|
|
* |
84
|
|
|
* @throws ColumnNotFoundException |
85
|
|
|
*/ |
86
|
21 |
|
public function getValue () |
87
|
|
|
{ |
88
|
21 |
|
$this->lazyLoad(); |
89
|
|
|
|
90
|
20 |
|
if ($this->isNullValue()) |
91
|
|
|
{ |
92
|
7 |
|
return static::DEFAULT_VALUE; |
93
|
|
|
} |
94
|
|
|
|
95
|
16 |
|
if (!isset($this->column_value)) |
96
|
|
|
{ |
97
|
8 |
|
$this->setValue($this->jsonResponse); |
98
|
|
|
} |
99
|
|
|
|
100
|
16 |
|
return $this->column_value; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Create the appropriate object based on the type of column |
105
|
|
|
* |
106
|
|
|
* @internal |
107
|
|
|
* |
108
|
|
|
* @param string $type The type of column as specified by DaPulse's API; i.e. 'text', 'date', 'status', 'person' |
109
|
|
|
* @param array $data An associative array containing data regarding the column |
110
|
|
|
* |
111
|
|
|
* @since 0.1.0 |
112
|
|
|
* |
113
|
|
|
* @todo Fix the hardcoded "color" case statement. See: https://github.com/allejo/PhpPulse/issues/5 |
114
|
|
|
* |
115
|
|
|
* @throws InvalidObjectException |
116
|
|
|
* |
117
|
|
|
* @return PulseColumnStatusValue|PulseColumnDateValue|PulseColumnNumericValue|PulseColumnPersonValue|PulseColumnTagValue|PulseColumnTextValue|PulseColumnTimelineValue |
118
|
|
|
*/ |
119
|
43 |
|
public static function _createColumnType ($type, $data) |
120
|
|
|
{ |
121
|
|
|
switch ($type) |
122
|
|
|
{ |
123
|
43 |
|
case PulseColumn::Text: |
124
|
4 |
|
return (new PulseColumnTextValue($data)); |
125
|
|
|
|
126
|
39 |
|
case "color": |
127
|
39 |
|
case PulseColumn::Status: |
128
|
6 |
|
return (new PulseColumnStatusValue($data)); |
129
|
|
|
|
130
|
33 |
|
case PulseColumn::Numeric: |
131
|
7 |
|
return (new PulseColumnNumericValue($data)); |
132
|
|
|
|
133
|
26 |
|
case PulseColumn::Person: |
134
|
9 |
|
return (new PulseColumnPersonValue($data)); |
135
|
|
|
|
136
|
17 |
|
case PulseColumn::Date: |
137
|
8 |
|
return (new PulseColumnDateValue($data)); |
138
|
|
|
|
139
|
9 |
|
case PulseColumn::Timeline: |
140
|
5 |
|
return (new PulseColumnTimelineValue($data)); |
141
|
|
|
|
142
|
4 |
|
case PulseColumn::Tag: |
143
|
3 |
|
return (new PulseColumnTagValue($data)); |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
throw new InvalidObjectException("'$type' is an unsupported column type to modify."); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Check whether to return null because a column's value does not exist. |
151
|
|
|
* |
152
|
|
|
* @return bool True if the column does not have a value |
153
|
|
|
*/ |
154
|
20 |
|
protected function isNullValue () |
155
|
|
|
{ |
156
|
20 |
|
return (!isset($this->column_value) && is_null($this->jsonResponse["value"])); |
157
|
|
|
} |
158
|
|
|
|
159
|
21 |
|
protected function lazyLoad () |
160
|
|
|
{ |
161
|
21 |
|
if (!array_key_exists('value', $this->jsonResponse) && is_null($this->column_value)) |
162
|
|
|
{ |
163
|
3 |
|
$url = sprintf("%s/%d/columns/%s/value.json", self::apiEndpoint("boards"), $this->board_id, $this->column_id); |
164
|
|
|
$params = [ |
165
|
3 |
|
"pulse_id" => $this->pulse_id |
166
|
|
|
]; |
167
|
|
|
|
168
|
|
|
try |
169
|
|
|
{ |
170
|
3 |
|
$results = self::sendGet($url, $params); |
171
|
|
|
} |
172
|
1 |
|
catch (HttpException $e) |
173
|
|
|
{ |
174
|
1 |
|
throw new ColumnNotFoundException("The '$this->column_id' column could not be found"); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
// Store our value inside of jsonResponse so all of the respective objects can treat the data the same |
178
|
|
|
// as when accessed through a PulseBoard |
179
|
2 |
|
$this->jsonResponse['value'] = $results['value']; |
180
|
|
|
} |
181
|
20 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Cast and set the appropriate value for this column |
185
|
|
|
* |
186
|
|
|
* @param $response |
187
|
|
|
*/ |
188
|
|
|
abstract protected function setValue ($response); |
189
|
|
|
} |
190
|
|
|
|