1
|
|
|
<?php |
2
|
|
|
namespace sibilino\yii2\openlayers; |
3
|
|
|
|
4
|
|
|
use yii\web\JsExpression; |
5
|
|
|
use yii\helpers\Json; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This class makes it easier to specify OpenLayers objects as options for the OpenLayers widget, resulting in more concise PHP array structures than when using JsExpression. |
9
|
|
|
* See {@link OL::__construct} for usage examples. |
10
|
|
|
* @link https://github.com/Sibilino/yii2-openlayers |
11
|
|
|
* @copyright Copyright (c) 2015 Luis Hernández Hernández |
12
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
13
|
|
|
*/ |
14
|
|
|
class OL extends JsExpression |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Must include the full namespace path, excluding 'ol.'. For example, "layer.Tile". |
18
|
|
|
* @var string the classname to be instantiated. |
19
|
|
|
*/ |
20
|
|
|
protected $class; |
21
|
|
|
/** |
22
|
|
|
* @var array|null the properties to be passed to the constructor of $class. |
23
|
|
|
*/ |
24
|
|
|
protected $properties; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The OL class takes a classname and an optional property array as constructor arguments. Then, when an OL object is passed to Json::encode(), the following expression will be generated: |
28
|
|
|
* <code>new ol.$class($properties)</code>. |
29
|
|
|
* Therefore, the need for JsExpression is eliminated, and the 'ol.' namespace, the 'new' keyword and constructor call are automatically added. |
30
|
|
|
* For example, the following mapOptions array: |
31
|
|
|
* <pre>//... |
32
|
|
|
* 'layers' => [ |
33
|
|
|
* new OL('layer.Tile', [ |
34
|
|
|
* 'source' => new OL('source.MapQuest', [ |
35
|
|
|
* 'layer' => 'sat', |
36
|
|
|
* ]), |
37
|
|
|
* ]), |
38
|
|
|
* ], |
39
|
|
|
* 'view' => [ |
40
|
|
|
* new OL('View', [ |
41
|
|
|
* 'center' => [0, 0], |
42
|
|
|
* 'zoom' => 4, |
43
|
|
|
* ], |
44
|
|
|
* ] |
45
|
|
|
* /...</pre> |
46
|
|
|
* Will be encoded as follows: |
47
|
|
|
* <pre> |
48
|
|
|
* { |
49
|
|
|
* "layers": [ |
50
|
|
|
* new ol.layer.Tile({ |
51
|
|
|
* "source": new ol.source.MapQuest({ |
52
|
|
|
* "layer": "sat" |
53
|
|
|
* }) |
54
|
|
|
* }) |
55
|
|
|
* ], |
56
|
|
|
* "view": new ol.View({ |
57
|
|
|
* "center": [0, 0], |
58
|
|
|
* "zoom": 4 |
59
|
|
|
* }) |
60
|
|
|
* } |
61
|
|
|
* </pre> |
62
|
|
|
* @param string $class |
63
|
|
|
* @param mixed $properties |
64
|
|
|
*/ |
65
|
|
|
public function __construct($class, $properties = null) |
66
|
|
|
{ |
67
|
|
|
$this->class = $class; |
68
|
|
|
$this->properties = $properties; |
69
|
|
|
$this->expression = $this->generateExpression(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Generates the JavaScript expression that correspond with this object's $class and $properties. |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
protected function generateExpression() |
77
|
|
|
{ |
78
|
|
|
if (isset($this->properties)) |
79
|
|
|
$properties = Json::encode($this->properties); |
80
|
|
|
else |
81
|
|
|
$properties = ''; |
82
|
|
|
return "new ol.$this->class($properties)"; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function __toString() { |
86
|
|
|
return $this->expression; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |