3
* CPropertyValue class file.
5
* @author Qiang Xue <qiang.xue@gmail.com>
6
* @link http://www.yiiframework.com/
7
* @copyright Copyright © 2008-2011 Yii Software LLC
8
* @license http://www.yiiframework.com/license/
12
* CPropertyValue is a helper class that provides static methods to convert component property values to specific types.
14
* CPropertyValue is commonly used in component setter methods to ensure
15
* the new property value is of the specific type.
16
* For example, a boolean-typed property setter method would be as follows,
18
* public function setPropertyName($value)
20
* $value=CPropertyValue::ensureBoolean($value);
21
* // $value is now of boolean type
25
* Properties can be of the following types with specific type conversion rules:
27
* <li>string: a boolean value will be converted to 'true' or 'false'.</li>
28
* <li>boolean: string 'true' (case-insensitive) will be converted to true,
29
* string 'false' (case-insensitive) will be converted to false.</li>
32
* <li>array: string starting with '(' and ending with ')' will be considered as
33
* as an array expression and will be evaluated. Otherwise, an array
34
* with the value to be ensured is returned.</li>
36
* <li>enum: enumerable type, represented by an array of strings.</li>
39
* @author Qiang Xue <qiang.xue@gmail.com>
40
* @version $Id: CPropertyValue.php 2799 2011-01-01 19:31:13Z qiang.xue $
41
* @package system.utils
47
* Converts a value to boolean type.
48
* Note, string 'true' (case-insensitive) will be converted to true,
49
* string 'false' (case-insensitive) will be converted to false.
50
* If a string represents a non-zero number, it will be treated as true.
51
* @param mixed $value the value to be converted.
54
public static function ensureBoolean($value)
56
if (is_string($value))
57
return !strcasecmp($value,'true') || $value!=0;
59
return (boolean)$value;
63
* Converts a value to string type.
64
* Note, a boolean value will be converted to 'true' if it is true
65
* and 'false' if it is false.
66
* @param mixed $value the value to be converted.
69
public static function ensureString($value)
72
return $value?'true':'false';
74
return (string)$value;
78
* Converts a value to integer type.
79
* @param mixed $value the value to be converted.
82
public static function ensureInteger($value)
84
return (integer)$value;
88
* Converts a value to float type.
89
* @param mixed $value the value to be converted.
92
public static function ensureFloat($value)
98
* Converts a value to array type. If the value is a string and it is
99
* in the form (a,b,c) then an array consisting of each of the elements
100
* will be returned. If the value is a string and it is not in this form
101
* then an array consisting of just the string will be returned. If the value
102
* is not a string then
103
* @param mixed $value the value to be converted.
106
public static function ensureArray($value)
108
if(is_string($value))
110
$value = trim($value);
111
$len = strlen($value);
112
if ($len >= 2 && $value[0] == '(' && $value[$len-1] == ')')
114
eval('$array=array'.$value.';');
118
return $len>0?array($value):array();
121
return (array)$value;
125
* Converts a value to object type.
126
* @param mixed $value the value to be converted.
129
public static function ensureObject($value)
131
return (object)$value;
135
* Converts a value to enum type.
137
* This method checks if the value is of the specified enumerable type.
138
* A value is a valid enumerable value if it is equal to the name of a constant
139
* in the specified enumerable type (class).
140
* For more details about enumerable, see {@link CEnumerable}.
142
* @param string $value the enumerable value to be checked.
143
* @param string $enumType the enumerable class name (make sure it is included before calling this function).
144
* @return string the valid enumeration value
145
* @throws CException if the value is not a valid enumerable value
147
public static function ensureEnum($value,$enumType)
149
static $types=array();
150
if(!isset($types[$enumType]))
151
$types[$enumType]=new ReflectionClass($enumType);
152
if($types[$enumType]->hasConstant($value))
155
throw new CException(Yii::t('yii','Invalid enumerable value "{value}". Please make sure it is among ({enum}).',
156
array('{value}'=>$value, '{enum}'=>implode(', ',$types[$enumType]->getConstants()))));