ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilADTLocation.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 class ilADTLocation extends ilADT
22 {
23  protected ?float $longitude;
24  protected ?float $latitude;
25  protected ?int $zoom;
26 
27  public const ADT_VALIDATION_ERROR_LONGITUDE = "loc1";
28  public const ADT_VALIDATION_ERROR_LATITUDE = "loc2";
29 
30  // definition
31 
32  protected function isValidDefinition(ilADTDefinition $a_def): bool
33  {
34  return $a_def instanceof ilADTLocationDefinition;
35  }
36 
37  // default
38 
39  public function reset(): void
40  {
41  parent::reset();
42  $this->setZoom(9);
43  $this->setLatitude();
44  $this->setLongitude();
45  }
46 
47  // properties
48 
49  public function setLongitude(?float $a_value = null): void
50  {
51  $this->longitude = $a_value;
52  }
53 
54  public function getLongitude(): ?float
55  {
56  return $this->longitude;
57  }
58 
59  public function setLatitude(?float $a_value = null): void
60  {
61  $this->latitude = $a_value;
62  }
63 
64  public function getLatitude(): ?float
65  {
66  return $this->latitude;
67  }
68 
69  public function getZoom(): ?int
70  {
71  return $this->zoom;
72  }
73 
74  public function setZoom($a_value): void
75  {
76  $this->zoom = max(1, abs((int) $a_value));
77  }
78 
79  // comparison
80 
81  public function equals(ilADT $a_adt): ?bool
82  {
83  if ($this->getDefinition()->isComparableTo($a_adt)) {
84  return ($this->getLongitude() == $a_adt->getLongitude() &&
85  $this->getLatitude() == $a_adt->getLatitude());
86  }
87  return null;
88  }
89 
90  public function isLarger(ilADT $a_adt): ?bool
91  {
92  return null;
93  }
94 
95  public function isSmaller(ilADT $a_adt): ?bool
96  {
97  return null;
98  }
99 
100  // null
101 
102  public function isNull(): bool
103  {
104  return $this->getLongitude() === null && $this->getLatitude() === null;
105  }
106 
107  // validation
108 
109  public function isValid(): bool
110  {
111  $valid = parent::isValid();
112  $long = $this->getLongitude();
113  $lat = $this->getLatitude();
114  if ($long !== null && $lat !== null) {
115  // 0 - (+-)180
116  if ($long < -180 || $long > 180) {
117  $this->addValidationError(self::ADT_VALIDATION_ERROR_LONGITUDE);
118  $valid = false;
119  }
120  // 0 - (+-)90
121  if ($lat < -90 || $lat > 90) {
122  $this->addValidationError(self::ADT_VALIDATION_ERROR_LATITUDE);
123  $valid = false;
124  }
125  }
126  return $valid;
127  }
128 
129 
130  // check
131 
135  public function translateErrorCode(string $a_code): string
136  {
137  switch ($a_code) {
138  case self::ADT_VALIDATION_ERROR_LONGITUDE:
139  return $this->lng->txt("adt_error_longitude");
140 
141  case self::ADT_VALIDATION_ERROR_LATITUDE:
142  return $this->lng->txt("adt_error_latitude");
143 
144  default:
145  return parent::translateErrorCode($a_code);
146  }
147  }
148 
149  public function getCheckSum(): ?string
150  {
151  if (!$this->isNull()) {
152  return md5($this->getLongitude() .
153  "#" . $this->getLatitude() .
154  "#" . $this->getZoom());
155  }
156  return null;
157  }
158 
159  public function exportStdClass(): ?stdClass
160  {
161  if (!$this->isNull()) {
162  $obj = new stdClass();
163  $obj->lat = $this->getLatitude();
164  $obj->long = $this->getLongitude();
165  $obj->zoom = $this->getZoom();
166  return $obj;
167  }
168  return null;
169  }
170 
171  public function importStdClass(?stdClass $a_std): void
172  {
173  if (is_object($a_std)) {
174  $this->setLatitude($a_std->lat);
175  $this->setLongitude($a_std->long);
176  $this->setZoom($a_std->zoom);
177  }
178  }
179 }
addValidationError(string $a_error_code)
setLongitude(?float $a_value=null)
isSmaller(ilADT $a_adt)
isValidDefinition(ilADTDefinition $a_def)
$valid
const ADT_VALIDATION_ERROR_LATITUDE
ADT base class.
Definition: class.ilADT.php:25
translateErrorCode(string $a_code)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
importStdClass(?stdClass $a_std)
setLatitude(?float $a_value=null)
equals(ilADT $a_adt)
const ADT_VALIDATION_ERROR_LONGITUDE
isLarger(ilADT $a_adt)
ADT definition base class.
getDefinition()
Get definition.