|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Botonomous\utility; |
|
4
|
|
|
|
|
5
|
|
|
use Botonomous\AbstractBaseSlack; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class ClassUtility. |
|
9
|
|
|
*/ |
|
10
|
|
|
class ClassUtility |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @param string $fullName |
|
14
|
|
|
* |
|
15
|
|
|
* @return mixed |
|
16
|
|
|
*/ |
|
17
|
19 |
|
public function extractClassNameFromFullName($fullName) |
|
18
|
|
|
{ |
|
19
|
|
|
// get last part of the namespace |
|
20
|
19 |
|
$classParts = explode('\\', $fullName); |
|
21
|
|
|
|
|
22
|
19 |
|
return end($classParts); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param AbstractBaseSlack $object |
|
27
|
|
|
* @param $attributes |
|
28
|
|
|
* |
|
29
|
|
|
* @return AbstractBaseSlack |
|
30
|
|
|
*/ |
|
31
|
11 |
|
public function loadAttributes(AbstractBaseSlack $object, $attributes) |
|
32
|
|
|
{ |
|
33
|
|
|
// if $attributes is not array convert it to array |
|
34
|
11 |
|
if (!is_array($attributes)) { |
|
35
|
1 |
|
$attributes = json_decode($attributes, true); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
11 |
|
foreach ($attributes as $attributeKey => $attributeValue) { |
|
39
|
11 |
|
$method = $this->getSetMethodByAttribute($object, $attributeKey); |
|
40
|
|
|
|
|
41
|
|
|
// ignore if setter function does not exist |
|
42
|
11 |
|
if (empty($method)) { |
|
43
|
2 |
|
continue; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
11 |
|
$object->$method($attributeValue); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
11 |
|
return $object; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param AbstractBaseSlack $object |
|
54
|
|
|
* @param $attributeKey |
|
55
|
|
|
* |
|
56
|
|
|
* @return bool|string |
|
57
|
|
|
*/ |
|
58
|
11 |
|
private function getSetMethodByAttribute(AbstractBaseSlack $object, $attributeKey) |
|
59
|
|
|
{ |
|
60
|
|
|
// For id, we cannot use 'set'.$stringUtility->snakeCaseToCamelCase($attributeKey) since it's named slackId |
|
61
|
11 |
|
if ($attributeKey === 'id') { |
|
62
|
10 |
|
return 'setSlackId'; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// handle ts because there is setTimestamp instead of setTs |
|
66
|
11 |
|
$camelCase = (new StringUtility())->snakeCaseToCamelCase($this->processTimestamp($attributeKey)); |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* If camel case attribute starts with 'is', 'has', ... following by an uppercase letter, remove it |
|
70
|
|
|
* This is used to handle calling functions such as setIm or setUserDeleted |
|
71
|
|
|
* instead of setIsIm or setIsUserDeleted. |
|
72
|
|
|
* |
|
73
|
|
|
* The style checkers complain about functions such as setIsIm, ... |
|
74
|
|
|
*/ |
|
75
|
11 |
|
$function = 'set'.$this->removeBooleanPrefix($camelCase); |
|
76
|
|
|
|
|
77
|
11 |
|
return method_exists($object, $function) ? $function : false; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* If text is 'ts' or ends with '_ts' replace it with 'timestamp'. |
|
82
|
|
|
* |
|
83
|
|
|
* @param $text |
|
84
|
|
|
* |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
*/ |
|
87
|
11 |
|
private function processTimestamp($text) |
|
88
|
|
|
{ |
|
89
|
11 |
|
if ($text === 'ts' || (new StringUtility())->endsWith($text, '_ts')) { |
|
90
|
|
|
// replace the last ts with timestamp |
|
91
|
3 |
|
$text = preg_replace('/ts$/', 'timestamp', $text); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
11 |
|
return $text; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Check if the text starts with boolean prefixes such as 'is', 'has', ... |
|
99
|
|
|
* |
|
100
|
|
|
* @param $text |
|
101
|
|
|
* |
|
102
|
|
|
* @return mixed |
|
103
|
|
|
*/ |
|
104
|
11 |
|
private function findBooleanPrefix($text) |
|
105
|
|
|
{ |
|
106
|
11 |
|
$booleanPrefixes = ['is', 'has']; |
|
107
|
11 |
|
sort($booleanPrefixes); |
|
108
|
|
|
|
|
109
|
11 |
|
foreach ($booleanPrefixes as $booleanPrefix) { |
|
110
|
11 |
|
if (!preg_match('/^((?i)'.$booleanPrefix.')[A-Z0-9]/', $text)) { |
|
111
|
11 |
|
continue; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
3 |
|
return $booleanPrefix; |
|
115
|
|
|
} |
|
116
|
11 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* If find the boolean prefix, remove it. |
|
120
|
|
|
* |
|
121
|
|
|
* @param $text |
|
122
|
|
|
* |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
11 |
|
private function removeBooleanPrefix($text) |
|
126
|
|
|
{ |
|
127
|
11 |
|
$booleanPrefix = $this->findBooleanPrefix($text); |
|
128
|
11 |
|
if (!empty($booleanPrefix)) { |
|
129
|
|
|
// found the boolean prefix - remove it |
|
130
|
3 |
|
$text = substr($text, strlen($booleanPrefix)); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
11 |
|
return $text; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|