Completed
Push — master ( ef46bd...45441d )
by Jeroen De
02:58
created

ParameterTypes   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 86
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addType() 0 3 1
A hasType() 0 3 1
A getType() 0 3 1
A __construct() 0 5 2
A newCoreTypes() 0 3 1
A getCoreTypes() 0 28 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace ParamProcessor;
6
7
use ParamProcessor\Definition\DimensionParam;
8
use ParamProcessor\Definition\StringParam;
9
use ParamProcessor\PackagePrivate\ParamType;
10
use ValueParsers\BoolParser;
11
use ValueParsers\FloatParser;
12
use ValueParsers\IntParser;
13
use ValueValidators\DimensionValidator;
14
use ValueValidators\RangeValidator;
15
use ValueValidators\StringValidator;
16
17
/**
18
 * @licence GNU GPL v2+
19
 * @author Jeroen De Dauw < [email protected] >
20
 */
21
class ParameterTypes {
22
23
	/**
24
	 * @since 1.7
25
	 */
26
	public const BOOLEAN = 'boolean';
27
	public const FLOAT = 'float';
28
	public const INTEGER = 'integer';
29
	public const STRING = 'string';
30
	public const DIMENSION = 'dimension';
31
32
	/**
33
	 * @var ParamType[]
34
	 */
35
	private $types = [];
36
37
	/**
38
	 * @param array[] $typeSpecs
39
	 */
40 16
	public function __construct( array $typeSpecs = [] ) {
41 16
		foreach ( $typeSpecs as $typeName => $typeSpec ) {
42 14
			$this->addType( $typeName, $typeSpec );
43
		}
44 16
	}
45
46
	/**
47
	 * @since 1.8
48
	 */
49 16
	public function addType( string $typeName, array $typeSpec ) {
50 16
		$this->types[$typeName] = ParamType::newFromArray( $typeName, $typeSpec );
51 16
	}
52
53
	/**
54
	 * Package private
55
	 */
56 50
	public function hasType( string $typeName ): bool {
57 50
		return array_key_exists( $typeName, $this->types );
58
	}
59
60
	/**
61
	 * Package private
62
	 */
63 80
	public function getType( string $typeName ): ParamType {
64 80
		return $this->types[$typeName];
65
	}
66
67
	/**
68
	 * @since 1.8
69
	 */
70 14
	public static function newCoreTypes(): self {
71 14
		return new self( self::getCoreTypes() );
72
	}
73
74
	/**
75
	 * @since 1.4
76
	 */
77 20
	public static function getCoreTypes(): array {
78
		return [
79 14
			self::BOOLEAN => [
80 14
				'string-parser' => BoolParser::class,
81
				'validation-callback' => 'is_bool',
82
			],
83 14
			self::FLOAT => [
84
				'string-parser' => FloatParser::class,
85
				'validation-callback' => function( $value ) {
86 20
					return is_float( $value ) || is_int( $value );
87 14
				},
88
				'validator' => RangeValidator::class,
89
			],
90 14
			self::INTEGER => [
91
				'string-parser' => IntParser::class,
92
				'validation-callback' => 'is_int',
93
				'validator' => RangeValidator::class,
94
			],
95 14
			self::STRING => [
96
				'validator' => StringValidator::class,
97
				'definition' => StringParam::class,
98
			],
99 14
			self::DIMENSION => [
100
				'definition' => DimensionParam::class,
101
				'validator' => DimensionValidator::class,
102
			],
103
		];
104
	}
105
106
}
107