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\Api\Traits; |
12
|
|
|
|
13
|
|
|
use Teebot\Api\Exception\PropertyException; |
14
|
|
|
|
15
|
|
|
trait Property |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Returns camel cased property's getter or setter method name. Checks method for existence. |
19
|
|
|
* |
20
|
|
|
* @param string $prefix Prefix of the method e.g. "set" or "get" |
21
|
|
|
* @param string $name Method's name |
22
|
|
|
* |
23
|
|
|
* @return null|string |
24
|
|
|
*/ |
25
|
|
|
protected function getSetGetMethodName($prefix, $name) |
26
|
|
|
{ |
27
|
|
|
$setter = $prefix . str_replace("_", "", ucwords($name, "_")); |
28
|
|
|
|
29
|
|
|
if (method_exists($this, $setter)) { |
30
|
|
|
return $setter; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return null; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Sets properties of the class from array. |
38
|
|
|
* |
39
|
|
|
* @param array $data An associative array with property => value data |
40
|
|
|
*/ |
41
|
|
|
protected function setProperties(array $data) |
42
|
|
|
{ |
43
|
|
|
foreach ($data as $name => $value) { |
44
|
|
|
$this->setProperty($name, $value); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Sets property of the class. |
50
|
|
|
* |
51
|
|
|
* @param string $name Property name |
52
|
|
|
* @param null|mixed $value Value of the property |
53
|
|
|
*/ |
54
|
|
|
protected function setProperty($name, $value = null) |
55
|
|
|
{ |
56
|
|
|
$setterMethod = $this->getSetGetMethodName("set", $name); |
57
|
|
|
|
58
|
|
|
if ($setterMethod) { |
|
|
|
|
59
|
|
|
$this->{$setterMethod}($value); |
60
|
|
|
|
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (property_exists($this, $name)) { |
65
|
|
|
$this->{$name} = $value; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns properties as string. Used for building get query string if GET method was set. |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getPropertiesAsString() |
75
|
|
|
{ |
76
|
|
|
$properties = $this->getPropertiesArray(); |
77
|
|
|
|
78
|
|
|
return $properties ? http_build_query($properties) : ''; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns an array with properties. Array with supported properties should be defined |
83
|
|
|
* in the class. |
84
|
|
|
* |
85
|
|
|
* @param bool $validate Flag whether validation for required properties should be applied |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
public function getPropertiesArray($validate = true) |
90
|
|
|
{ |
91
|
|
|
$properties = []; |
92
|
|
|
|
93
|
|
|
if (empty($this->supportedProperties)) { |
|
|
|
|
94
|
|
|
return $properties; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
foreach ($this->supportedProperties as $name => $isRequired) { |
98
|
|
|
|
99
|
|
|
$getterMethod = $this->getSetGetMethodName("get", $name); |
100
|
|
|
|
101
|
|
|
if ($getterMethod && $this->{$getterMethod}() !== null) { |
102
|
|
|
$properties[$name] = $this->{$getterMethod}(); |
103
|
|
|
|
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (property_exists($this, $name) && $this->{$name} !== null) { |
108
|
|
|
$properties[$name] = $this->{$name}; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($validate) { |
113
|
|
|
$this->validateProperties($properties); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $properties; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getPropertiesMultipart($validate = true) |
120
|
|
|
{ |
121
|
|
|
$requestProperties = []; |
122
|
|
|
$properties = $this->getPropertiesArray($validate); |
123
|
|
|
|
124
|
|
|
foreach ($properties as $k => $v) { |
125
|
|
|
$requestProperties[] = [ |
126
|
|
|
'name' => $k, |
127
|
|
|
'contents' => $v, |
128
|
|
|
]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $requestProperties; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Validates properties and checks which are required |
136
|
|
|
* |
137
|
|
|
* @param array $properties An associative array of the properties |
138
|
|
|
* |
139
|
|
|
* @throws PropertyException |
140
|
|
|
*/ |
141
|
|
|
protected function validateProperties($properties) |
142
|
|
|
{ |
143
|
|
|
foreach ($this->supportedProperties as $propertyName => $isRequired) { |
144
|
|
|
if ($isRequired === true && empty($properties[$propertyName])) { |
145
|
|
|
throw new PropertyException('Required property "'.$propertyName.'" is not set!'); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Returns object's properties encoded as JSON string |
152
|
|
|
* |
153
|
|
|
* @param bool $validate Flag whether validation for required properties should be applied |
154
|
|
|
* |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public function asJson($validate = true) |
158
|
|
|
{ |
159
|
|
|
$properties = $this->getPropertiesArray($validate); |
160
|
|
|
|
161
|
|
|
return json_encode($properties); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
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: