1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Util; |
4
|
|
|
|
5
|
|
|
class Util |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Copies the members of obj1 that have public getters to obj2 if there exists a public setter in obj2 of the same suffix, it will also copy public variables. |
9
|
|
|
* |
10
|
|
|
* @param object $obj1 |
11
|
|
|
* @param object $obj2 |
12
|
|
|
*/ |
13
|
40 |
|
public static function copy($obj1, $obj2) |
14
|
|
|
{ |
15
|
40 |
|
if (!is_object($obj1)) { |
16
|
|
|
throw new \InvalidArgumentException('$obj1 must be an object, not '.gettype($obj1)); |
17
|
|
|
} |
18
|
40 |
|
if (!is_object($obj2)) { |
19
|
|
|
throw new \InvalidArgumentException('$obj2 must be an object, not '.gettype($obj2)); |
20
|
|
|
} |
21
|
40 |
|
$reflection1 = new \ReflectionObject($obj1); |
22
|
40 |
|
$reflection2 = new \ReflectionObject($obj2); |
23
|
40 |
|
self::copyMethods([$obj1, $obj2], $reflection1, $reflection2); |
24
|
40 |
|
self::copyProperties([$obj1, $obj2], $reflection1, $reflection2); |
25
|
40 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param array[object, object] $payload |
|
|
|
|
29
|
|
|
*/ |
30
|
40 |
|
private static function copyProperties(array $payload, \ReflectionObject $reflection1, \ReflectionObject $reflection2) |
31
|
|
|
{ |
32
|
40 |
|
list($obj1, $obj2) = $payload; |
33
|
40 |
|
$publicVars = $reflection1->getProperties(\ReflectionProperty::IS_PUBLIC); |
34
|
40 |
|
foreach ($publicVars as $property) { |
35
|
|
|
$propertyName = $property->getName(); |
36
|
|
|
if ($reflection2->hasProperty($propertyName) && $reflection2->getProperty($propertyName)->isPublic()) { |
37
|
|
|
$obj2->$propertyName = $obj1->$propertyName; |
38
|
|
|
} |
39
|
|
|
} |
40
|
40 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param array[object, object] $payload |
|
|
|
|
44
|
|
|
*/ |
45
|
40 |
|
private static function copyMethods(array $payload, \ReflectionObject $reflection1, \ReflectionObject $reflection2) |
46
|
|
|
{ |
47
|
40 |
|
list($obj1, $obj2) = $payload; |
48
|
40 |
|
$methods = $reflection1->getMethods(\ReflectionMethod::IS_PUBLIC); |
49
|
40 |
|
foreach ($methods as $method) { |
50
|
40 |
|
$methodName = $method->name; |
51
|
40 |
|
if (0 === strpos($methodName, 'get')) { |
52
|
40 |
|
$getMethod = $methodName; |
53
|
40 |
|
$setMethod = $methodName; |
54
|
40 |
|
$setMethod[0] = 's'; |
55
|
40 |
|
self::copyMethod([$obj1, $obj2], $getMethod, $setMethod, $reflection2); |
56
|
|
|
} |
57
|
|
|
} |
58
|
40 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array[object, object] $payload |
|
|
|
|
62
|
|
|
* @param string $getMethod |
63
|
|
|
* @param string $setMethod |
64
|
|
|
*/ |
65
|
40 |
|
private static function copyMethod(array $payload, $getMethod, $setMethod, \ReflectionObject $reflection2) |
66
|
|
|
{ |
67
|
40 |
|
list($obj1, $obj2) = $payload; |
68
|
40 |
|
if ($reflection2->hasMethod($setMethod)) { |
69
|
40 |
|
$value = $obj1->$getMethod(); |
70
|
40 |
|
if (null !== $value) { |
71
|
40 |
|
$obj2->$setMethod($value); |
72
|
|
|
} |
73
|
|
|
} |
74
|
40 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $varName |
78
|
|
|
* @param int $var |
79
|
|
|
* @param int $pow |
80
|
|
|
*/ |
81
|
4 |
|
public static function validateIntNull($varName, $var, $pow) |
82
|
|
|
{ |
83
|
4 |
|
if (null === $var) { |
|
|
|
|
84
|
4 |
|
return null; |
85
|
|
|
} |
86
|
4 |
|
if (!ctype_digit(strval($var))) { |
87
|
1 |
|
throw new \InvalidArgumentException("$varName must be an integer"); |
88
|
|
|
} |
89
|
|
|
|
90
|
4 |
|
if (strval(intval($var)) !== strval($var) || $var < 0 || $var >= pow(2, $pow)) { |
91
|
1 |
|
throw new \InvalidArgumentException("$varName must be an base 10 integer within 2^$pow"); |
92
|
|
|
} |
93
|
|
|
|
94
|
4 |
|
return intval($var); |
95
|
|
|
} |
96
|
|
|
|
97
|
136 |
|
public static function getMicrotimeStr() |
98
|
|
|
{ |
99
|
136 |
|
$parts = explode(' ', microtime()); |
100
|
136 |
|
$pos1 = strpos($parts[0], '.'); |
101
|
|
|
|
102
|
136 |
|
$timeU = $parts[1].'.'.substr($parts[0], $pos1 + 1, 6); |
103
|
|
|
|
104
|
136 |
|
return $timeU; |
105
|
|
|
} |
106
|
|
|
|
107
|
8 |
|
public static function getMicrotimeFloatDateTime($microtime) |
108
|
|
|
{ |
109
|
8 |
|
if (!is_float($microtime)) { |
110
|
|
|
throw new \RuntimeException("Could not create date time expected-float microtime: $microtime"); |
111
|
|
|
} |
112
|
8 |
|
$result = \DateTime::createFromFormat('U.u', number_format($microtime, 6, '.', '')); |
113
|
8 |
|
if (!$result) { |
|
|
|
|
114
|
|
|
throw new \RuntimeException("Could not create date time from float microtime: $microtime"); |
115
|
|
|
} |
116
|
8 |
|
$result->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
117
|
|
|
|
118
|
8 |
|
return $result; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @throws \RuntimeException |
123
|
|
|
* |
124
|
|
|
* @return \DateTime |
125
|
|
|
*/ |
126
|
135 |
|
public static function getMicrotimeDateTime() |
127
|
|
|
{ |
128
|
135 |
|
$result = \DateTime::createFromFormat('U.u', $microtime = self::getMicrotimeStr()); |
129
|
135 |
|
if (!$result) { |
|
|
|
|
130
|
|
|
throw new \RuntimeException("Could not create date time from $microtime"); |
131
|
|
|
} |
132
|
135 |
|
$result->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
133
|
|
|
|
134
|
135 |
|
return $result; |
135
|
|
|
} |
136
|
|
|
|
137
|
25 |
|
public static function getMicrotimeInteger() |
138
|
|
|
{ |
139
|
25 |
|
return self::getMicrotimeIntegerFormat(self::getMicrotimeDateTime()); |
140
|
|
|
} |
141
|
|
|
|
142
|
48 |
|
public static function getMicrotimeIntegerFormat(\DateTime $dateTime) |
143
|
|
|
{ |
144
|
48 |
|
$dateTimeUs = $dateTime->format('Uu'); |
145
|
48 |
|
$dateTimeUs = str_pad($dateTimeUs, 18, '0', STR_PAD_RIGHT); |
146
|
|
|
|
147
|
48 |
|
return $dateTimeUs; |
148
|
|
|
} |
149
|
|
|
|
150
|
34 |
|
public static function getDateTimeFromDecimalFormat($decimal) |
151
|
|
|
{ |
152
|
34 |
|
$len = strlen((string) time()); |
153
|
34 |
|
$timePart = substr($decimal, 0, $len); |
154
|
34 |
|
$decimalPart = substr($decimal, $len, 6); |
155
|
|
|
|
156
|
34 |
|
$result = \DateTime::createFromFormat('U.u', "{$timePart}.{$decimalPart}"); |
157
|
34 |
|
if (!$result) { |
|
|
|
|
158
|
|
|
throw new \RuntimeException("Could not create date time from {$timePart}.{$decimalPart}"); |
159
|
|
|
} |
160
|
34 |
|
$result->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
161
|
|
|
|
162
|
34 |
|
return $result; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|