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
|
|
|
* @param \stdClass|array|null $parameters |
22
|
|
|
*/ |
23
|
|
|
public function __construct($parameters = null) |
24
|
|
|
{ |
25
|
|
|
if (!$parameters) { |
26
|
|
|
return; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if ($parameters instanceof \stdClass) { |
30
|
|
|
$parameters = get_object_vars($parameters); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->build($parameters); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $parameters |
38
|
|
|
*/ |
39
|
|
|
public function build(array $parameters) |
40
|
|
|
{ |
41
|
|
|
foreach ($parameters as $property => $value) { |
42
|
|
|
$property = static::convertToCamelCase($property); |
43
|
|
|
|
44
|
|
|
if (property_exists($this, $property)) { |
45
|
|
|
$this->$property = $value; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public function toArray() |
54
|
|
|
{ |
55
|
|
|
$settings = []; |
56
|
|
|
$called = get_called_class(); |
57
|
|
|
|
58
|
|
|
$reflection = new \ReflectionClass($called); |
59
|
|
|
$properties = $reflection->getProperties(\ReflectionProperty::IS_PUBLIC); |
60
|
|
|
|
61
|
|
|
foreach ($properties as $property) { |
62
|
|
|
$prop = $property->getName(); |
63
|
|
|
if (isset($this->$prop) && $property->class == $called) { |
64
|
|
|
$settings[self::convertToSnakeCase($prop)] = $this->$prop; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $settings; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string|null $date DateTime string |
73
|
|
|
* |
74
|
|
|
* @return string|null DateTime in ISO8601 format |
75
|
|
|
*/ |
76
|
|
|
protected static function convertDateTime($date) |
77
|
|
|
{ |
78
|
|
|
if (!$date) { |
|
|
|
|
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$date = new \DateTime($date); |
83
|
|
|
$date->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
84
|
|
|
|
85
|
|
|
return $date->format(\DateTime::ISO8601); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $str Snake case string |
90
|
|
|
* |
91
|
|
|
* @return string Camel case string |
92
|
|
|
*/ |
93
|
|
|
protected static function convertToCamelCase($str) |
94
|
|
|
{ |
95
|
|
|
$callback = function ($match) { |
96
|
|
|
return strtoupper($match[2]); |
97
|
|
|
}; |
98
|
|
|
|
99
|
|
|
return lcfirst(preg_replace_callback('/(^|_)([a-z])/', $callback, $str)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param $str Camel case string |
104
|
|
|
* |
105
|
|
|
* @return string Snake case string |
106
|
|
|
*/ |
107
|
|
|
protected static function convertToSnakeCase($str) |
108
|
|
|
{ |
109
|
|
|
return strtolower(implode('_', preg_split('/(?=[A-Z])/', $str))); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
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: