~horux-dev/horux-webcli/thfo

« back to all changes in this revision

Viewing changes to yii/framework/utils/CPropertyValue.php

  • Committer: Thierry Forchelet
  • Date: 2011-02-25 13:30:15 UTC
  • Revision ID: thierry.forchelet@letux.ch-20110225133015-zxyj9w7sqv8ly971
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * CPropertyValue class file.
 
4
 *
 
5
 * @author Qiang Xue <qiang.xue@gmail.com>
 
6
 * @link http://www.yiiframework.com/
 
7
 * @copyright Copyright &copy; 2008-2011 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
 * @version $Id: CPropertyValue.php 2799 2011-01-01 19:31:13Z qiang.xue $
 
41
 * @package system.utils
 
42
 * @since 1.0
 
43
 */
 
44
class CPropertyValue
 
45
{
 
46
        /**
 
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.
 
52
         * @return boolean
 
53
         */
 
54
        public static function ensureBoolean($value)
 
55
        {
 
56
                if (is_string($value))
 
57
                        return !strcasecmp($value,'true') || $value!=0;
 
58
                else
 
59
                        return (boolean)$value;
 
60
        }
 
61
 
 
62
        /**
 
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.
 
67
         * @return string
 
68
         */
 
69
        public static function ensureString($value)
 
70
        {
 
71
                if (is_bool($value))
 
72
                        return $value?'true':'false';
 
73
                else
 
74
                        return (string)$value;
 
75
        }
 
76
 
 
77
        /**
 
78
         * Converts a value to integer type.
 
79
         * @param mixed $value the value to be converted.
 
80
         * @return integer
 
81
         */
 
82
        public static function ensureInteger($value)
 
83
        {
 
84
                return (integer)$value;
 
85
        }
 
86
 
 
87
        /**
 
88
         * Converts a value to float type.
 
89
         * @param mixed $value the value to be converted.
 
90
         * @return float
 
91
         */
 
92
        public static function ensureFloat($value)
 
93
        {
 
94
                return (float)$value;
 
95
        }
 
96
 
 
97
        /**
 
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.
 
104
         * @return array
 
105
         */
 
106
        public static function ensureArray($value)
 
107
        {
 
108
                if(is_string($value))
 
109
                {
 
110
                        $value = trim($value);
 
111
                        $len = strlen($value);
 
112
                        if ($len >= 2 && $value[0] == '(' && $value[$len-1] == ')')
 
113
                        {
 
114
                                eval('$array=array'.$value.';');
 
115
                                return $array;
 
116
                        }
 
117
                        else
 
118
                                return $len>0?array($value):array();
 
119
                }
 
120
                else
 
121
                        return (array)$value;
 
122
        }
 
123
 
 
124
        /**
 
125
         * Converts a value to object type.
 
126
         * @param mixed $value the value to be converted.
 
127
         * @return object
 
128
         */
 
129
        public static function ensureObject($value)
 
130
        {
 
131
                return (object)$value;
 
132
        }
 
133
 
 
134
        /**
 
135
         * Converts a value to enum type.
 
136
         *
 
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}.
 
141
         *
 
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
 
146
         */
 
147
        public static function ensureEnum($value,$enumType)
 
148
        {
 
149
                static $types=array();
 
150
                if(!isset($types[$enumType]))
 
151
                        $types[$enumType]=new ReflectionClass($enumType);
 
152
                if($types[$enumType]->hasConstant($value))
 
153
                        return $value;
 
154
                else
 
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()))));
 
157
        }
 
158
}