Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
location.php
1<?php
2
4
13
19final class Location implements \Serializable
20{
22 protected $id = 0;
24 protected $code = '';
26 protected $externalId = '';
28 protected $sourceCode = '';
30 protected $type = Type::UNKNOWN;
32 protected $name = '';
34 protected $languageId = '';
36 protected $latitude = '';
38 protected $longitude = '';
39
41 private $fieldCollection;
42
44 protected $address = null;
45
47 protected $parents = null;
48
52 public function __construct()
53 {
54 $this->fieldCollection = new FieldCollection();
55 }
56
63 public function isParentOf(Location $childCandidate): bool
64 {
65 $candidateParents = $childCandidate->getParents();
66
67 if(!$candidateParents)
68 {
69 return false;
70 }
71
72 return $candidateParents->isContain($this);
73 }
74
81 public function isChildOf(Location $parentCandidate): bool
82 {
83 $parents = $this->getParents();
84
85 if(!$parents)
86 {
87 return false;
88 }
89
90 return $parents->isContain($parentCandidate);
91 }
92
99 public function isEqualTo(Location $location): bool
100 {
101 if($this->getId() > 0 && $location->getId() > 0)
102 {
103 return $this->getId() === $location->getId();
104 }
105
106 if($this->getExternalId() !== '' || $location->getExternalId() !== ''
107 || $this->getSourceCode() !== '' || $location->getSourceCode() !== '')
108 {
109
110 if($this->getExternalId() === $location->getExternalId()
111 && $this->getSourceCode() === $location->getSourceCode())
112 {
113 return true;
114 }
115
116 if($this->getExternalId() !== $location->getExternalId())
117 {
118 return false;
119 }
120
121 if($this->getSourceCode() !== $location->getSourceCode())
122 {
123 return false;
124 }
125 }
126
127 if($this->getType() !== $location->getType())
128 {
129 return false;
130 }
131
132 if($this->getLanguageId() !== $location->getLanguageId())
133 {
134 return false;
135 }
136
137 if($this->getName() !== $location->getName())
138 {
139 return false;
140 }
141
142 if($this->getLatitude() !== $location->getLatitude())
143 {
144 return false;
145 }
146
147 if($this->getLongitude() !== $location->getLongitude())
148 {
149 return false;
150 }
151
152 $thisParents = $this->getParents();
153 $otherParents = $location->getParents();
154
155 if($thisParents && $otherParents && !$thisParents->isEqualTo($otherParents))
156 {
157 return false;
158 }
159
160 return true;
161 }
162
167 public function getParents()
168 {
169 $this->loadParents();
170 return $this->parents;
171 }
172
177 public function loadParents(): void
178 {
179 if($this->parents === null)
180 {
181 $this->parents = Service\LocationService::getInstance()->findParents($this, $this->languageId);
182 }
183 }
184
190 public function getParentByLevel(int $level): ?Location
191 {
192 $parents = $this->getParents();
193 return (bool)$parents ? $parents[$level] : null;
194 }
195
201 public function getParentByType(int $type): ?Location
202 {
203 $parents = $this->getParents();
204 return (bool)$parents ? $parents->getItemByType($type) : null;
205 }
206
212 public function setParents(Parents $parents): self
213 {
214 $this->parents = $parents;
215 $this->parents->setDescendant($this);
216 return $this;
217 }
218
222 public function getExternalId(): string
223 {
224 return $this->externalId;
225 }
226
231 public function setExternalId(string $externalId): self
232 {
233 $this->externalId = $externalId;
234 return $this;
235 }
236
240 public function getLanguageId(): string
241 {
242 return $this->languageId;
243 }
244
249 public function setLanguageId(string $languageId): self
250 {
251 $this->languageId = $languageId;
252 return $this;
253 }
254
258 public function getName(): string
259 {
260 return $this->name;
261 }
262
269 public function getNameWithParents(): string
270 {
271 $result = $this->getName();
272
273 if($parents = $this->getParents())
274 {
275 foreach ($parents as $parent)
276 {
277 $result = $parent->getName().', '.$this->getName();
278 }
279 }
280
281 return $result;
282 }
283
288 public function setName(string $name): self
289 {
290 $this->name = $name;
291 return $this;
292 }
293
297 public function getAddress(): ?Address
298 {
299 if($this->address === null)
300 {
301 $this->address = Location\Converter\AddressConverter::convertToAddress($this);
302 }
303
304 return $this->address;
305 }
306
311 public function setAddress(Address $address): self
312 {
313 $this->address = $address;
314 return $this;
315 }
316
320 public function getId(): int
321 {
322 return $this->id;
323 }
324
329 public function setId(int $id): self
330 {
331 $this->id = $id;
332 return $this;
333 }
334
338 public function getCode(): string
339 {
340 return $this->code;
341 }
342
347 public function setCode(string $code): self
348 {
349 $this->code = $code;
350 return $this;
351 }
352
356 public function getSourceCode(): string
357 {
358 return $this->sourceCode;
359 }
360
365 public function setSourceCode($sourceCode): self
366 {
367 $this->sourceCode = $sourceCode;
368 return $this;
369 }
370
375 public function getType(): int
376 {
377 return $this->type;
378 }
379
386 public function setType(int $type): self
387 {
388 if(!Type::isValueExist($type))
389 {
390 throw new ArgumentOutOfRangeException('Wrong location type');
391 }
392
393 $this->type = $type;
394 return $this;
395 }
396
403 public function copyDataFrom(Location $otherLocation): void
404 {
405 $this->setId($otherLocation->getId())
406 ->setCode($otherLocation->getCode())
407 ->setExternalId($otherLocation->getExternalId())
408 ->setSourceCode($otherLocation->getSourceCode())
409 ->setType($otherLocation->getType())
410 ->setName($otherLocation->getName())
411 ->setLanguageId($otherLocation->getLanguageId())
412 ->setLatitude($otherLocation->getLatitude())
413 ->setLongitude($otherLocation->getLongitude());
414
415 if($address = $otherLocation->getAddress())
416 {
417 $this->setAddress($address);
418 }
419 }
420
424 public function getLatitude(): string
425 {
426 return $this->latitude;
427 }
428
433 public function setLatitude(string $latitude): self
434 {
435 $this->latitude = $latitude;
436 return $this;
437 }
438
442 public function getLongitude(): string
443 {
444 return $this->longitude;
445 }
446
451 public function setLongitude(string $longitude): self
452 {
453 $this->longitude = $longitude;
454 return $this;
455 }
456
462 public function save(): \Bitrix\Main\Result
463 {
464 return Service\LocationService::getInstance()->save($this);
465 }
466
474 public static function load(int $id, string $languageId = LANGUAGE_ID)
475 {
476 return Service\LocationService::getInstance()->findById($id, $languageId);
477 }
478
484 public function delete(): \Bitrix\Main\Result
485 {
486 return Service\LocationService::getInstance()->delete($this);
487 }
488
494 public function serialize()
495 {
496 return serialize(
497 \Bitrix\Location\Entity\Location\Converter\ArrayConverter::convertToArray($this)
498 );
499 }
500
501 public function __serialize(): array
502 {
503 return \Bitrix\Location\Entity\Location\Converter\ArrayConverter::convertToArray($this);
504 }
505
511 public function unserialize($serialized)
512 {
513 $this->copyDataFrom(
515 unserialize($serialized, ['allowed_classes' => false])
516 )
517 );
518 }
519
520 public function __unserialize(array $data): void
521 {
522 $this->copyDataFrom(
524 $data
525 )
526 );
527 }
528
534 public function toArray(): array
535 {
536 return ArrayConverter::convertToArray($this);
537 }
538
539 public function toJson(): string
540 {
541 return Json::encode($this->toArray());
542 }
543
550 public static function fromArray(array $location): Location
551 {
552 return ArrayConverter::convertFromArray($location);
553 }
554
563 public function setFieldValue(int $type, string $value): self
564 {
565 if($field = $this->fieldCollection->getItemByType($type))
566 {
567 $field->setValue($value);
568 }
569 else
570 {
571 $this->fieldCollection->addItem(
572 new Field($type, $value)
573 );
574 }
575
576 return $this;
577 }
578
584 public function getAllFieldsValues(): array
585 {
586 $result = [];
587
588 foreach ($this->fieldCollection as $field)
589 {
590 $result[$field->getType()] = $field->getValue();
591 }
592
593 return $result;
594 }
595
600 public function getFieldValue(int $type): ?string
601 {
602 $result = null;
603
604 if($field = $this->fieldCollection->getItemByType($type))
605 {
606 $result = $field->getValue();
607 }
608
609 return $result;
610 }
611
618 public function isFieldExist(int $type): bool
619 {
620 return (bool)$this->fieldCollection->getItemByType($type);
621 }
622
628 {
629 return $this->fieldCollection;
630 }
631}
setDescendant(Location $descendant)
Definition parents.php:31
setLatitude(string $latitude)
Definition location.php:433
copyDataFrom(Location $otherLocation)
Definition location.php:403
setExternalId(string $externalId)
Definition location.php:231
isChildOf(Location $parentCandidate)
Definition location.php:81
setFieldValue(int $type, string $value)
Definition location.php:563
setAddress(Address $address)
Definition location.php:311
setLongitude(string $longitude)
Definition location.php:451
isParentOf(Location $childCandidate)
Definition location.php:63
setLanguageId(string $languageId)
Definition location.php:249
static fromArray(array $location)
Definition location.php:550
static load(int $id, string $languageId=LANGUAGE_ID)
Definition location.php:474
setParents(Parents $parents)
Definition location.php:212
isEqualTo(Location $location)
Definition location.php:99