system system.base system.caching system.caching.dependencies system.collections system.console system.db system.db.ar system.db.schema system.db.schema.cubrid system.db.schema.mssql system.db.schema.mysql system.db.schema.oci system.db.schema.pgsql system.db.schema.sqlite system.gii system.i18n system.i18n.gettext system.logging system.test system.utils system.validators system.web system.web.actions system.web.auth system.web.filters system.web.form system.web.helpers system.web.renderers system.web.services system.web.widgets system.web.widgets.captcha system.web.widgets.pagers zii.behaviors zii.widgets zii.widgets.grid zii.widgets.jui

CPropertyValue

system.utils
继承 class CPropertyValue
可用自 1.0
源码 framework/utils/CPropertyValue.php
CPropertyValue is a helper class that provides static methods to convert component property values to specific types.

CPropertyValue is commonly used in component setter methods to ensure the new property value is of the specific type. For example, a boolean-typed property setter method would be as follows,
public function setPropertyName($value)
{
    $value=CPropertyValue::ensureBoolean($value);
    // $value is now of boolean type
}


Properties can be of the following types with specific type conversion rules:
  • string: a boolean value will be converted to 'true' or 'false'.
  • boolean: string 'true' (case-insensitive) will be converted to true, string 'false' (case-insensitive) will be converted to false.
  • integer
  • float
  • array: string starting with '(' and ending with ')' will be considered as as an array expression and will be evaluated. Otherwise, an array with the value to be ensured is returned.
  • object
  • enum: enumerable type, represented by an array of strings.

公共方法

隐藏继承的方法

方法描述被定义在
ensureArray() Converts a value to array type. CPropertyValue
ensureBoolean() Converts a value to boolean type. CPropertyValue
ensureEnum() Converts a value to enum type. CPropertyValue
ensureFloat() Converts a value to float type. CPropertyValue
ensureInteger() Converts a value to integer type. CPropertyValue
ensureObject() Converts a value to object type. CPropertyValue
ensureString() Converts a value to string type. CPropertyValue

方法详情

ensureArray() 方法
public static array ensureArray(mixed $value)
$value mixed the value to be converted.
{return} array
源码: framework/utils/CPropertyValue.php#108 (显示)
public static function ensureArray($value)
{
    if(
is_string($value))
    {
        
$value trim($value);
        
$len strlen($value);
        if (
$len >= && $value[0] == '(' && $value[$len-1] == ')')
        {
            try
            {
                return eval(
'return array' $value ';');
            }
            catch (
ParseError $e)
            {
                return array();
            }
        }
        else
            return 
$len>0?array($value):array();
    }
    else
        return (array)
$value;
}

Converts a value to array type.

If the value is a string and it is in the form (a,b,c) then an array consisting of each of the elements will be returned. If the value is a string and it is not in this form then an array consisting of just the string will be returned, if the string is empty an empty array will be returned. If the value is not a string then it will return an array containing that value or the same value in case it is already an array.

ensureBoolean() 方法
public static boolean ensureBoolean(mixed $value)
$value mixed the value to be converted.
{return} boolean
源码: framework/utils/CPropertyValue.php#53 (显示)
public static function ensureBoolean($value)
{
    if (
is_string($value))
        return !
strcasecmp($value,'true') || $value!=0;
    else
        return (boolean)
$value;
}

Converts a value to boolean type. Note, string 'true' (case-insensitive) will be converted to true, string 'false' (case-insensitive) will be converted to false. If a string represents a non-zero number, it will be treated as true.

ensureEnum() 方法
public static string ensureEnum(string $value, string $enumType)
$value string the enumerable value to be checked.
$enumType string the enumerable class name (make sure it is included before calling this function).
{return} string the valid enumeration value
源码: framework/utils/CPropertyValue.php#155 (显示)
public static function ensureEnum($value,$enumType)
{
    static 
$types=array();
    if(!isset(
$types[$enumType]))
        
$types[$enumType]=new ReflectionClass($enumType);
    if(
$types[$enumType]->hasConstant($value))
        return 
$value;
    else
        throw new 
CException(Yii::t('yii','Invalid enumerable value "{value}". Please make sure it is among ({enum}).',
            array(
'{value}'=>$value'{enum}'=>implode(', ',$types[$enumType]->getConstants()))));
}

Converts a value to enum type.

This method checks if the value is of the specified enumerable type. A value is a valid enumerable value if it is equal to the name of a constant in the specified enumerable type (class). For more details about enumerable, see CEnumerable.

ensureFloat() 方法
public static float ensureFloat(mixed $value)
$value mixed the value to be converted.
{return} float
源码: framework/utils/CPropertyValue.php#91 (显示)
public static function ensureFloat($value)
{
    return (float)
$value;
}

Converts a value to float type.

ensureInteger() 方法
public static integer ensureInteger(mixed $value)
$value mixed the value to be converted.
{return} integer
源码: framework/utils/CPropertyValue.php#81 (显示)
public static function ensureInteger($value)
{
    return (integer)
$value;
}

Converts a value to integer type.

ensureObject() 方法
public static object ensureObject(mixed $value)
$value mixed the value to be converted.
{return} object
源码: framework/utils/CPropertyValue.php#137 (显示)
public static function ensureObject($value)
{
    return (object)
$value;
}

Converts a value to object type.

ensureString() 方法
public static string ensureString(mixed $value)
$value mixed the value to be converted.
{return} string
源码: framework/utils/CPropertyValue.php#68 (显示)
public static function ensureString($value)
{
    if (
is_bool($value))
        return 
$value?'true':'false';
    else
        return (string)
$value;
}

Converts a value to string type. Note, a boolean value will be converted to 'true' if it is true and 'false' if it is false.