r72414 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72413‎ | r72414 | r72415 >
Date:12:24, 5 September 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.4 - added ListParameter class
Modified paths:
  • /trunk/extensions/Validator/Validator.php (modified) (history)
  • /trunk/extensions/Validator/includes/ListParameter.php (added) (history)
  • /trunk/extensions/Validator/includes/Parameter.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Validator/includes/ListParameter.php
@@ -0,0 +1,62 @@
 2+<?php
 3+
 4+/**
 5+ * Class for list parameters.
 6+ *
 7+ * @since 0.4
 8+ *
 9+ * @file ListParameter.php
 10+ * @ingroup Validator
 11+ *
 12+ * @author Jeroen De Dauw
 13+ */
 14+class ListParameter extends Parameter {
 15+
 16+ /**
 17+ * The default delimiter for lists, used when the parameter definition does not specify one.
 18+ *
 19+ * @since 0.4
 20+ *
 21+ * @var string
 22+ */
 23+ const DEFAULT_DELIMITER = ',';
 24+
 25+ /**
 26+ * The list delimiter.
 27+ *
 28+ * @since 0.4
 29+ *
 30+ * @var string
 31+ */
 32+ protected $delimiter;
 33+
 34+ /**
 35+ * Constructor.
 36+ *
 37+ * @since 0.4
 38+ *
 39+ * @param string $name
 40+ * @param string $delimiter
 41+ * @param mixed $type
 42+ * @param mixed $default Use null for no default (which makes the parameter required)
 43+ * @param array $aliases
 44+ * @param array $criteria
 45+ */
 46+ public function __construct( $name, $delimiter = ListParameter::DEFAULT_DELIMITER, $type = Parameter::TYPE_STRING,
 47+ $default = null, array $aliases = array(), array $criteria = array() ) {
 48+ parent::construct( $name, $type, $default, $aliases, $criteria );
 49+ $this->delimiter = $delimiter;
 50+ }
 51+
 52+ /**
 53+ * Returns if the parameter is a list or not.
 54+ *
 55+ * @since 0.4
 56+ *
 57+ * @return boolean
 58+ */
 59+ public function isList() {
 60+ return true;
 61+ }
 62+
 63+}
\ No newline at end of file
Property changes on: trunk/extensions/Validator/includes/ListParameter.php
___________________________________________________________________
Added: svn:eol-style
164 + native
Index: trunk/extensions/Validator/includes/Parameter.php
@@ -3,8 +3,6 @@
44 /**
55 * Parameter definition class.
66 *
7 - * TODO: create deriving ListParameter class and split list logic off to it
8 - *
97 * @since 0.4
108 *
119 * @file Parameter.php
@@ -22,15 +20,6 @@
2321 const TYPE_CHAR = 'char';
2422
2523 /**
26 - * The default delimiter for lists, used when the parameter definition does not specify one.
27 - *
28 - * @since 0.4
29 - *
30 - * @var string
31 - */
32 - public static $defaultListDelimeter = ',';
33 -
34 - /**
3524 * Indicates if the parameter value should be lowercased.
3625 *
3726 * @since 0.4
@@ -76,13 +65,11 @@
7766 protected $name;
7867
7968 /**
80 - * The type of the parameter. Is either a string of the Parameter::TYPE_ enum,
81 - * or an array with such a string as first element, and optionaly a delimiter
82 - * as second element for list types.
 69+ * The type of the parameter, element of the Parameter::TYPE_ enum.
8370 *
8471 * @since 0.4
8572 *
86 - * @var mixed
 73+ * @var string
8774 */
8875 protected $type;
8976
@@ -100,11 +87,41 @@
10188 *
10289 * @since 0.4
10390 *
104 - * @var array
 91+ * @var array of ParameterCriterion
10592 */
10693 protected $criteria;
10794
10895 /**
 96+ * The original parameter name as provided by the user. This can be the
 97+ * main name or an alias.
 98+ *
 99+ * @since 0.4
 100+ *
 101+ * @var string
 102+ */
 103+ protected $originalName;
 104+
 105+ /**
 106+ * The original value as provided by the user. This is mainly retained for
 107+ * usage in error messages when the parameter turns out to be invalid.
 108+ *
 109+ * @since 0.4
 110+ *
 111+ * @var string
 112+ */
 113+ protected $originalValue;
 114+
 115+ /**
 116+ * Keeps track of how many times the parameter has been set by the user.
 117+ * This is used to detect overrides and for figuring out a parameter is missing.
 118+ *
 119+ * @since 0.4
 120+ *
 121+ * @var integer
 122+ */
 123+ protected $setCount = 0;
 124+
 125+ /**
109126 * Returns a new instance of Parameter by converting a Validator 3.x-style parameter array definition.
110127 * Note: this method is for backward compatibility and should not be used in new code.
111128 *
@@ -116,15 +133,17 @@
117134 * @return Parameter
118135 */
119136 public static function newFromArray( $name, array $definition ) {
 137+ $isList = false;
 138+ $delimiter = false;
 139+
120140 if ( array_key_exists( 'type', $definition ) ) {
121141 if ( is_array( $definition['type'] ) ) {
122142 if ( count( $definition['type'] ) > 1 ) {
 143+ $isList = true;
 144+
123145 if ( count( $definition['type'] ) > 2 ) {
124 - $type = array( $definition['type'][0], $definition['type'][2] );
 146+ $delimiter = $definition['type'][2];
125147 }
126 - else {
127 - $type = array( $definition['type'][0] );
128 - }
129148 }
130149 else {
131150 $type = $definition['type'][0];
@@ -145,13 +164,25 @@
146165 $default = array_key_exists( 'default', $definition ) ? $definition['default'] : '';
147166 }
148167
149 - $parameter = new Parameter(
150 - $name,
151 - $type,
152 - $default,
153 - array_key_exists( 'aliases', $definition ) ? $definition['aliases'] : array(),
154 - array_key_exists( 'criteria', $definition ) ? $definition['criteria'] : array()
155 - );
 168+ if ( $isList ) {
 169+ $parameter = new ListParameter(
 170+ $name,
 171+ $delimiter,
 172+ $type,
 173+ $default,
 174+ array_key_exists( 'aliases', $definition ) ? $definition['aliases'] : array(),
 175+ array_key_exists( 'criteria', $definition ) ? $definition['criteria'] : array()
 176+ );
 177+ }
 178+ else {
 179+ $parameter = new Parameter(
 180+ $name,
 181+ $type,
 182+ $default,
 183+ array_key_exists( 'aliases', $definition ) ? $definition['aliases'] : array(),
 184+ array_key_exists( 'criteria', $definition ) ? $definition['criteria'] : array()
 185+ );
 186+ }
156187
157188 if ( array_key_exists( 'output-types', $definition ) ) {
158189 $types = array();
@@ -191,7 +222,7 @@
192223 * @since 0.4
193224 *
194225 * @param string $name
195 - * @param mixed $type
 226+ * @param string $type
196227 * @param mixed $default Use null for no default (which makes the parameter required)
197228 * @param array $aliases
198229 * @param array $criteria
@@ -205,6 +236,47 @@
206237 }
207238
208239 /**
 240+ * @since 0.4
 241+ *
 242+ * @param string $paramName
 243+ * @param string $paramValue
 244+ */
 245+ public function setUserValue( $paramName, $paramValue ) {
 246+ if ( $this->setCount > 0 && true /* TODO: accept overridng? */ ) {
 247+ // TODO: fatal error
 248+ }
 249+ else {
 250+ $this->originalName = $paramName;
 251+ $this->paramValue = $paramValue;
 252+
 253+ $this->setCount++;
 254+ }
 255+ }
 256+
 257+ /**
 258+ * Validates the parameter value against it's criteria.
 259+ *
 260+ * @since 0.4
 261+ */
 262+ public function validate() {
 263+ if ( $this->setCount == 0 ) {
 264+ if ( $this->isRequired() ) {
 265+ // TODO: fatal error
 266+ }
 267+ else {
 268+ $this->validateCriteria( $this->default );
 269+ }
 270+ }
 271+ else {
 272+ $this->validateCriteria( $this->originalValue );
 273+ }
 274+ }
 275+
 276+ protected function validateCriteria( $value ) {
 277+
 278+ }
 279+
 280+ /**
209281 * Returns the parameters main name.
210282 *
211283 * @since 0.4
@@ -234,7 +306,7 @@
235307 * @return boolean
236308 */
237309 public function isList() {
238 - return is_array( $this->type );
 310+ return false;
239311 }
240312
241313 /**
@@ -251,14 +323,14 @@
252324 else {
253325 return false;
254326 }
255 - }
 327+ }
256328
257329 /**
258330 * Returns the parameter criteria.
259331 *
260332 * @since 0.4
261333 *
262 - * @return array
 334+ * @return array of ParameterCriterion
263335 */
264336 public function getCriteria() {
265337 return array_merge( $this->getCriteriaForType(), $this->criteria );
Index: trunk/extensions/Validator/Validator.php
@@ -53,6 +53,7 @@
5454
5555 // Autoload the general classes.
5656 $incDir = dirname( __FILE__ ) . '/includes/';
 57+$wgAutoloadClasses['ListParameter'] = $incDir . 'ListParameter.php';
5758 $wgAutoloadClasses['Parameter'] = $incDir . 'Parameter.php';
5859 $wgAutoloadClasses['ParserHook'] = $incDir . 'ParserHook.php';
5960 $wgAutoloadClasses['Validator'] = $incDir . 'Validator.php';

Status & tagging log