r72428 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72427‎ | r72428 | r72429 >
Date:14:06, 5 September 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.4 - copied over list criteria validation functions
Modified paths:
  • /trunk/extensions/Validator/Validator.php (modified) (history)
  • /trunk/extensions/Validator/includes/ListParameterCriterion.php (added) (history)
  • /trunk/extensions/Validator/includes/Parameter.php (modified) (history)
  • /trunk/extensions/Validator/includes/ParameterCriterion.php (modified) (history)
  • /trunk/extensions/Validator/includes/criteria/CriterionItemCount.php (added) (history)
  • /trunk/extensions/Validator/includes/criteria/CriterionUniqueItems.php (added) (history)

Diff [purge]

Index: trunk/extensions/Validator/includes/ListParameterCriterion.php
@@ -0,0 +1,26 @@
 2+<?php
 3+
 4+/**
 5+ * List parameter criterion definition class. This is for criteria
 6+ * that apply to list parameters as a whole instead of to their
 7+ * individual items.
 8+ *
 9+ * @since 0.4
 10+ *
 11+ * @file ListParameterCriterion.php
 12+ * @ingroup Validator
 13+ * @ingroup Criteria
 14+ *
 15+ * @author Jeroen De Dauw
 16+ */
 17+abstract class ListParameterCriterion extends ParameterCriterion {
 18+
 19+ /**
 20+ * Constructor.
 21+ *
 22+ * @since 0.4
 23+ */
 24+ public function __construct() {
 25+ parent::__construct();
 26+ }
 27+}
\ No newline at end of file
Property changes on: trunk/extensions/Validator/includes/ListParameterCriterion.php
___________________________________________________________________
Added: svn:eol-style
128 + native
Index: trunk/extensions/Validator/includes/criteria/CriterionItemCount.php
@@ -0,0 +1,42 @@
 2+<?php
 3+
 4+/**
 5+ * Parameter criterion stating that the value must have a certain length.
 6+ *
 7+ * @since 0.4
 8+ *
 9+ * @file CriterionHasLength.php
 10+ * @ingroup Validator
 11+ * @ingroup Criteria
 12+ *
 13+ * @author Jeroen De Dauw
 14+ */
 15+class CriterionHasLength extends ListParameterCriterion {
 16+
 17+ protected $lowerBound;
 18+ protected $upperBound;
 19+
 20+ /**
 21+ * Constructor.
 22+ *
 23+ * @param integer $lowerBound
 24+ * @param mixed $upperBound
 25+ *
 26+ * @since 0.4
 27+ */
 28+ public function __construct( $lowerBound, $upperBound = false ) {
 29+ parent::__construct();
 30+
 31+ $this->lowerBound = $lowerBound;
 32+ $this->upperBound = $upperBound === false ? $lowerBound : $upperBound;
 33+ }
 34+
 35+ /**
 36+ * @see ParameterCriterion::validate
 37+ */
 38+ public function validate( array $values ) {
 39+ $count = count( $values );
 40+ return $count <= $this->upperBound && $count >= $this->lowerBound;
 41+ }
 42+
 43+}
\ No newline at end of file
Property changes on: trunk/extensions/Validator/includes/criteria/CriterionItemCount.php
___________________________________________________________________
Added: svn:eol-style
144 + native
Index: trunk/extensions/Validator/includes/criteria/CriterionUniqueItems.php
@@ -0,0 +1,32 @@
 2+<?php
 3+
 4+/**
 5+ * Parameter criterion stating that the value must have a certain length.
 6+ *
 7+ * @since 0.4
 8+ *
 9+ * @file CriterionHasLength.php
 10+ * @ingroup Validator
 11+ * @ingroup Criteria
 12+ *
 13+ * @author Jeroen De Dauw
 14+ */
 15+class CriterionHasLength extends ListParameterCriterion {
 16+
 17+ /**
 18+ * Constructor.
 19+ *
 20+ * @since 0.4
 21+ */
 22+ public function __construct() {
 23+ parent::__construct();
 24+ }
 25+
 26+ /**
 27+ * @see ParameterCriterion::validate
 28+ */
 29+ public function validate( array $values ) {
 30+ return count( $values ) == count( array_unique( $values ) );
 31+ }
 32+
 33+}
