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(string $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
|
12 |
|
public function loadAttributes(AbstractBaseSlack $object, $attributes) |
32
|
|
|
{ |
33
|
|
|
// if $attributes is not array convert it to array |
34
|
12 |
|
if (!is_array($attributes)) { |
35
|
1 |
|
$attributes = json_decode($attributes, true); |
36
|
|
|
} |
37
|
|
|
|
38
|
12 |
|
foreach ($attributes as $attributeKey => $attributeValue) { |
39
|
12 |
|
$method = $this->getSetMethodByAttribute($object, $attributeKey); |
40
|
|
|
|
41
|
|
|
// ignore if setter function does not exist |
42
|
12 |
|
if (empty($method)) { |
43
|
2 |
|
continue; |
44
|
|
|
} |
45
|
|
|
|
46
|
12 |
|
$object->$method($attributeValue); |
47
|
|
|
} |
48
|
|
|
|
49
|
12 |
|
return $object; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param AbstractBaseSlack $object |
54
|
|
|
* @param $attributeKey |
55
|
|
|
* |
56
|
|
|
* @return bool|string |
57
|
|
|
*/ |
58
|
12 |
|
private function getSetMethodByAttribute(AbstractBaseSlack $object, string $attributeKey) |
59
|
|
|
{ |
60
|
|
|
// For id, we cannot use 'set'.$stringUtility->snakeCaseToCamelCase($attributeKey) since it's named slackId |
61
|
12 |
|
if ($attributeKey === 'id') { |
62
|
10 |
|
return 'setSlackId'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// handle ts because there is setTimestamp instead of setTs |
66
|
12 |
|
$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
|
12 |
|
$function = 'set'.$this->removeBooleanPrefix($camelCase); |
76
|
|
|
|
77
|
12 |
|
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
|
12 |
|
private function processTimestamp(string $text): string |
88
|
|
|
{ |
89
|
12 |
|
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
|
12 |
|
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
|
12 |
|
private function findBooleanPrefix(string $text) |
105
|
|
|
{ |
106
|
12 |
|
$booleanPrefixes = ['is', 'has']; |
107
|
12 |
|
sort($booleanPrefixes); |
108
|
|
|
|
109
|
12 |
|
foreach ($booleanPrefixes as $booleanPrefix) { |
110
|
12 |
|
if (!preg_match('/^((?i)'.$booleanPrefix.')[A-Z0-9]/', $text)) { |
111
|
12 |
|
continue; |
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
return $booleanPrefix; |
115
|
|
|
} |
116
|
12 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* If find the boolean prefix, remove it. |
120
|
|
|
* |
121
|
|
|
* @param $text |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
12 |
|
private function removeBooleanPrefix(string $text): string |
126
|
|
|
{ |
127
|
12 |
|
$booleanPrefix = $this->findBooleanPrefix($text); |
128
|
12 |
|
if (!empty($booleanPrefix)) { |
129
|
|
|
// found the boolean prefix - remove it |
130
|
3 |
|
$text = substr($text, strlen($booleanPrefix)); |
131
|
|
|
} |
132
|
|
|
|
133
|
12 |
|
return $text; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|