1 <?php
2 /**
3 * Class file.
4 *
5 * @author Tobias Munk <schmunk@usrbin.de>
6 * @link http://www.phundament.com/
7 * @copyright Copyright © 2005-2012 diemeisterei GmbH
8 * @license http://www.phundament.com/license/
9 */
10
11 /**
12 * CArrayValidator validates that the attribute value is of certain length.
13 *
14 * Note, this validator should only be used with Array-typed attributes.
15 *
16 * @author Tobias Munk <schmunk@usrbin.de>
17 * @package p3extensions.commands
18 * @since 3.0.3
19 */
20 class P3ArrayValidator extends CValidator {
21 /**
22 * @var integer maximum length. Defaults to null, meaning no maximum limit.
23 */
24 public $max;
25 /**
26 * @var integer minimum length. Defaults to null, meaning no minimum limit.
27 */
28 public $min;
29 /**
30 * @var integer exact length. Defaults to null, meaning no exact length limit.
31 */
32 public $is;
33 /**
34 * @var Array user-defined error message used when the value is too long.
35 */
36 public $tooShort;
37 /**
38 * @var Array user-defined error message used when the value is too short.
39 */
40 public $tooLong;
41 /**
42 * @var boolean whether the attribute value can be null or empty. Defaults to true,
43 * meaning that if the attribute is empty, it is considered valid.
44 */
45 public $allowEmpty=true;
46
47 /**
48 * Validates the attribute of the object.
49 * If there is any error, the error message is added to the object.
50 * @param CModel the object being validated
51 * @param Array the attribute being validated
52 */
53 protected function validateAttribute($object,$attribute) {
54 $value=$object->$attribute;
55 if($this->allowEmpty && $this->isEmpty($value))
56 return;
57 elseif(!is_array($value)) {
58 $message=$this->message!==null?$this->message:Yii::t('P2Module.p2','{attribute} is not valid.');
59 $this->addError($object,$attribute,$message);
60 }
61
62 $length=count($value);
63 if($this->min!==null && $length<$this->min) {
64 $message=$this->tooShort!==null?$this->tooShort:Yii::t('P2Module.p2','{attribute} is too short (minimum is {min} entries).');
65 $this->addError($object,$attribute,$message,array('{min}'=>$this->min));
66 }
67 if($this->max!==null && $length>$this->max) {
68 $message=$this->tooLong!==null?$this->tooLong:Yii::t('P2Module.p2','{attribute} is too long (maximum is {max} entries).');
69 $this->addError($object,$attribute,$message,array('{max}'=>$this->max));
70 }
71 if($this->is!==null && $length!==$this->is) {
72 $message=$this->message!==null?$this->message:Yii::t('P2Module.p2','{attribute} is of the wrong length (should be {length} entries).');
73 $this->addError($object,$attribute,$message,array('{length}'=>$this->is));
74 }
75 }
76 }
77
78