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