|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @copyright (c) 2013-2015 Cross Solution (http://cross-solution.de) |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** GetText.php */ |
|
11
|
|
|
namespace Geo\Form; |
|
12
|
|
|
|
|
13
|
|
|
use Geo\Form\GeoText\Converter; |
|
14
|
|
|
use Jobs\Entity\Location; |
|
15
|
|
|
use Zend\Form\Element\Hidden; |
|
16
|
|
|
use Zend\Form\Element\Text; |
|
17
|
|
|
use Core\Form\ViewPartialProviderInterface; |
|
18
|
|
|
use Zend\Form\ElementPrepareAwareInterface; |
|
19
|
|
|
use Zend\Form\FormInterface; |
|
20
|
|
|
|
|
21
|
|
|
class GeoText extends Text implements ViewPartialProviderInterface, ElementPrepareAwareInterface |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
protected $partial = 'geo/form/GeoText'; |
|
25
|
|
|
|
|
26
|
|
|
protected $nameElement; |
|
27
|
|
|
protected $dataElement; |
|
28
|
|
|
protected $typeElement; |
|
29
|
|
|
protected $converter; |
|
30
|
|
|
protected $filter; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct($name = null, array $options = null) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->nameElement = new Text('name'); |
|
35
|
|
|
$this->dataElement = new Hidden('data'); |
|
36
|
|
|
$this->typeElement = new Hidden('type'); |
|
37
|
|
|
|
|
38
|
|
|
parent::__construct($name, $options); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function setOptions($options) |
|
42
|
|
|
{ |
|
43
|
|
|
parent::setOptions($options); |
|
44
|
|
|
|
|
45
|
|
|
if (isset($options['engine_type'])) { |
|
46
|
|
|
$this->setType($options['engine_type']); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function setConverter($converter) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->converter = $converter; |
|
55
|
|
|
|
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function setFilter($filter) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->filter = $filter; |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getConverter() |
|
67
|
|
|
{ |
|
68
|
|
|
if (!$this->converter) { |
|
69
|
|
|
$this->setConverter(new Converter()); |
|
70
|
|
|
} |
|
71
|
|
|
return $this->converter; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function getViewPartial() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->partial; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function setViewPartial($partial) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->partial = $partial; |
|
82
|
|
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Prepare the form element (mostly used for rendering purposes) |
|
87
|
|
|
* |
|
88
|
|
|
* @param FormInterface $form |
|
89
|
|
|
* |
|
90
|
|
|
* @return mixed |
|
91
|
|
|
*/ |
|
92
|
|
|
public function prepareElement(FormInterface $form) |
|
93
|
|
|
{ |
|
94
|
|
|
$name = $this->getName(); |
|
95
|
|
|
$id = str_replace(array('[', ']'), array('-', ''), $name); |
|
96
|
|
|
$this->setAttribute('id', $id); |
|
97
|
|
|
$this->nameElement->setName($name . '[name]') |
|
98
|
|
|
->setAttribute('id', $id . '-name') |
|
99
|
|
|
->setAttribute('class', 'form-control geolocation'); |
|
100
|
|
|
$this->dataElement->setName($name . '[data]') |
|
101
|
|
|
->setAttribute('id', $id . '-data'); |
|
102
|
|
|
$this->typeElement->setName($name . '[type]') |
|
103
|
|
|
->setAttribute('id', $id . '-type'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return mixed |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getDataElement() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->dataElement; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return mixed |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getNameElement() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->nameElement; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function getTypeElement() |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->typeElement; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function setType($type) |
|
128
|
|
|
{ |
|
129
|
|
|
$this->typeElement->setValue($type); |
|
130
|
|
|
|
|
131
|
|
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param mixed $value |
|
136
|
|
|
* @param null $type |
|
137
|
|
|
* |
|
138
|
|
|
* @return $this |
|
139
|
|
|
*/ |
|
140
|
|
|
public function setValue($value, $type=null) |
|
141
|
|
|
{ |
|
142
|
|
|
if ($value instanceOf Location) { |
|
143
|
|
|
$value = $this->getConverter()->toValue($value, $type ?: $this->typeElement->getValue()); |
|
144
|
|
|
} |
|
145
|
|
|
if ('geo' == $value['type'] && empty($value['data'])) { |
|
146
|
|
|
$lonLat = $this->getConverter()->toCoordinates($value['name']); |
|
147
|
|
|
foreach($lonLat as $k=>$v) { |
|
148
|
|
|
list($lon,$lat) = explode(',', $v, 2); |
|
149
|
|
|
$latLon[]=$lat.','.$lon; |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
$value['data'] = ['coordinates'=>[ (float) $lat, (float) $lon] ,'type'=>'Point', 'region' => '' ,'postalcode' =>'', 'country' => 'DE']; |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
if (!is_array($value)) { |
|
154
|
|
|
$value = explode('|', $value, 2); |
|
155
|
|
|
$value = [ |
|
156
|
|
|
'name' => $value[0], |
|
157
|
|
|
'data' => isset($value[1]) ? $value[1] : '', |
|
158
|
|
|
]; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$this->nameElement->setValue($value['name']); |
|
162
|
|
|
$this->dataElement->setValue($value['data']); |
|
163
|
|
|
|
|
164
|
|
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param string $type |
|
169
|
|
|
* |
|
170
|
|
|
* @return array|mixed |
|
171
|
|
|
*/ |
|
172
|
|
|
public function getValue($type = 'name') |
|
173
|
|
|
{ |
|
174
|
|
|
switch ($type) { |
|
175
|
|
|
case 'entity': |
|
176
|
|
|
default: |
|
177
|
|
|
return $this->getConverter()->toEntity($this->dataElement->getValue(), $this->typeElement->getValue()); |
|
178
|
|
|
break; |
|
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
case 'all': |
|
|
|
|
|
|
181
|
|
|
return [ |
|
182
|
|
|
'name' => $this->nameElement->getValue(), |
|
183
|
|
|
'data' => $this->dataElement->getValue(), |
|
184
|
|
|
'type' => $this->typeElement->getValue(), |
|
185
|
|
|
]; |
|
186
|
|
|
break; |
|
187
|
|
|
|
|
188
|
|
|
case 'name': |
|
189
|
|
|
return $this->nameElement->getValue(); |
|
190
|
|
|
break; |
|
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
case 'data': |
|
193
|
|
|
return $this->dataElement->getValue(); |
|
194
|
|
|
break; |
|
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
case 'type': |
|
197
|
|
|
return $this->typeElement->getValue(); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.