ILIAS  release_8 Revision v8.24
class.ilADTLocation.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
5class ilADTLocation extends ilADT
6{
7 protected ?float $longitude;
8 protected ?float $latitude;
9 protected ?int $zoom;
10
11 public const ADT_VALIDATION_ERROR_LONGITUDE = "loc1";
12 public const ADT_VALIDATION_ERROR_LATITUDE = "loc2";
13
14 // definition
15
16 protected function isValidDefinition(ilADTDefinition $a_def): bool
17 {
18 return $a_def instanceof ilADTLocationDefinition;
19 }
20
21 // default
22
23 public function reset(): void
24 {
25 parent::reset();
26 $this->setZoom(9);
27 $this->setLatitude();
28 $this->setLongitude();
29 }
30
31 // properties
32
33 public function setLongitude(float $a_value = null): void
34 {
35 $this->longitude = $a_value;
36 }
37
38 public function getLongitude(): ?float
39 {
40 return $this->longitude;
41 }
42
43 public function setLatitude(?float $a_value = null): void
44 {
45 $this->latitude = $a_value;
46 }
47
48 public function getLatitude(): ?float
49 {
50 return $this->latitude;
51 }
52
53 public function getZoom(): ?int
54 {
55 return $this->zoom;
56 }
57
58 public function setZoom($a_value): void
59 {
60 $this->zoom = max(1, abs((int) $a_value));
61 }
62
63 // comparison
64
65 public function equals(ilADT $a_adt): ?bool
66 {
67 if ($this->getDefinition()->isComparableTo($a_adt)) {
68 return ($this->getLongitude() == $a_adt->getLongitude() &&
69 $this->getLatitude() == $a_adt->getLatitude());
70 }
71 return null;
72 }
73
74 public function isLarger(ilADT $a_adt): ?bool
75 {
76 return null;
77 }
78
79 public function isSmaller(ilADT $a_adt): ?bool
80 {
81 return null;
82 }
83
84 // null
85
86 public function isNull(): bool
87 {
88 return $this->getLongitude() === null && $this->getLatitude() === null;
89 }
90
91 // validation
92
93 public function isValid(): bool
94 {
95 $valid = parent::isValid();
96 $long = $this->getLongitude();
97 $lat = $this->getLatitude();
98 if ($long !== null && $lat !== null) {
99 // 0 - (+-)180
100 if ($long < -180 || $long > 180) {
101 $this->addValidationError(self::ADT_VALIDATION_ERROR_LONGITUDE);
102 $valid = false;
103 }
104 // 0 - (+-)90
105 if ($lat < -90 || $lat > 90) {
106 $this->addValidationError(self::ADT_VALIDATION_ERROR_LATITUDE);
107 $valid = false;
108 }
109 }
110 return $valid;
111 }
112
113
114 // check
115
119 public function translateErrorCode(string $a_code): string
120 {
121 switch ($a_code) {
123 return $this->lng->txt("adt_error_longitude");
124
126 return $this->lng->txt("adt_error_latitude");
127
128 default:
129 return parent::translateErrorCode($a_code);
130 }
131 }
132
133 public function getCheckSum(): ?string
134 {
135 if (!$this->isNull()) {
136 return md5($this->getLongitude() .
137 "#" . $this->getLatitude() .
138 "#" . $this->getZoom());
139 }
140 return null;
141 }
142
143 public function exportStdClass(): ?stdClass
144 {
145 if (!$this->isNull()) {
146 $obj = new stdClass();
147 $obj->lat = $this->getLatitude();
148 $obj->long = $this->getLongitude();
149 $obj->zoom = $this->getZoom();
150 return $obj;
151 }
152 return null;
153 }
154
155 public function importStdClass(?stdClass $a_std): void
156 {
157 if (is_object($a_std)) {
158 $this->setLatitude($a_std->lat);
159 $this->setLongitude($a_std->long);
160 $this->setZoom($a_std->zoom);
161 }
162 }
163}
ADT definition base class.
const ADT_VALIDATION_ERROR_LONGITUDE
setLongitude(float $a_value=null)
setLatitude(?float $a_value=null)
exportStdClass()
Export value as stdClass.
isValidDefinition(ilADTDefinition $a_def)
Check if definition is valid for ADT.
getCheckSum()
Get unique checksum.
isLarger(ilADT $a_adt)
Check if given ADT is larger than self.
isNull()
Is currently null.
reset()
Init property defaults.
isSmaller(ilADT $a_adt)
Check if given ADT is smaller than self.
const ADT_VALIDATION_ERROR_LATITUDE
translateErrorCode(string $a_code)
@inheritcoc
importStdClass(?stdClass $a_std)
Import value from stdClass.
equals(ilADT $a_adt)
Check if given ADT equals self.
ADT base class.
Definition: class.ilADT.php:12
addValidationError(string $a_error_code)
getDefinition()
Get definition.
Definition: class.ilADT.php:92
$valid