|
1
|
|
|
<?php |
|
2
|
|
|
namespace As3\Modlr\DataTypes; |
|
3
|
|
|
|
|
4
|
|
|
use As3\Modlr\DataTypes\Types\TypeInterface; |
|
5
|
|
|
use As3\Modlr\Exception\InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Responsible for loading attribute data type classes. |
|
9
|
|
|
* Each data type class converts attribute values between Modlr format and PHP. |
|
10
|
|
|
* Built-in type: array, object, boolean, date, float, integer, string. |
|
11
|
|
|
* You can also register custom types. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Jacob Bare <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class TypeFactory |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Available data types. |
|
19
|
|
|
* Mapped by the type name/key to fully-qualified class name. |
|
20
|
|
|
* The class must extend the abstract DataTypes\Types\Type class. |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private $types = [ |
|
25
|
|
|
'array' => 'As3\Modlr\DataTypes\Types\ArrayType', |
|
26
|
|
|
'boolean' => 'As3\Modlr\DataTypes\Types\BooleanType', |
|
27
|
|
|
'date' => 'As3\Modlr\DataTypes\Types\DateType', |
|
28
|
|
|
'float' => 'As3\Modlr\DataTypes\Types\FloatType', |
|
29
|
|
|
'integer' => 'As3\Modlr\DataTypes\Types\IntegerType', |
|
30
|
|
|
'mixed' => 'As3\Modlr\DataTypes\Types\MixedType', |
|
31
|
|
|
'object' => 'As3\Modlr\DataTypes\Types\ObjectType', |
|
32
|
|
|
'string' => 'As3\Modlr\DataTypes\Types\StringType', |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* In-memory loaded type objects. |
|
37
|
|
|
* |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private $loaded = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Converts the value to the internal, Modlr (PHP) value. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $name The data type name. |
|
46
|
|
|
* @param mixed $value The value to convert. |
|
47
|
|
|
* @return mixed |
|
48
|
|
|
*/ |
|
49
|
|
|
public function convertToModlrValue($name, $value) |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->getType($name)->convertToModlrValue($value); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Gets all registered types by name/key. |
|
56
|
|
|
* |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getTypes() |
|
60
|
|
|
{ |
|
61
|
|
|
return array_keys($this->types); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Gets a type object. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $name |
|
68
|
|
|
* @return DataTypes\Types\Type |
|
69
|
|
|
* @throws InvalidArgumentException If the type wasn't found. |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getType($name) |
|
72
|
|
|
{ |
|
73
|
|
|
if (false === $this->hasType($name)) { |
|
74
|
|
|
throw new InvalidArgumentException(sprintf('The type "%s" was not found.', $name)); |
|
75
|
|
|
} |
|
76
|
|
|
if (isset($this->loaded[$name])) { |
|
77
|
|
|
return $this->loaded[$name]; |
|
78
|
|
|
} |
|
79
|
|
|
$fqcn = $this->types[$name]; |
|
80
|
|
|
$type = new $fqcn; |
|
81
|
|
|
if (!$type instanceof TypeInterface) { |
|
82
|
|
|
throw new InvalidArgumentException(sprintf('The class "%s" must implement the "%s\\Types\TypeInterface"', $fqcn, __NAMESPACE__)); |
|
83
|
|
|
} |
|
84
|
|
|
return $this->loaded[$name] = new $fqcn; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Adds a type object. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $name |
|
91
|
|
|
* @param string $fqcn |
|
92
|
|
|
* @return self |
|
93
|
|
|
* @throws InvalidArgumentException If the type already exists. |
|
94
|
|
|
*/ |
|
95
|
|
View Code Duplication |
public function addType($name, $fqcn) |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
if (true === $this->hasType($name)) { |
|
98
|
|
|
throw new InvalidArgumentException(sprintf('The type "%s" already exists.', $name)); |
|
99
|
|
|
} |
|
100
|
|
|
return $this->setType($name, $fqcn); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Overrides a type object with new class. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $name |
|
107
|
|
|
* @param string $fqcn |
|
108
|
|
|
* @return self |
|
109
|
|
|
* @throws InvalidArgumentException If the type was not found. |
|
110
|
|
|
*/ |
|
111
|
|
View Code Duplication |
public function overrideType($name, $fqcn) |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
if (false === $this->hasType($name)) { |
|
114
|
|
|
throw new InvalidArgumentException(sprintf('The type "%s" was not found.', $name)); |
|
115
|
|
|
} |
|
116
|
|
|
return $this->setType($name, $fqcn); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Sets a type. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $name |
|
123
|
|
|
* @param string $fqcn |
|
124
|
|
|
* @return self |
|
125
|
|
|
*/ |
|
126
|
|
|
private function setType($name, $fqcn) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->types[$name] = $fqcn; |
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Determines if a type exists. |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $name |
|
136
|
|
|
* @return bool |
|
137
|
|
|
*/ |
|
138
|
|
|
public function hasType($name) |
|
139
|
|
|
{ |
|
140
|
|
|
return isset($this->types[$name]); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.