-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleAddress.php
More file actions
114 lines (100 loc) · 2.84 KB
/
SimpleAddress.php
File metadata and controls
114 lines (100 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php namespace ProcessWire;
/**
* Class to hold a simple address
*
*/
class SimpleAddress extends WireData
{
public function __construct()
{
try {
$this->set('street', null);
$this->set('number', null);
$this->set('postalcode', null);
$this->set('city', null);
$this->set('state', null);
$this->set('country', null);
} catch (WireException $e) {
}
}
public function set($key, $value)
{
$value = wire('sanitizer')->text($value);
return parent::set($key, $value);
}
public function get($key)
{
return parent::get($key);
}
/**
* Method to create combined string of street and number
* @return string
*/
public function renderStreet(): string
{
$number = ($this->number && $this->street) ? ' '.$this->number : $this->number;
return ($this->street || $this->number) ? $this->street.$number : '';
}
/**
* Method to create combined string of postalcode and city
* @return string
*/
public function renderCity(): string
{
$city = ($this->postalcode && $this->city) ? ' '.$this->city : $this->city;
return ($this->postalcode || $this->city) ? $this->postalcode.$city : '';
}
/**
* Method to create state stringy
* @return string
*/
public function renderState(): string
{
return ($this->state) ? $this->state : '';
}
/**
* Method to render country string
* @return string
*/
public function renderCountry(): string
{
return ($this->country) ? $this->country : '';
}
/**
* Method to render complete address string
* @param array $options => separator, class
* @return string
*/
public function renderAddress(array $options = []): string
{
$defaultOptions = [
'separator' => '<br />',
'class' => ''
];
$addressOptions = array_merge($defaultOptions, $options);
$addressParts = [
$this->renderStreet(),
$this->renderCity(),
$this->renderState(),
$this->renderCountry()
];
$cleanAddress = array_filter($addressParts);
$class = ($addressOptions['class']) ? ' class="'.$addressOptions['class'].'"' : '';
$out = '<address'.$class.'>';
$out .= implode($addressOptions['separator'], $cleanAddress);
$out .= '</address>';
return $out;
}
public function renderLatLng(): string
{
if($this->lat || $this->lng){
$coordinates = [$this->lat,$this->lng];
return implode(', ', $coordinates);
}
return '';
}
public function __toString()
{
return $this->renderAddress();
}
}