-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEGmap3OptionBase.php
133 lines (125 loc) · 3.59 KB
/
EGmap3OptionBase.php
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* EGmap3 Yii extension
*
* Object oriented PHP interface to GMAP3 Javascript library for
* Google Maps.
*
* @copyright © Digitick <www.digitick.net> 2011
* @license GNU Lesser General Public License v3.0
* @author Ianaré Sévi
*
*/
/**
* Base class for all options.
*/
abstract class EGmap3OptionBase extends EGmap3ObjectBase
{
/**
* Get the option validation information. Must be overriden by the child
* class if there are checks to run.
* @return array
*/
public function getOptionChecks()
{
return array();
}
/**
* Go through the options passed to the object and make sure they are valid.
*/
public function verifyOptions()
{
$thisClass = get_class($this);
foreach ($this->getOptionChecks() as $property => $allowed) {
$propertyValue = $this->$property;
if ($propertyValue === null) {
continue;
}
// array of allowed values
if (is_array($allowed)) {
if (!in_array($propertyValue, $allowed)) {
throw new CException("'Invalid value given for '{$thisClass}.{$property}' : '{$propertyValue}'.");
}
}
// allowed class
else if (is_string($allowed) && strpos($allowed, 'class') !== false) {
// class name different from property name
if (strpos($allowed, 'class:') !== false) {
$className = substr($allowed, 6);
}
// class name same as property name
else {
$className = 'EGmap3' . ucfirst($property);
}
$this->$property = $this->verifyClassType($thisClass, $property, $className, $propertyValue);
$this->$property->verifyOptions();
}
/*TODO*/
// array of a given class
else if (is_string($allowed) && strpos($allowed, 'arrayOfClass:') !== false) {
$className = substr($allowed, 13);
// basic sanity check
if (!is_array($propertyValue)) {
throw new CException("The value of '{$thisClass}.{$property}' must be an array containg objects of class : '{$className}'.");
}
// verify each sub-object
foreach ($propertyValue as $prop => $val) {
$object = $this->verifyClassType($thisClass, $prop, $className, $val);
if ($object->verifyOptions()) {
$propertyValue[$prop] = $object;
}
}
}
// must be an array
else if (is_string($allowed) && strpos($allowed, 'array') !== false) {
if (!is_array($propertyValue)) {
throw new CException("The value of '{$thisClass}.{$property}' must be an array.");
}
}
}
return true;
}
/**
* Check that a given value is an object of a given type.
* If the value is an array, attempt to create an object of the array values.
*
* @param string $property Object property
* @param string $className Name of class to verify or instanciate
* @param mixed $propertyValue Value to check
* @return EGmap3OptionBase
*/
protected function verifyClassType($thisClass, $property, $className, $propertyValue)
{
// array given, convert to object
if (is_array($propertyValue)) {
$object = new $className();
foreach ($propertyValue as $k => $v) {
$object->$k = $v;
}
return $object;
}
// object given, check type
else if (is_object($propertyValue)) {
if (get_class($propertyValue) !== $className) {
throw new CException("Invalid object type given for '{$thisClass}.{$property}' : " . get_class($propertyValue));
}
return $propertyValue;
}
else {
throw new CException("The value of '{$thisClass}.{$property}' must be an object or an array.");
}
}
/**
* Determine if any options are set.
* @return boolean
*/
public function isEmpty()
{
foreach (get_object_vars($this) as $var) {
if ($var !== null) {
return false;
}
}
return true;
}
}