1
|
|
|
<?php |
2
|
|
|
namespace FloSports\ScaleEngine\Model\Factory; |
3
|
|
|
|
4
|
|
|
use DateTime; |
5
|
|
|
use DateTimeZone; |
6
|
|
|
use FloSports\ScaleEngine\ScaleEngineClient; |
7
|
|
|
use Guzzle\Service\Resource\Model; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Collects shared functionality used by all ScaleEngine models. |
11
|
|
|
*/ |
12
|
|
|
abstract class AbstractScaleEngineModelFactory |
13
|
|
|
{ |
14
|
|
|
/** @type string The model class name. **/ |
15
|
|
|
private $_model; |
16
|
|
|
|
17
|
|
|
/** @type array A map of keys from ScaleEngine's API to exposed model. **/ |
18
|
|
|
private $_keyMap; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Create the model factory for a specific type of model. |
22
|
|
|
* |
23
|
|
|
* @param string $model The full class name for the model to construct. |
24
|
|
|
* @param array $keyMap A map of keys from ScaleEngine's API to exposed model. |
25
|
|
|
*/ |
26
|
|
|
public function __construct($model, array $keyMap) |
27
|
|
|
{ |
28
|
|
|
$this->_model = $model; |
29
|
|
|
$this->_keyMap = $keyMap; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create a Ticket model using the provided data. |
34
|
|
|
* |
35
|
|
|
* The data given will be parsed and any missing keys will result in |
36
|
|
|
* failure. |
37
|
|
|
* |
38
|
|
|
* @param array $data The data to use to populate the model. |
39
|
|
|
* @return \Guzzle\Service\Resource\Model The parsed model. |
40
|
|
|
*/ |
41
|
|
|
public function create(array $data) |
42
|
|
|
{ |
43
|
|
|
$model = $this->_model; |
44
|
|
|
return $model::fromConfig($this->parseData($data), [], $this->_keyMap); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Parse the data given into correct types and changes keys based on the |
49
|
|
|
* key map. |
50
|
|
|
* |
51
|
|
|
* @param array $data The ticket data to parse. |
52
|
|
|
* @return array The data after being converted to the parsed form. |
53
|
|
|
*/ |
54
|
|
|
abstract public function parseData(array $data); |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Convert a date from ScaleEngine's format into a PHP DateTime. |
58
|
|
|
* |
59
|
|
|
* ScaleEngine uses an all-zeros response for null dates, and this will |
60
|
|
|
* convert those into null itself. |
61
|
|
|
* |
62
|
|
|
* This also considers that ScaleEngine uses UTC for its timezone and makes |
63
|
|
|
* sure that the time returned is correct considering that behavior. |
64
|
|
|
* |
65
|
|
|
* @param string $date The date as returned from ScaleEngine. |
66
|
|
|
* @return DateTime The converted date/time object. |
67
|
|
|
*/ |
68
|
|
|
public function convertDate($date) |
69
|
|
|
{ |
70
|
|
|
if ($date === ScaleEngineClient::SCALEENGINE_NULL_DATE) { |
71
|
|
|
return null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$dateTime = DateTime::createFromFormat( |
75
|
|
|
ScaleEngineClient::SCALEENGINE_DATE_FORMAT, |
76
|
|
|
$date, |
77
|
|
|
new DateTimeZone(ScaleEngineClient::SCALEENGINE_TIMEZONE) |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
return $dateTime ?: null; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|