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