1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2013-2015 2amigOS! Consulting Group LLC |
4
|
|
|
* @link http://2amigos.us |
5
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace dosamigos\leaflet\types; |
9
|
|
|
|
10
|
|
|
use dosamigos\leaflet\LeafLet; |
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
use yii\helpers\Json; |
13
|
|
|
use yii\web\JsExpression; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Point represents a point with x and y coordinates in pixels. |
17
|
|
|
* |
18
|
|
|
* ``` |
19
|
|
|
* $map->panBy(new Point(['x' => 200, 'y' => '300'])); |
20
|
|
|
* ``` |
21
|
|
|
* |
22
|
|
|
* @see http://leafletjs.com/reference.html#point |
23
|
|
|
* @author Antonio Ramirez <[email protected]> |
24
|
|
|
* @link http://www.ramirezcobos.com/ |
25
|
|
|
* @link http://www.2amigos.us/ |
26
|
|
|
* @package dosamigos\leaflet\types |
27
|
|
|
*/ |
28
|
|
|
class Point extends Type implements ArrayableInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var float x coordinate |
32
|
|
|
*/ |
33
|
|
|
public $x; |
34
|
|
|
/** |
35
|
|
|
* @var float y coordinate |
36
|
|
|
*/ |
37
|
|
|
public $y; |
38
|
|
|
/** |
39
|
|
|
* @var bool if round is set to true, LetLeaf will round the x and y values. |
40
|
|
|
*/ |
41
|
|
|
public $round = false; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Initializes the class |
45
|
|
|
* @throws \yii\base\InvalidConfigException |
46
|
|
|
*/ |
47
|
12 |
|
public function init() |
48
|
|
|
{ |
49
|
12 |
|
if (empty($this->x) || empty($this->y)) { |
50
|
3 |
|
throw new InvalidConfigException("'x' or 'y' cannot be empty."); |
51
|
|
|
} |
52
|
12 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return \yii\web\JsExpression the js initialization code of the object |
56
|
|
|
*/ |
57
|
3 |
|
public function encode() |
58
|
|
|
{ |
59
|
3 |
|
$x = $this->x; |
60
|
3 |
|
$y = $this->y; |
61
|
3 |
|
return new JsExpression("L.point($x, $y)"); // no semicolon |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns the point values as array |
66
|
|
|
* |
67
|
|
|
* @param bool $encode whether to return the array json_encoded or raw |
68
|
|
|
* |
69
|
|
|
* @return array|JsExpression |
70
|
|
|
*/ |
71
|
12 |
|
public function toArray($encode = false) |
72
|
|
|
{ |
73
|
12 |
|
$point = [$this->x, $this->y]; |
74
|
12 |
|
return $encode ? new JsExpression(Json::encode($point, LeafLet::JSON_OPTIONS)) : $point; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|