Class \Prado\TPropertyValue
TPropertyValue is a utility class that provides static methods to convert component property values to specific types.
TPropertyValue is commonly used in component setter methods to ensure the new property value is of specific type. For example, a boolean-typed property setter method would be as follows,
function setPropertyName($value) {
$value=TPropertyValue::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.
Class hierarchy
Author: Qiang Xue <qiang.xue@gmail.com>Since: 3.0
public
static
|
ensureArray(mixed $value) : array<string|int, mixed>
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 value
is not a string then
|
public
static
|
ensureBoolean(mixed $value) : bool
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. |
public
static
|
ensureEnum(mixed $value, mixed $enums) : string
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 TEnumerable. For backward compatibility, this method also supports sanity check of a string value to see if it is among the given list of strings. |
public
static
|
|
public
static
|
ensureHexColor(array<string|int, mixed>|float|int|string $value[, bool|float|int $green = true ][, null|float|int $blue = null ]) : string
Converts the value to a web "#RRGGBB" hex color.
The value[s] could be as an A) Web Color or # Hex Color string, or B) as a color encoded integer, eg 0x00RRGGBB, C) a triple ($value [red], $green, $blue), or D) an array of red, green, and blue, and index 0, 1, 2 or 'red', 'green', 'blue'. In instance (A), $green is treated as a boolean flag for whether to convert any web colors to their # hex color. When red, green, or blue colors are specified they are assumed be be bound [0...255], inclusive. |
public
static
|
|
public
static
|
|
public
static
|
|
public
static
|
ensureString(mixed $value) : string
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. |
|