|
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 |
|
$stringUtility = new StringUtility(); |
|
67
|
11 |
|
if ($attributeKey === 'ts' || $stringUtility->endsWith($attributeKey, '_ts')) { |
|
68
|
|
|
// replace the last ts with timestamp |
|
69
|
3 |
|
$attributeKey = preg_replace('/ts$/', 'timestamp', $attributeKey); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
11 |
|
$camelCase = $stringUtility->snakeCaseToCamelCase($attributeKey); |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* If camel case attribute starts with 'is', 'has', ... following by an uppercase letter, remove it |
|
76
|
|
|
* This is used to handle calling functions such as setIm or setUserDeleted |
|
77
|
|
|
* instead of setIsIm or setIsUserDeleted |
|
78
|
|
|
* |
|
79
|
|
|
* The style checkers complain about functions such as setIsIm, ... |
|
80
|
|
|
*/ |
|
81
|
11 |
|
$booleanPrefixes = ['is', 'has']; |
|
82
|
11 |
|
sort($booleanPrefixes); |
|
83
|
|
|
|
|
84
|
11 |
|
foreach ($booleanPrefixes as $booleanPrefix) { |
|
85
|
11 |
|
if (!preg_match('/^((?i)'.$booleanPrefix.')[A-Z0-9]/', $camelCase)) { |
|
86
|
11 |
|
continue; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// found the boolean prefix - remove it |
|
90
|
3 |
|
$camelCase = substr($camelCase, strlen($booleanPrefix)); |
|
91
|
3 |
|
break; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
11 |
|
$function = 'set'.$camelCase; |
|
95
|
|
|
|
|
96
|
11 |
|
if (method_exists($object, $function)) { |
|
97
|
11 |
|
return $function; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|