Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
userdata.php
1<?php
2
4
6
7final class UserData
8{
9 public const GENDER_FEMALE = 'f';
10 public const GENDER_MALE = 'm';
11
12 private $container = [];
13
19 public function __construct(?array $params = null)
20 {
21 if ($params && !empty($params))
22 {
23 if (array_key_exists('email', $params) && is_string($params['email']))
24 {
25 $this->setEmail($params['email']);
26 }
27 if (array_key_exists('phone', $params) && is_string($params['phone']))
28 {
29 $this->setPhone($params['phone']);
30 }
31 if (array_key_exists('gender', $params) && is_string($params['gender']))
32 {
33 $this->setGender($params['gender']);
34 }
35 if (array_key_exists('date_of_birth', $params) && $params['date_of_birth'] instanceof DateTime)
36 {
37 $this->setDateOfBirth($params['date_of_birth']);
38 }
39 if (array_key_exists('last_name', $params) && is_string($params['last_name']))
40 {
41 $this->setLastName($params['last_name']);
42 }
43 if (array_key_exists('first_name', $params) && is_string($params['first_name']))
44 {
45 $this->setFirstName($params['first_name']);
46 }
47 if (array_key_exists('city', $params) && is_string($params['city']))
48 {
49 $this->setCity($params['city']);
50 }
51 if (array_key_exists('client_ip_address', $params) && is_string($params['client_ip_address']))
52 {
53 $this->setClientIpAddress($params['client_ip_address']);
54 }
55 if (array_key_exists('client_user_agent', $params) && is_string($params['client_user_agent']))
56 {
57 $this->setClientUserAgent($params['client_user_agent']);
58 }
59 if (array_key_exists('fbc', $params) && is_string($params['fbc']))
60 {
61 $this->setFacebookClick($params['fbc']);
62 }
63 if (array_key_exists('fbp', $params) && is_string($params['fbp']))
64 {
65 $this->setFacebookPixel($params['fbp']);
66 }
67 }
68 }
69
75 public function setEmail(?string $email)
76 {
77 if (check_email($email))
78 {
79 $this->container['email'] = $email;
80 }
81
82 return $this;
83 }
84
90 public function setPhone(?string $phone)
91 {
92 if (preg_match('/^[\+]?[\d]{4,25}$/', $phone))
93 {
94 $this->container['phone'] = $phone;
95 }
96
97 return $this;
98 }
99
105 public function setGender(?string $gender)
106 {
107 if (in_array($gender, [static::GENDER_FEMALE, static::GENDER_MALE]))
108 {
109 $this->container['gender'] = $gender;
110 }
111
112 return $this;
113 }
114
120 public function setDateOfBirth(?DateTime $date)
121 {
122 if ($date)
123 {
124 $this->container['date_of_birth'] = $date->format('Ymd');
125 }
126
127 return $this;
128 }
129
135 public function setLastName(?string $lastName)
136 {
137 $this->container['last_name'] = $lastName;
138
139 return $this;
140 }
141
147 public function setFirstName(?string $name)
148 {
149 $this->container['first_name'] = $name;
150
151 return $this;
152 }
153
159 public function setCity(?string $city)
160 {
161 $this->container['city'] = $city;
162
163 return $this;
164 }
165
171 public function setClientIpAddress(?string $ipAddress)
172 {
173 if ($ipAddress &&
174 preg_match('/^[1-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}.[0-9]{1,3}$/i', $ipAddress) &&
175 array_reduce(explode('.', $ipAddress),
176 function($element) {
177 $element = (int)$element;
178
179 return $element > 0 && $element < 255;
180 },
181 true))
182 {
183 $this->container['client_ip_address'] = $ipAddress;
184 }
185
186 return $this;
187 }
188
189 public function setClientUserAgent(?string $userAgent)
190 {
191 if ($userAgent)
192 {
193 $this->container['client_user_agent'] = $userAgent;
194 }
195
196 return $this;
197 }
198
206 public function setFacebookClick(?string $facebookClick)
207 {
208 if ($facebookClick && preg_match('/^fb\.[0-2]{1}\.[0-9]+\.[0-9a-z]+$/i', $facebookClick))
209 {
210 $this->container['fbc'] = $facebookClick;
211 }
212
213 return $this;
214 }
215
223 public function setFacebookPixel(?string $facebookPixel)
224 {
225 if ($facebookPixel && preg_match('/^fb\.[0-2]{1}\.[0-9]+\.[0-9]+$/i', $facebookPixel))
226 {
227 $this->container['fbp'] = $facebookPixel;
228 }
229
230 return $this;
231 }
232
233 public function getEmail()
234 {
235 return $this->container['email'];
236 }
237
238 public function getPhone()
239 {
240 return $this->container['phone'];
241 }
242
243 public function getGender()
244 {
245 return $this->container['email'];
246 }
247
248 public function getDateOfBirth()
249 {
250 return $this->container['date_of_birth'];
251 }
252
253 public function getLastName()
254 {
255 return $this->container['last_name'];
256 }
257
258 public function getFirstName()
259 {
260 return $this->container['first_name'];
261 }
262
263 public function getCity()
264 {
265 return $this->container['city'];
266 }
267
268 public function getClientIpAddress()
269 {
270 return $this->container['client_ip_address'];
271 }
272
273 public function getClientUserAgent()
274 {
275 return $this->container['client_user_agent'];
276 }
277
278 public function getFacebookClick()
279 {
280 return $this->container['fbc'];
281 }
282
283 public function getFacebookPixel()
284 {
285 return $this->container['fbp'];
286 }
287
288 public function validate(): bool
289 {
290 if (0 === $count = count($this->container))
291 {
292 return false;
293 }
294 elseif ($count === 1)
295 {
296 return !($this->container['client_user_agent'] || $this->container['client_ip_address']);
297 }
298
299 return true;
300 }
301
302 public function toArray()
303 {
304 return $this->container;
305 }
306}
setFacebookPixel(?string $facebookPixel)
Definition userdata.php:223
setFacebookClick(?string $facebookClick)
Definition userdata.php:206