1: <?php
2: /**
3: * CTypeValidator 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: /**
12: * CTypeValidator verifies if the attribute is of the type specified by {@link type}.
13: *
14: * The following data types are supported:
15: * <ul>
16: * <li><b>integer</b> A 32-bit signed integer data type.</li>
17: * <li><b>float</b> A double-precision floating point number data type.</li>
18: * <li><b>string</b> A string data type.</li>
19: * <li><b>array</b> An array value. </li>
20: * <li><b>date</b> A date data type.</li>
21: * <li><b>time</b> A time data type.</li>
22: * <li><b>datetime</b> A date and time data type.</li>
23: * </ul>
24: *
25: * For <b>date</b> type, the property {@link dateFormat}
26: * will be used to determine how to parse the date string. If the given date
27: * value doesn't follow the format, the attribute is considered as invalid.
28: *
29: * Starting from version 1.1.7, we have a dedicated date validator {@link CDateValidator}.
30: * Please consider using this validator to validate a date-typed value.
31: *
32: * When using the {@link message} property to define a custom error message, the message
33: * may contain additional placeholders that will be replaced with the actual content. In addition
34: * to the "{attribute}" placeholder, recognized by all validators (see {@link CValidator}),
35: * CTypeValidator allows for the following placeholders to be specified:
36: * <ul>
37: * <li>{type}: replaced with data type the attribute should be {@link type}.</li>
38: * </ul>
39: *
40: * @author Qiang Xue <qiang.xue@gmail.com>
41: * @package system.validators
42: * @since 1.0
43: */
44: class CTypeValidator extends CValidator
45: {
46: /**
47: * @var string the data type that the attribute should be. Defaults to 'string'.
48: * Valid values include 'string', 'integer', 'float', 'array', 'date', 'time' and 'datetime'.
49: */
50: public $type='string';
51: /**
52: * @var string the format pattern that the date value should follow. Defaults to 'MM/dd/yyyy'.
53: * Please see {@link CDateTimeParser} for details about how to specify a date format.
54: * This property is effective only when {@link type} is 'date'.
55: */
56: public $dateFormat='MM/dd/yyyy';
57: /**
58: * @var string the format pattern that the time value should follow. Defaults to 'hh:mm'.
59: * Please see {@link CDateTimeParser} for details about how to specify a time format.
60: * This property is effective only when {@link type} is 'time'.
61: */
62: public $timeFormat='hh:mm';
63: /**
64: * @var string the format pattern that the datetime value should follow. Defaults to 'MM/dd/yyyy hh:mm'.
65: * Please see {@link CDateTimeParser} for details about how to specify a datetime format.
66: * This property is effective only when {@link type} is 'datetime'.
67: */
68: public $datetimeFormat='MM/dd/yyyy hh:mm';
69: /**
70: * @var boolean whether the attribute value can be null or empty. Defaults to true,
71: * meaning that if the attribute is empty, it is considered valid.
72: */
73: public $allowEmpty=true;
74:
75: /**
76: * @var boolean whether the actual PHP type of attribute value should be checked.
77: * Defaults to false, meaning that correctly formatted strings are accepted for
78: * integer and float validators.
79: *
80: * @since 1.1.13
81: */
82: public $strict=false;
83:
84: /**
85: * Validates the attribute of the object.
86: * If there is any error, the error message is added to the object.
87: * @param CModel $object the object being validated
88: * @param string $attribute the attribute being validated
89: */
90: protected function validateAttribute($object,$attribute)
91: {
92: $value=$object->$attribute;
93: if($this->allowEmpty && $this->isEmpty($value))
94: return;
95:
96: if(!$this->validateValue($value))
97: {
98: $message=$this->message!==null?$this->message : Yii::t('yii','{attribute} must be {type}.');
99: $this->addError($object,$attribute,$message,array('{type}'=>$this->type));
100: }
101: }
102:
103: /**
104: * Validates a static value.
105: * Note that this method does not respect {@link allowEmpty} property.
106: * This method is provided so that you can call it directly without going through the model validation rule mechanism.
107: * @param mixed $value the value to be validated
108: * @return boolean whether the value is valid
109: * @since 1.1.13
110: */
111: public function validateValue($value)
112: {
113: $type=$this->type==='float' ? 'double' : $this->type;
114: if($type===gettype($value))
115: return true;
116: elseif($this->strict || is_array($value) || is_object($value) || is_resource($value) || is_bool($value))
117: return false;
118:
119: if($type==='integer')
120: return (boolean)preg_match('/^[-+]?[0-9]+$/',trim($value));
121: elseif($type==='double')
122: return (boolean)preg_match('/^[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?$/',trim($value));
123: elseif($type==='date')
124: return CDateTimeParser::parse($value,$this->dateFormat,array('month'=>1,'day'=>1,'hour'=>0,'minute'=>0,'second'=>0))!==false;
125: elseif($type==='time')
126: return CDateTimeParser::parse($value,$this->timeFormat)!==false;
127: elseif($type==='datetime')
128: return CDateTimeParser::parse($value,$this->datetimeFormat, array('month'=>1,'day'=>1,'hour'=>0,'minute'=>0,'second'=>0))!==false;
129:
130: return false;
131: }
132: }