1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the DigitalOceanV2 library. |
5
|
|
|
* |
6
|
|
|
* (c) Antoine Corcy <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace DigitalOceanV2\Entity; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Antoine Corcy <[email protected]> |
16
|
|
|
* @author Graham Campbell <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
abstract class AbstractEntity |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The model's attributes. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $attributes = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \stdClass|array|null $parameters |
29
|
|
|
*/ |
30
|
|
|
public function __construct($parameters = null) |
31
|
|
|
{ |
32
|
|
|
if (!$parameters) { |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($parameters instanceof \stdClass) { |
37
|
|
|
$parameters = get_object_vars($parameters); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->build($parameters); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $parameters |
45
|
|
|
*/ |
46
|
|
|
public function build(array $parameters) |
47
|
|
|
{ |
48
|
|
|
foreach ($parameters as $property => $value) { |
49
|
|
|
$property = static::convertToCamelCase($property); |
50
|
|
|
|
51
|
|
|
if (property_exists($this, $property)) { |
52
|
|
|
$this->$property = $value; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function toArray() |
61
|
|
|
{ |
62
|
|
|
$settings = []; |
63
|
|
|
$called = get_called_class(); |
64
|
|
|
|
65
|
|
|
$reflection = new \ReflectionClass($called); |
66
|
|
|
$properties = $reflection->getProperties(\ReflectionProperty::IS_PUBLIC); |
67
|
|
|
|
68
|
|
|
foreach ($properties as $property) { |
69
|
|
|
$prop = $property->getName(); |
70
|
|
|
if (isset($this->$prop) && $property->class == $called) { |
71
|
|
|
$settings[self::convertToSnakeCase($prop)] = $this->$prop; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $settings; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string|null $date DateTime string |
80
|
|
|
* |
81
|
|
|
* @return string|null DateTime in ISO8601 format |
82
|
|
|
*/ |
83
|
|
|
protected static function convertDateTime($date) |
84
|
|
|
{ |
85
|
|
|
if (!$date) { |
|
|
|
|
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$date = new \DateTime($date); |
90
|
|
|
$date->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
91
|
|
|
|
92
|
|
|
return $date->format(\DateTime::ISO8601); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $str Snake case string |
97
|
|
|
* |
98
|
|
|
* @return string Camel case string |
99
|
|
|
*/ |
100
|
|
|
protected static function convertToCamelCase($str) |
101
|
|
|
{ |
102
|
|
|
$callback = function ($match) { |
103
|
|
|
return strtoupper($match[2]); |
104
|
|
|
}; |
105
|
|
|
|
106
|
|
|
return lcfirst(preg_replace_callback('/(^|_)([a-z])/', $callback, $str)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param $str Camel case string |
111
|
|
|
* |
112
|
|
|
* @return string Snake case string |
113
|
|
|
*/ |
114
|
|
|
protected static function convertToSnakeCase($str) |
115
|
|
|
{ |
116
|
|
|
return strtolower(implode('_', preg_split('/(?=[A-Z])/', $str))); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Dynamically retrieve attributes on the model. |
121
|
|
|
* |
122
|
|
|
* @param string $key |
123
|
|
|
* @return mixed |
124
|
|
|
*/ |
125
|
|
|
public function __get($key) |
126
|
|
|
{ |
127
|
|
|
return isset($this->attributes[$key]) ? $this->attributes[$key] : null; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Dynamically set attributes on the model. |
132
|
|
|
* |
133
|
|
|
* @param string $key |
134
|
|
|
* @param mixed $value |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
|
|
public function __set($key, $value) |
138
|
|
|
{ |
139
|
|
|
$this->attributes[$key] = $value; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: