1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DataTypes; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use OutOfBoundsException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @since 0.1 |
10
|
|
|
* |
11
|
|
|
* @license GPL-2.0+ |
12
|
|
|
* @author Daniel Kinzler |
13
|
|
|
*/ |
14
|
|
|
class DataTypeFactory { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Associative array mapping data type identifiers to DataType objects. |
18
|
|
|
* |
19
|
|
|
* @var DataType[] |
20
|
|
|
*/ |
21
|
|
|
private $types = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string[] Associative array mapping data type identifiers to data value type identifiers. |
25
|
|
|
*/ |
26
|
|
|
private $valueTypes = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @since 0.5 |
30
|
|
|
* |
31
|
|
|
* @param string[] $valueTypes Associative array mapping data type identifiers (also |
32
|
|
|
* referred to as "property types") to data value type identifiers. |
33
|
|
|
* |
34
|
|
|
* @throws InvalidArgumentException |
35
|
|
|
*/ |
36
|
|
|
public function __construct( array $valueTypes ) { |
37
|
|
|
foreach ( $valueTypes as $typeId => $valueType ) { |
38
|
|
|
if ( !is_string( $typeId ) || $typeId === '' |
39
|
|
|
|| !is_string( $valueType ) || $valueType === '' |
40
|
|
|
) { |
41
|
|
|
throw new InvalidArgumentException( |
42
|
|
|
'$valueTypes must be an associative array of non-empty strings' |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->valueTypes = $valueTypes; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @since 0.1 |
52
|
|
|
* |
53
|
|
|
* @param DataType[] $dataTypes |
54
|
|
|
* |
55
|
|
|
* @return self |
56
|
|
|
*/ |
57
|
|
|
public static function newFromTypes( array $dataTypes ) { |
58
|
|
|
$factory = new self( [] ); |
59
|
|
|
|
60
|
|
|
foreach ( $dataTypes as $dataType ) { |
61
|
|
|
$factory->registerDataType( $dataType ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $factory; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @since 0.1 |
69
|
|
|
* |
70
|
|
|
* @param DataType $dataType |
71
|
|
|
*/ |
72
|
|
|
public function registerDataType( DataType $dataType ) { |
73
|
|
|
$this->types[$dataType->getId()] = $dataType; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns the list of registered data type identifiers (also referred to as "property types"). |
78
|
|
|
* |
79
|
|
|
* @since 0.1 |
80
|
|
|
* |
81
|
|
|
* @return string[] |
82
|
|
|
*/ |
83
|
|
|
public function getTypeIds() { |
84
|
|
|
return array_keys( $this->valueTypes ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns the data type that has the specified type identifier. |
89
|
|
|
* Types may be instantiated on the fly using a type builder spec. |
90
|
|
|
* |
91
|
|
|
* @since 0.1 |
92
|
|
|
* |
93
|
|
|
* @param string $typeId Data type identifier (also referred to as "property type"). |
94
|
|
|
* |
95
|
|
|
* @throws OutOfBoundsException if the requested type is not known. |
96
|
|
|
* @return DataType |
97
|
|
|
*/ |
98
|
|
|
public function getType( $typeId ) { |
99
|
|
|
if ( !array_key_exists( $typeId, $this->types ) ) { |
100
|
|
|
if ( !array_key_exists( $typeId, $this->valueTypes ) ) { |
101
|
|
|
throw new OutOfBoundsException( "Unknown data type '$typeId'" ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$valueType = $this->valueTypes[$typeId]; |
105
|
|
|
$this->types[$typeId] = new DataType( $typeId, $valueType ); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->types[$typeId]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns all data types in an associative array with |
113
|
|
|
* the keys being data type identifiers (also referred to as "property types") pointing to their |
114
|
|
|
* corresponding data type. |
115
|
|
|
* |
116
|
|
|
* @since 0.1 |
117
|
|
|
* |
118
|
|
|
* @return DataType[] |
119
|
|
|
*/ |
120
|
|
|
public function getTypes() { |
121
|
|
|
$types = []; |
122
|
|
|
|
123
|
|
|
foreach ( $this->getTypeIds() as $typeId ) { |
124
|
|
|
$types[$typeId] = $this->getType( $typeId ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $types; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|