Permalink
Browse files

Added DimensionParam

Change-Id: I1321b543011861f72385f976937eb86cfa08c7d6
  • Loading branch information...
JeroenDeDauw committed Aug 14, 2012
1 parent f32b73b commit 9717714d48055e492268f811b6e7b6a1a2a7d881
View
@@ -14,6 +14,7 @@ End of 2012.
* Drop PHP 5.2 compatibility
** Kill ParamDefinition::get_called_class
** Kill boilerplate in ParserHook
+** __DIR__
== Validator 0.7 ==
@@ -28,4 +29,6 @@ Unknown, below list is stuff that should be completed at some point before this
* Fully implement error handling system, including inline settings and inline reporting
* Good test coverage
* Have the core library work as stand alone (ie isolate and split off MW specific code)
-* List validation and formatting
+* List validation and formatting
+* Rename to ParamProcessor
+* Use of namespaces where applicable
View
@@ -26,6 +26,7 @@ public static function registerUnitTests( array &$files ) {
$testFiles = array(
'definitions/BoolParam',
'definitions/CharParam',
+ 'definitions/DimensionParam',
'definitions/FloatParam',
'definitions/IntParam',
'definitions/StringParam',
View
@@ -71,6 +71,7 @@
// includes/definitions
$wgAutoloadClasses['BoolParam'] = $incDir . 'definitions/BoolParam.php';
$wgAutoloadClasses['CharParam'] = $incDir . 'definitions/CharParam.php';
+$wgAutoloadClasses['DimensionParam'] = $incDir . 'definitions/DimensionParam.php';
$wgAutoloadClasses['FloatParam'] = $incDir . 'definitions/FloatParam.php';
$wgAutoloadClasses['IntParam'] = $incDir . 'definitions/IntParam.php';
$wgAutoloadClasses['NumericParam'] = $incDir . 'definitions/NumericParam.php';
@@ -106,6 +107,7 @@
'integer' => 'IntParam', // Parameter::TYPE_INTEGER
'string' => 'StringParam', // Parameter::TYPE_STRING
'title' => 'TitleParam', // Parameter::TYPE_TITLE
+ 'dimension' => 'DimensionParam',
);
@@ -29,7 +29,42 @@
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
-class DimensionParam extends ParamDefinition {
+class DimensionParam extends NumericParam {
+
+ /**
+ * @since 0.5
+ *
+ * @var boolean
+ */
+ protected $allowAuto = false;
+
+ /**
+ * @since 0.5
+ *
+ * @var array
+ */
+ protected $allowedUnits = array( 'px', '' );
+
+ /**
+ * @since 0.5
+ *
+ * @var integer
+ */
+ protected $minPercentage = 0;
+
+ /**
+ * @since 0.5
+ *
+ * @var integer
+ */
+ protected $maxPercentage = 100;
+
+ /**
+ * @since 0.5
+ *
+ * @var string
+ */
+ protected $defaultUnit = 'px';
/**
* Returns an identifier for the parameter type.
@@ -55,25 +90,30 @@ public function getType() {
* @return boolean
*/
protected function validateValue( $value, IParam $param, array $definitions, array $params, ValidatorOptions $options ) {
- if ( !parent::validateValue( $value, $param, $definitions, $params, $options ) ) {
+ if ( !$this->valueIsAllowed( $value ) ) {
return false;
}
- if ( !$this->canBeEmpty && $value === '' ) {
- return false;
+ if ( $value === 'auto' ) {
+ return $this->allowAuto;
}
- if ( $this->length !== false ) {
- $length = strlen( $value );
+ if ( !preg_match( '/^\d+(\.\d+)?(' . implode( '|', $this->allowedUnits ) . ')$/', $value ) ) {
+ return false;
+ }
- if ( is_array( $this->length ) ) {
- return ( $this->length[1] === false || $value <= $this->length[1] )
- && ( $this->length[0] === false || $value >= $this->length[0] );
- }
- else {
- return $length == $this->length;
- }
+ if ( in_string( '%', $value ) ) {
+ $upperBound = $this->maxPercentage;
+ $lowerBound = $this->minPercentage;
}
+ else {
+ $upperBound = null;
+ $lowerBound = null;
+ }
+
+ $value = (float)preg_replace( '/[^0-9]/', '', $value );
+
+ return $this->validateBounds( $value, $upperBound, $lowerBound );
}
/**
@@ -90,13 +130,17 @@ protected function validateValue( $value, IParam $param, array $definitions, arr
* @return mixed
*/
protected function formatValue( $value, IParam $param, array &$definitions, array $params ) {
- $value = (string)$value;
+ if ( $value === 'auto' ) {
+ return $value;
+ }
- if ( $this->toLower ) {
- $value = strtolower( $value );
+ foreach ( $this->allowedUnits as $unit ) {
+ if ( $unit !== '' && in_string( $unit, $value ) ) {
+ return $value;
+ }
}
- return $value;
+ return $value . $this->defaultUnit;
}
/**
@@ -110,6 +154,80 @@ protected function formatValue( $value, IParam $param, array &$definitions, arra
public function setArrayValues( array $param ) {
parent::setArrayValues( $param );
+ if ( array_key_exists( 'allowauto', $param ) ) {
+ $this->setAllowAuto( $param['allowauto'] );
+ }
+
+ if ( array_key_exists( 'maxpercentage', $param ) ) {
+ $this->setMaxPercentage( $param['maxpercentage'] );
+ }
+
+ if ( array_key_exists( 'minpercentage', $param ) ) {
+ $this->setMinPercentage( $param['minpercentage'] );
+ }
+
+ if ( array_key_exists( 'units', $param ) ) {
+ $this->setAllowedUnits( $param['units'] );
+ }
+
+ if ( array_key_exists( 'defaultunit', $param ) ) {
+ $this->setDefaultUnit( $param['defaultunit'] );
+ }
+ }
+
+ /**
+ * If 'auto' should be seen as a valid value.
+ *
+ * @since 0.5
+ *
+ * @param boolean $allowAuto
+ */
+ public function setAllowAuto( $allowAuto ) {
+ $this->allowAuto = $allowAuto;
+ }
+
+ /**
+ * Set the upper bound for the value in case it's a percentage.
+ *
+ * @since 0.5
+ *
+ * @param integer $maxPercentage
+ */
+ public function setMaxPercentage( $maxPercentage ) {
+ $this->maxPercentage = $maxPercentage;
+ }
+
+ /**
+ * Set the lower bound for the value in case it's a percentage.
+ *
+ * @since 0.5
+ *
+ * @param integer $minPercentage
+ */
+ public function setMinPercentage( $minPercentage ) {
+ $this->minPercentage = $minPercentage;
+ }
+
+ /**
+ * Sets the default unit, ie the one that will be assumed when the empty unit is provided.
+ *
+ * @since 0.5
+ *
+ * @param string $defaultUnit
+ */
+ public function setDefaultUnit( $defaultUnit ) {
+ $this->defaultUnit = $defaultUnit;
+ }
+
+ /**
+ * If percentage values should be accepted.
+ *
+ * @since 0.5
+ *
+ * @param array $units
+ */
+ public function setAllowedUnits( array $units = array( 'px', 'em', 'ex', '%', '' ) ) {
+ $this->allowedUnits = $units;
}
}
@@ -86,8 +86,26 @@ protected function validateValue( $value, IParam $param, array $definitions, arr
return false;
}
- return ( $this->upperBound === false || $value <= $this->upperBound )
- && ( $this->lowerBound === false || $value >= $this->lowerBound );
+ return $this->validateBounds( $value );
+ }
+
+ /**
+ * Validates the parameters value and returns the result.
+ *
+ * @since 0.5
+ *
+ * @param $value mixed
+ * @param float|null|false $upperBound
+ * @param float|null|false $lowerBound
+ *
+ * @return boolean
+ */
+ protected function validateBounds( $value, $upperBound = null, $lowerBound = null ) {
+ $upperBound = is_null( $upperBound ) ? $this->upperBound : $upperBound;
+ $lowerBound = is_null( $lowerBound ) ? $this->lowerBound : $lowerBound;
+
+ return ( $upperBound === false || $value <= $upperBound )
+ && ( $lowerBound === false || $value >= $lowerBound );
}
/**
@@ -780,6 +780,20 @@ protected function formatValue( $value, IParam $param, array &$definitions, arra
* @return boolean
*/
protected function validateValue( $value, IParam $param, array $definitions, array $params, ValidatorOptions $options ) {
+ return $this->valueIsAllowed( $value );
+ }
+
+ /**
+ * Returns if the value is in the allowed values list in case this list is set,
+ * and if it's not in the prohibited values list in case that one is set.
+ *
+ * @since 0.5
+ *
+ * @param mixed $value
+ *
+ * @return boolean
+ */
+ protected function valueIsAllowed( $value ) {
if ( $this->allowedValues !== false && !in_array( $value, $this->allowedValues ) ) {
return false;
}
@@ -41,6 +41,22 @@ class DimensionParamTest extends ParamDefinitionTest {
public function getDefinitions() {
$params = parent::getDefinitions();
+ $params['auto'] = array(
+ 'allowauto' => true,
+ );
+
+ $params['allunits'] = array(
+ 'units' => array( 'px', 'ex', 'em', '%', '' ),
+ );
+
+ $params['bounds'] = array(
+ 'lowerbound' => 42,
+ 'upperbound' => 9000,
+ 'maxpercentage' => 34,
+ 'minpercentage' => 23,
+ 'units' => array( 'px', 'ex', '%', '' ),
+ );
+
return $params;
}
@@ -52,21 +68,57 @@ public function getDefinitions() {
* @return array
*/
public function valueProvider( $stringlyTyped = true ) {
- return array(
+ $values = array(
'empty' => array(
- array( 'a', true, 'a' ),
- array( '0', true, '0' ),
- array( 'abc', false ),
- array( 42, false ),
- array( 4, false ),
+ array( '100px', true, '100px' ),
+ array( '100', true, '100px' ),
+ array( 42, true, '42px' ),
+ array( 42.5, true, '42.5px' ),
+ array( 'over9000', false ),
+ array( 'yes', false ),
+ array( 'auto', false ),
+ array( '100%', false ),
),
'values' => array(
- array( '1', true, '1' ),
+ array( 1, true, '1px' ),
+ array( 2, false ),
array( 'yes', false ),
array( 'no', false ),
- array( '0.1', false ),
+ ),
+ 'auto' => array(
+ array( 'auto', true, 'auto' ),
+ ),
+ 'allunits' => array(
+ array( '100%', true, '100%' ),
+ array( '100em', true, '100em' ),
+ array( '100ex', true, '100ex' ),
+ array( '101%', false ),
+ ),
+ 'bounds' => array(
+ array( '30%', true, '30%' ),
+ array( '20%', false ),
+ array( '40%', false ),
+ array( '100px', true, '100px' ),
+ array( '100ex', true, '100ex' ),
+ array( '10px', false ),
+ array( '9001ex', false ),
),
);
+
+ if ( $stringlyTyped ) {
+ foreach ( $values as &$set ) {
+ foreach ( $set as &$value ) {
+ if ( is_int( $value[0] ) || is_float( $value[0] ) ) {
+ $value[0] = (string)$value[0];
+ }
+ }
+ }
+
+ $values['empty'][] = array( 42, false );
+ $values['empty'][] = array( 42.5, false );
+ }
+
+ return $values;
}
/**

0 comments on commit 9717714

Please sign in to comment.