\ No newline at end of file
Property changes on: trunk/extensions/Validator/includes/criteria/CriterionUniqueItems.php
___________________________________________________________________
Added: svn:eol-style
134 + native
Index: trunk/extensions/Validator/includes/ParameterCriterion.php
@@ -7,6 +7,7 @@
88 *
99 * @file ParameterCriterion.php
1010 * @ingroup Validator
 11+ * @ingroup Criteria
1112 *
1213 * @author Jeroen De Dauw
1314 */
@@ -17,7 +18,7 @@
1819 /**
1920 * Validate a value against the criterion.
2021 *
21 - * @param string $value
 22+ * @param mixed $value
2223 *
2324 * @since 0.4
2425 *
Index: trunk/extensions/Validator/includes/Parameter.php
@@ -273,7 +273,11 @@
274274 }
275275
276276 protected function validateCriteria( $value ) {
277 -
 277+ foreach ( $this->getCriteria() as $criterion ) {
 278+ if ( !$criterion->validate( $value ) ) {
 279+
 280+ }
 281+ }
278282 }
279283
280284 /**
@@ -310,22 +314,6 @@
311315 }
312316
313317 /**
314 - * Returns the list delimeter.
315 - *
316 - * @since 0.4
317 - *
318 - * @return string
319 - */
320 - public function getListDelimeter() {
321 - if ( $this->isList() ) {
322 - return count( $this->type ) > 1 ? $this->type[1] : self::$defaultListDelimeter;
323 - }
324 - else {
325 - return false;
326 - }
327 - }
328 -
329 - /**
330318 * Returns the parameter criteria.
331319 *
332320 * @since 0.4
Index: trunk/extensions/Validator/Validator.php
@@ -24,7 +24,7 @@
2525 die( 'Not an entry point.' );
2626 }
2727
28 -define( 'Validator_VERSION', '0.4 alpha-3' );
 28+define( 'Validator_VERSION', '0.4 alpha-4' );
2929
3030 // Constants indicating the strictness of the parameter validation.
3131 define( 'Validator_ERRORS_NONE', 0 );
@@ -54,7 +54,9 @@
5555 // Autoload the classes.
5656 $incDir = dirname( __FILE__ ) . '/includes/';
5757 $wgAutoloadClasses['ListParameter'] = $incDir . 'ListParameter.php';
 58+$wgAutoloadClasses['ListParameterCriterion']= $incDir . 'ListParameterCriterion.php';
5859 $wgAutoloadClasses['Parameter'] = $incDir . 'Parameter.php';
 60+$wgAutoloadClasses['ParameterCriterion'] = $incDir . 'ParameterCriterion.php';
5961 $wgAutoloadClasses['ParserHook'] = $incDir . 'ParserHook.php';
6062 $wgAutoloadClasses['Validator'] = $incDir . 'Validator.php';
6163 $wgAutoloadClasses['TopologicalSort'] = $incDir . 'TopologicalSort.php';
@@ -70,8 +72,10 @@
7173 $wgAutoloadClasses['CriterionIsFloat'] = $incDir . 'criteria/CriterionIsFloat.php';
7274 $wgAutoloadClasses['CriterionIsInteger'] = $incDir . 'criteria/CriterionIsInteger.php';
7375 $wgAutoloadClasses['CriterionIsNumeric'] = $incDir . 'criteria/CriterionIsNumeric.php';
 76+$wgAutoloadClasses['CriterionItemCount'] = $incDir . 'criteria/CriterionItemCount.php';
7477 $wgAutoloadClasses['CriterionMatchesRegex'] = $incDir . 'criteria/CriterionMatchesRegex.php';
7578 $wgAutoloadClasses['CriterionNotEmpty'] = $incDir . 'criteria/CriterionNotEmpty.php';
 79+$wgAutoloadClasses['CriterionUniqueItems'] = $incDir . 'criteria/CriterionUniqueItems.php';
7680
7781 $wgAutoloadClasses['ValidatorListErrors'] = $incDir . 'parserHooks/Validator_ListErrors.php';
7882 unset( $incDir );

Status & tagging log