|
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
|
36 |
|
public static function copy($obj1, $obj2) |
|
14
|
|
|
{ |
|
15
|
36 |
|
if (!is_object($obj1)) { |
|
16
|
|
|
throw new \InvalidArgumentException('$obj1 must be an object, not '.gettype($obj1)); |
|
17
|
|
|
} |
|
18
|
36 |
|
if (!is_object($obj2)) { |
|
19
|
|
|
throw new \InvalidArgumentException('$obj2 must be an object, not '.gettype($obj2)); |
|
20
|
|
|
} |
|
21
|
36 |
|
$reflection1 = new \ReflectionObject($obj1); |
|
22
|
36 |
|
$reflection2 = new \ReflectionObject($obj2); |
|
23
|
36 |
|
self::copyMethods([$obj1, $obj2], $reflection1, $reflection2); |
|
24
|
36 |
|
self::copyProperties([$obj1, $obj2], $reflection1, $reflection2); |
|
25
|
36 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param array[object, object] $payload |
|
29
|
|
|
* @param \ReflectionObject $reflection1 |
|
30
|
|
|
* @param \ReflectionObject $reflection2 |
|
31
|
|
|
*/ |
|
32
|
36 |
|
private static function copyProperties(array $payload, \ReflectionObject $reflection1, \ReflectionObject $reflection2) |
|
33
|
|
|
{ |
|
34
|
36 |
|
list($obj1, $obj2) = $payload; |
|
35
|
36 |
|
$publicVars = $reflection1->getProperties(\ReflectionProperty::IS_PUBLIC); |
|
36
|
36 |
|
foreach ($publicVars as $property) { |
|
37
|
|
|
$propertyName = $property->getName(); |
|
38
|
|
|
if ($reflection2->hasProperty($propertyName) && $reflection2->getProperty($propertyName)->isPublic()) { |
|
39
|
|
|
$obj2->$propertyName = $obj1->$propertyName; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
36 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param array[object, object] $payload |
|
46
|
|
|
* @param \ReflectionObject $reflection1 |
|
47
|
|
|
* @param \ReflectionObject $reflection2 |
|
48
|
|
|
*/ |
|
49
|
36 |
|
private static function copyMethods(array $payload, \ReflectionObject $reflection1, \ReflectionObject $reflection2) |
|
50
|
|
|
{ |
|
51
|
36 |
|
list($obj1, $obj2) = $payload; |
|
52
|
36 |
|
$methods = $reflection1->getMethods(\ReflectionMethod::IS_PUBLIC); |
|
53
|
36 |
|
foreach ($methods as $method) { |
|
54
|
36 |
|
$methodName = $method->name; |
|
55
|
36 |
|
if (0 === strpos($methodName, 'get')) { |
|
56
|
36 |
|
$getMethod = $methodName; |
|
57
|
36 |
|
$setMethod = $methodName; |
|
58
|
36 |
|
$setMethod[0] = 's'; |
|
59
|
36 |
|
self::copyMethod([$obj1, $obj2], $getMethod, $setMethod, $reflection2); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
36 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param array[object, object] $payload |
|
66
|
|
|
* @param string $getMethod |
|
67
|
|
|
* @param string $setMethod |
|
68
|
|
|
* @param \ReflectionObject $reflection2 |
|
69
|
|
|
*/ |
|
70
|
36 |
|
private static function copyMethod(array $payload, $getMethod, $setMethod, \ReflectionObject $reflection2) |
|
71
|
|
|
{ |
|
72
|
36 |
|
list($obj1, $obj2) = $payload; |
|
73
|
36 |
|
if ($reflection2->hasMethod($setMethod)) { |
|
74
|
36 |
|
$value = $obj1->$getMethod(); |
|
75
|
36 |
|
if (null !== $value) { |
|
76
|
36 |
|
$obj2->$setMethod($value); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
36 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $varName |
|
83
|
|
|
* @param int $var |
|
84
|
|
|
* @param int $pow |
|
85
|
|
|
*/ |
|
86
|
4 |
|
public static function validateIntNull($varName, $var, $pow) |
|
87
|
|
|
{ |
|
88
|
4 |
|
if (null === $var) { |
|
89
|
4 |
|
return null; |
|
90
|
|
|
} |
|
91
|
4 |
|
if (!ctype_digit(strval($var))) { |
|
92
|
1 |
|
throw new \InvalidArgumentException("$varName must be an integer"); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
4 |
|
if (strval(intval($var)) !== strval($var) || $var <= 0 || $var >= pow(2, $pow)) { |
|
96
|
3 |
|
throw new \InvalidArgumentException("$varName must be an base 10 integer within 2^$pow"); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
4 |
|
return intval($var); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
112 |
|
public static function getMicrotimeStr() |
|
103
|
|
|
{ |
|
104
|
112 |
|
$parts = explode(' ', microtime()); |
|
105
|
112 |
|
$pos1 = strpos($parts[0], '.'); |
|
106
|
|
|
|
|
107
|
112 |
|
$timeU = $parts[1].'.'.substr($parts[0], $pos1 + 1, 6); |
|
108
|
|
|
|
|
109
|
112 |
|
return $timeU; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @throws \RuntimeException |
|
114
|
|
|
* |
|
115
|
|
|
* @return \DateTime |
|
116
|
|
|
*/ |
|
117
|
86 |
|
public static function getMicrotimeDateTime() |
|
118
|
|
|
{ |
|
119
|
86 |
|
$result = \DateTime::createFromFormat('U.u', $microtime = self::getMicrotimeStr()); |
|
120
|
86 |
|
if (!$result) { |
|
121
|
|
|
throw new \RuntimeException("Could not create date time from $microtime"); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
86 |
|
return $result; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
18 |
|
public static function getMicrotimeDecimal() |
|
128
|
|
|
{ |
|
129
|
18 |
|
return self::getMicrotimeDecimalFormat(self::getMicrotimeDateTime()); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
34 |
|
public static function getMicrotimeDecimalFormat(\DateTime $dateTime) |
|
133
|
|
|
{ |
|
134
|
34 |
|
$dateTimeUs = $dateTime->format('Uu'); |
|
135
|
34 |
|
$dateTimeUs = str_pad($dateTimeUs, 18, '0', STR_PAD_RIGHT); |
|
136
|
|
|
|
|
137
|
34 |
|
return $dateTimeUs; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
23 |
|
public static function getDateTimeFromDecimalFormat($decimal) |
|
141
|
|
|
{ |
|
142
|
23 |
|
$len = strlen((string) time()); |
|
143
|
23 |
|
$timePart = substr($decimal, 0, $len); |
|
144
|
23 |
|
$decimalPart = substr($decimal, $len, 6); |
|
145
|
|
|
|
|
146
|
23 |
|
return \DateTime::createFromFormat('U.u', "${timePart}.${decimalPart}"); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: