1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Trait with methods to work with properties. Used in Method and Entity classes. |
5
|
|
|
* |
6
|
|
|
* @package Teebot (Telegram bot framework) |
7
|
|
|
* |
8
|
|
|
* @author Stanislav Drozdov <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Teebot\Traits; |
12
|
|
|
|
13
|
|
|
use Teebot\Exception\Output; |
14
|
|
|
use Teebot\Exception\Critical; |
15
|
|
|
|
16
|
|
|
trait Property |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Returns camel cased property's getter or setter method name. Checks method for existence. |
20
|
|
|
* |
21
|
|
|
* @param string $prefix Prefix of the method e.g. "set" or "get" |
22
|
|
|
* @param string $name Method's name |
23
|
|
|
* |
24
|
|
|
* @return null|string |
25
|
|
|
*/ |
26
|
|
|
protected function getSetGetMethodName($prefix, $name) |
27
|
|
|
{ |
28
|
|
|
$setter = $prefix . str_replace("_", "", ucwords($name, "_")); |
29
|
|
|
|
30
|
|
|
if (method_exists($this, $setter)) { |
31
|
|
|
return $setter; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return null; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Sets properties of the class from array. |
39
|
|
|
* |
40
|
|
|
* @param array $data An associative array with property => value data |
41
|
|
|
*/ |
42
|
|
|
protected function setProperties(array $data) |
43
|
|
|
{ |
44
|
|
|
foreach ($data as $name => $value) { |
45
|
|
|
$this->setProperty($name, $value); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Sets property of the class. |
51
|
|
|
* |
52
|
|
|
* @param string $name Property name |
53
|
|
|
* @param null|mixed $value Value of the property |
54
|
|
|
*/ |
55
|
|
|
protected function setProperty($name, $value = null) |
56
|
|
|
{ |
57
|
|
|
$setterMethod = $this->getSetGetMethodName("set", $name); |
58
|
|
|
|
59
|
|
|
if ($setterMethod) { |
|
|
|
|
60
|
|
|
$this->{$setterMethod}($value); |
61
|
|
|
|
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (property_exists($this, $name)) { |
66
|
|
|
$this->{$name} = $value; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns properties as string. Used for building get query string if GET method was set. |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getPropertiesAsString() |
76
|
|
|
{ |
77
|
|
|
$properties = $this->getPropertiesArray(); |
78
|
|
|
|
79
|
|
|
return $properties ? http_build_query($properties) : ''; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns an array with properties. Array with supported properties should be defined |
84
|
|
|
* in the class. |
85
|
|
|
* |
86
|
|
|
* @param bool $validate Flag whether validation for required properties should be applied |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function getPropertiesArray($validate = true) |
91
|
|
|
{ |
92
|
|
|
$properties = []; |
93
|
|
|
|
94
|
|
|
if (empty($this->supportedProperties)) { |
|
|
|
|
95
|
|
|
return $properties; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
foreach ($this->supportedProperties as $name => $isRequired) { |
99
|
|
|
|
100
|
|
|
$getterMethod = $this->getSetGetMethodName("get", $name); |
101
|
|
|
|
102
|
|
|
if ($getterMethod && $this->{$getterMethod}() !== null) { |
103
|
|
|
$properties[$name] = $this->{$getterMethod}(); |
104
|
|
|
|
105
|
|
|
continue; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (property_exists($this, $name) && $this->{$name} !== null) { |
109
|
|
|
$properties[$name] = $this->{$name}; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($validate) { |
114
|
|
|
try { |
115
|
|
|
$this->validateProperties($properties); |
116
|
|
|
} catch (Critical $e) { |
117
|
|
|
Output::log($e); |
118
|
|
|
|
119
|
|
|
$properties = []; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $properties; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Validates properties and checks which are required |
128
|
|
|
* |
129
|
|
|
* @param array $properties An associative array of the properties |
130
|
|
|
* |
131
|
|
|
* @throws Critical |
132
|
|
|
*/ |
133
|
|
|
protected function validateProperties($properties) |
134
|
|
|
{ |
135
|
|
|
foreach ($this->supportedProperties as $propertyName => $isRequired) { |
136
|
|
|
if ($isRequired === true && empty($properties[$propertyName])) { |
137
|
|
|
throw new Critical('Required property "'.$propertyName.'" is not set!'); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns object's properties encoded as JSON string |
144
|
|
|
* |
145
|
|
|
* @param bool $validate Flag whether validation for required properties should be applied |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function asJson($validate = true) |
150
|
|
|
{ |
151
|
|
|
$properties = $this->getPropertiesArray($validate); |
152
|
|
|
|
153
|
|
|
return json_encode($properties); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: