CPropertyValue.php 4.6 KB

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