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