From a90da4361d02a53204f198f19072e2d420b394f0 Mon Sep 17 00:00:00 2001
From: xue <>
Date: Sun, 3 Sep 2006 21:33:04 +0000
Subject: Added TEnumerable and updated places where enumerable type should be
used.
---
framework/TComponent.php | 52 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 52 insertions(+)
(limited to 'framework/TComponent.php')
diff --git a/framework/TComponent.php b/framework/TComponent.php
index 21a62abd..b9dbd3d5 100644
--- a/framework/TComponent.php
+++ b/framework/TComponent.php
@@ -443,6 +443,32 @@ class TComponent
}
}
+/**
+ * TEnumerable class.
+ * TEnumerable is the base class for all enumerable types.
+ * To define an enumerable type, extend TEnumberable and define string constants.
+ * Each constant represents an enumerable value.
+ * The constant name must be the same as the constant value.
+ * For example,
+ *
+ * class TTextAlign extends TEnumerable
+ * {
+ * const Left='Left';
+ * const Right='Right';
+ * }
+ *
+ * Then, one can use the enumerable values such as TTextAlign::Left and
+ * TTextAlign::Right.
+ *
+ * @author Qiang Xue
+ * @version $Revision: $ $Date: $
+ * @package System
+ * @since 3.0
+ */
+class TEnumerable
+{
+}
+
/**
* TPropertyValue class
*
@@ -568,6 +594,7 @@ class TPropertyValue
/**
* Converts a value to enum type.
+ * This method is deprecated. Try to use {@link ensureEnumerable}, instead.
* This method mainly performs sanity check of a value to make sure
* it is a valid enumeration value. Each enumeration value is a string
* which is case-sensistive.
@@ -577,6 +604,7 @@ class TPropertyValue
* till the last parameters are enumeration values.
* @return string the valid enumeration value
* @throws TInvalidDataValueException if the original value is not in the string array.
+ * @deprecated deprecated since version 3.0.4
*/
public static function ensureEnum($value,$enums)
{
@@ -590,6 +618,30 @@ class TPropertyValue
else
throw new TInvalidDataValueException('propertyvalue_enumvalue_invalid',$value,implode(' | ',$enums));
}
+
+ /**
+ * 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 {@link TEnumerable}.
+ * @param string enumerable value
+ * @param string enumerable class name
+ * @return string valid enumerable value
+ * @throws TInvalidDataValueException if the original value is not a valid enumerable value.
+ * @see ensureEnum
+ * @sine 3.0.4
+ */
+ public static function ensureEnumerable($value,$enumType)
+ {
+ static $types=array();
+ if(!isset($types[$enumType]))
+ $types[$enumType]=new ReflectionClass($enumType);
+ if($types[$enumType]->hasConstant($value))
+ return $value;
+ else
+ throw new TInvalidDataValueException('propertyvalue_enumvalue_invalid',$value,$enumType);
+ }
}
/**
--
cgit v1.2.3