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
|
2 |
|
public function __construct( array $typeSpecs = [] ) { |
41
|
2 |
|
foreach ( $typeSpecs as $typeName => $typeSpec ) { |
42
|
|
|
$this->addType( $typeName, $typeSpec ); |
43
|
|
|
} |
44
|
2 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @since 1.8 |
48
|
|
|
*/ |
49
|
2 |
|
public function addType( string $typeName, array $typeSpec ) { |
50
|
2 |
|
$this->types[$typeName] = ParamType::newFromArray( $typeName, $typeSpec ); |
51
|
2 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Package private |
55
|
|
|
*/ |
56
|
36 |
|
public function hasType( string $typeName ): bool { |
57
|
36 |
|
return array_key_exists( $typeName, $this->types ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Package private |
62
|
|
|
*/ |
63
|
66 |
|
public function getType( string $typeName ): ParamType { |
64
|
66 |
|
return $this->types[$typeName]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @since 1.8 |
69
|
|
|
*/ |
70
|
|
|
public static function newCoreTypes(): self { |
71
|
|
|
return new self( self::getCoreTypes() ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @since 1.4 |
76
|
|
|
*/ |
77
|
20 |
|
public static function getCoreTypes(): array { |
78
|
|
|
return [ |
79
|
|
|
self::BOOLEAN => [ |
80
|
|
|
'string-parser' => BoolParser::class, |
81
|
|
|
'validation-callback' => 'is_bool', |
82
|
|
|
], |
83
|
|
|
self::FLOAT => [ |
84
|
|
|
'string-parser' => FloatParser::class, |
85
|
|
|
'validation-callback' => function( $value ) { |
86
|
20 |
|
return is_float( $value ) || is_int( $value ); |
87
|
|
|
}, |
88
|
|
|
'validator' => RangeValidator::class, |
89
|
|
|
], |
90
|
|
|
self::INTEGER => [ |
91
|
|
|
'string-parser' => IntParser::class, |
92
|
|
|
'validation-callback' => 'is_int', |
93
|
|
|
'validator' => RangeValidator::class, |
94
|
|
|
], |
95
|
|
|
self::STRING => [ |
96
|
|
|
'validator' => StringValidator::class, |
97
|
|
|
'definition' => StringParam::class, |
98
|
|
|
], |
99
|
|
|
self::DIMENSION => [ |
100
|
|
|
'definition' => DimensionParam::class, |
101
|
|
|
'validator' => DimensionValidator::class, |
102
|
|
|
], |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|