summaryrefslogtreecommitdiff
path: root/www/wiki/vendor/param-processor/param-processor/src/Definition/DimensionParam.php
blob: 1bf272ccf71a2d2924ea97df47dcf884af7b2db6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php

namespace ParamProcessor\Definition;

use Exception;
use ParamProcessor\ParamDefinition;
use ParamProcessor\IParam;
use ParamProcessor\IParamDefinition;

/**
 * Defines the dimension parameter type.
 * This parameter describes the size of a dimension (ie width) in some unit (ie px) or a percentage.
 * Specifies the type specific validation and formatting logic.
 *
 * TODO: this class is silly, should be handled by a dedicated formatting object/function.
 *
 * @since 1.0
 *
 * @licence GNU GPL v2+
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class DimensionParam extends ParamDefinition {

	/**
	 * Formats the parameter value to it's final result.
	 * @see ParamDefinition::formatValue
	 *
	 * @since 1.0
	 *
	 * @param mixed $value
	 * @param IParam $param
	 * @param IParamDefinition[] $definitions
	 * @param IParam[] $params
	 *
	 * @return mixed
	 * @throws Exception
	 */
	protected function formatValue( $value, IParam $param, array &$definitions, array $params ) {
		if ( $value === 'auto' ) {
			return $value;
		}

		/**
		 * @var \ValueValidators\DimensionValidator $validator
		 */
		$validator = $this->getValueValidator();

		if ( get_class( $validator ) === 'ValueValidators\DimensionValidator' ) {
			foreach ( $validator->getAllowedUnits() as $unit ) {
				if ( $unit !== '' && strpos( $value, $unit ) !== false ) {
					return $value;
				}
			}

			return $value . $validator->getDefaultUnit();
		}
		else {
			throw new Exception(
				'ValueValidator of a DimensionParam should be a ValueValidators\DimensionValidator and not a '
					. get_class( $validator )
			);
		}
	}

}