1: <?php
2: /**
3: * CPropertyValue class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CPropertyValue is a helper class that provides static methods to convert component property values to specific types.
13: *
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,
17: * <pre>
18: * public function setPropertyName($value)
19: * {
20: * $value=CPropertyValue::ensureBoolean($value);
21: * // $value is now of boolean type
22: * }
23: * </pre>
24: *
25: * Properties can be of the following types with specific type conversion rules:
26: * <ul>
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>
30: * <li>integer</li>
31: * <li>float</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>
35: * <li>object</li>
36: * <li>enum: enumerable type, represented by an array of strings.</li>
37: * </ul>
38: *
39: * @author Qiang Xue <qiang.xue@gmail.com>
40: * @package system.utils
41: * @since 1.0
42: */
43: class CPropertyValue
44: {
45: /**
46: * Converts a value to boolean type.
47: * Note, string 'true' (case-insensitive) will be converted to true,
48: * string 'false' (case-insensitive) will be converted to false.
49: * If a string represents a non-zero number, it will be treated as true.
50: * @param mixed $value the value to be converted.
51: * @return boolean
52: */
53: public static function ensureBoolean($value)
54: {
55: if (is_string($value))
56: return !strcasecmp($value,'true') || $value!=0;
57: else
58: return (boolean)$value;
59: }
60:
61: /**
62: * Converts a value to string type.
63: * Note, a boolean value will be converted to 'true' if it is true
64: * and 'false' if it is false.
65: * @param mixed $value the value to be converted.
66: * @return string
67: */
68: public static function ensureString($value)
69: {
70: if (is_bool($value))
71: return $value?'true':'false';
72: else
73: return (string)$value;
74: }
75:
76: /**
77: * Converts a value to integer type.
78: * @param mixed $value the value to be converted.
79: * @return integer
80: */
81: public static function ensureInteger($value)
82: {
83: return (integer)$value;
84: }
85:
86: /**
87: * Converts a value to float type.
88: * @param mixed $value the value to be converted.
89: * @return float
90: */
91: public static function ensureFloat($value)
92: {
93: return (float)$value;
94: }
95:
96: /**
97: * Converts a value to array type. If the value is a string and it is
98: * in the form (a,b,c) then an array consisting of each of the elements
99: * will be returned. If the value is a string and it is not in this form
100: * then an array consisting of just the string will be returned. If the value
101: * is not a string then
102: * @param mixed $value the value to be converted.
103: * @return array
104: */
105: public static function ensureArray($value)
106: {
107: if(is_string($value))
108: {
109: $value = trim($value);
110: $len = strlen($value);
111: if ($len >= 2 && $value[0] == '(' && $value[$len-1] == ')')
112: {
113: eval('$array=array'.$value.';');
114: return $array;
115: }
116: else
117: return $len>0?array($value):array();
118: }
119: else
120: return (array)$value;
121: }
122:
123: /**
124: * Converts a value to object type.
125: * @param mixed $value the value to be converted.
126: * @return object
127: */
128: public static function ensureObject($value)
129: {
130: return (object)$value;
131: }
132:
133: /**
134: * Converts a value to enum type.
135: *
136: * This method checks if the value is of the specified enumerable type.
137: * A value is a valid enumerable value if it is equal to the name of a constant
138: * in the specified enumerable type (class).
139: * For more details about enumerable, see {@link CEnumerable}.
140: *
141: * @param string $value the enumerable value to be checked.
142: * @param string $enumType the enumerable class name (make sure it is included before calling this function).
143: * @return string the valid enumeration value
144: * @throws CException if the value is not a valid enumerable value
145: */
146: public static function ensureEnum($value,$enumType)
147: {
148: static $types=array();
149: if(!isset($types[$enumType]))
150: $types[$enumType]=new ReflectionClass($enumType);
151: if($types[$enumType]->hasConstant($value))
152: return $value;
153: else
154: throw new CException(Yii::t('yii','Invalid enumerable value "{value}". Please make sure it is among ({enum}).',
155: array('{value}'=>$value, '{enum}'=>implode(', ',$types[$enumType]->getConstants()))));
156: }
157: }
158: