Index: trunk/extensions/Validator/includes/ListParameter.php |
— | — | @@ -41,6 +41,15 @@ |
42 | 42 | protected $delimiter; |
43 | 43 | |
44 | 44 | /** |
| 45 | + * List of criteria the parameter value as a whole needs to hold against. |
| 46 | + * |
| 47 | + * @since 0.4 |
| 48 | + * |
| 49 | + * @var array of ListParameterCriterion |
| 50 | + */ |
| 51 | + protected $listCriteria; |
| 52 | + |
| 53 | + /** |
45 | 54 | * Constructor. |
46 | 55 | * |
47 | 56 | * @since 0.4 |
— | — | @@ -54,8 +63,24 @@ |
55 | 64 | */ |
56 | 65 | public function __construct( $name, $delimiter = ListParameter::DEFAULT_DELIMITER, $type = Parameter::TYPE_STRING, |
57 | 66 | $default = null, array $aliases = array(), array $criteria = array() ) { |
58 | | - parent::construct( $name, $type, $default, $aliases, $criteria ); |
| 67 | + $itemCriteria = array(); |
| 68 | + $listCriteria = array(); |
| 69 | + |
| 70 | + foreach ( $criteria as $criterion ) { |
| 71 | + if ( $criterion instanceof ListParameterCriterion ) { |
| 72 | + $listCriteria[] = $criterion; |
| 73 | + } |
| 74 | + else { |
| 75 | + $itemCriteria[] = $criterion; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + parent::construct( $name, $type, $default, $aliases, $itemCriteria ); |
| 80 | + |
59 | 81 | $this->delimiter = $delimiter; |
| 82 | + |
| 83 | + $this->cleanCriteria( $listCriteria ); |
| 84 | + $this->listCriteria = $listCriteria; |
60 | 85 | } |
61 | 86 | |
62 | 87 | /** |
— | — | @@ -67,6 +92,73 @@ |
68 | 93 | */ |
69 | 94 | public function isList() { |
70 | 95 | return true; |
71 | | - } |
| 96 | + } |
72 | 97 | |
| 98 | + /** |
| 99 | + * Validates all items in the list. |
| 100 | + * |
| 101 | + * @since 0.4 |
| 102 | + * |
| 103 | + * @return boolean |
| 104 | + */ |
| 105 | + public function validate() { |
| 106 | + if ( $this->setCount == 0 ) { |
| 107 | + if ( $this->isRequired() ) { |
| 108 | + // TODO: fatal error |
| 109 | + $success = false; |
| 110 | + } |
| 111 | + else { |
| 112 | + $success = true; |
| 113 | + $this->value = $this->default; |
| 114 | + } |
| 115 | + } |
| 116 | + else { |
| 117 | + $this->validateListCriteria( $this->value ); |
| 118 | + |
| 119 | + // TODO |
| 120 | + |
| 121 | + foreach ( $this->value as $item ) { |
| 122 | + list( $itemSuccess, $itemHasError ) = $this->validateCriteria( $item ); |
| 123 | + |
| 124 | + // TODO |
| 125 | + |
| 126 | + $success = $success && $itemSuccess; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return $success; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * |
| 135 | + * |
| 136 | + * @since 0.4 |
| 137 | + * |
| 138 | + * @param array $values |
| 139 | + */ |
| 140 | + protected function validateListCriteria( array $values ) { |
| 141 | + foreach ( $this->getListCriteria() as $listCriterion ) { |
| 142 | + if ( !$listCriterion->validate( $value ) ) { |
| 143 | + $hasError = true; |
| 144 | + |
| 145 | + if ( !self::$accumulateParameterErrors ) { |
| 146 | + break; |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // TODO |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Returns the parameter list criteria. |
| 156 | + * |
| 157 | + * @since 0.4 |
| 158 | + * |
| 159 | + * @return array of ListParameterCriterion |
| 160 | + */ |
| 161 | + public function getListCriteria() { |
| 162 | + return $this->listCriteria; |
| 163 | + } |
| 164 | + |
73 | 165 | } |
\ No newline at end of file |
Index: trunk/extensions/Validator/includes/Parameter.php |
— | — | @@ -357,7 +357,11 @@ |
358 | 358 | } |
359 | 359 | } |
360 | 360 | else { |
361 | | - $success = $this->validateCriteria( $this->originalValue ); |
| 361 | + list( $success, $hasError ) = $this->validateCriteria( $this->originalValue ); |
| 362 | + |
| 363 | + if ( $hasError ) { |
| 364 | + $this->value = $this->default; |
| 365 | + } |
362 | 366 | } |
363 | 367 | |
364 | 368 | return $success; |
— | — | @@ -370,7 +374,7 @@ |
371 | 375 | * |
372 | 376 | * @param string $value |
373 | 377 | * |
374 | | - * @return boolean If there where no fatal errors |
| 378 | + * @return array Containing a boolean indicating if there where no fatal errors and one if there where any errors |
375 | 379 | */ |
376 | 380 | protected function validateCriteria( $value ) { |
377 | 381 | $success = true; |
— | — | @@ -386,12 +390,7 @@ |
387 | 391 | } |
388 | 392 | } |
389 | 393 | |
390 | | - // TODO: move this to a nicer place |
391 | | - if ( $hasError ) { |
392 | | - $this->value = $this->default; |
393 | | - } |
394 | | - |
395 | | - return $success; |
| 394 | + return array( $success, $hasError ); |
396 | 395 | } |
397 | 396 | |
398 | 397 | /** |