1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of php-simple-conversion. |
4
|
|
|
* |
5
|
|
|
* php-simple-conversion is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* php-simple-conversion is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with php-simple-conversion. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Mcustiel\Conversion; |
20
|
|
|
|
21
|
|
|
use Mcustiel\Conversion\Exception\ObjectIsNotConverterException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Builder used to register Converters into the library. It implments a fluent interface to |
25
|
|
|
* define the needed values to register the converter. |
26
|
|
|
* |
27
|
|
|
* @author mcustiel |
28
|
|
|
*/ |
29
|
|
|
class ConverterBuilder |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $from; |
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $to; |
39
|
|
|
/** |
40
|
|
|
* @var callable |
41
|
|
|
*/ |
42
|
|
|
private $converter; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* This class can't be instantiated. |
46
|
|
|
*/ |
47
|
14 |
|
private function __construct() |
48
|
|
|
{ |
49
|
14 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Creates a ConverterBuilder instance. |
53
|
|
|
* |
54
|
|
|
* @return \Mcustiel\Conversion\ConverterBuilder |
55
|
|
|
*/ |
56
|
14 |
|
public static function get() |
57
|
|
|
{ |
58
|
14 |
|
return new self(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Specifies from which type the converter will convert. |
63
|
|
|
* |
64
|
|
|
* @param string $from A string specifying the 'from' type. It must be 'string', 'array' or |
65
|
|
|
* the full name of a class. |
66
|
|
|
* |
67
|
|
|
* @throws \InvalidArgumentException When given parameter is not a string |
68
|
|
|
* |
69
|
|
|
* @return \Mcustiel\Conversion\ConverterBuilder |
70
|
|
|
*/ |
71
|
12 |
|
public function from($from) |
72
|
|
|
{ |
73
|
12 |
|
if (!is_string($from)) { |
74
|
1 |
|
throw new \InvalidArgumentException( |
75
|
|
|
"'From' parameter should be a string containing 'string', 'array', or a class name" |
76
|
1 |
|
); |
77
|
|
|
} |
78
|
11 |
|
$this->from = $from; |
79
|
|
|
|
80
|
11 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Specifies to which type the converter will convert to. |
85
|
|
|
* |
86
|
|
|
* @param string $to a string specifying the destination type of the conversion |
87
|
|
|
* |
88
|
|
|
* @throws \InvalidArgumentException When given parameter is not a string |
89
|
|
|
* |
90
|
|
|
* @return \Mcustiel\Conversion\ConverterBuilder |
91
|
|
|
*/ |
92
|
12 |
|
public function to($to) |
93
|
|
|
{ |
94
|
12 |
|
if (!is_string($to)) { |
95
|
1 |
|
throw new \InvalidArgumentException( |
96
|
|
|
"'To' parameter should be a string containing a type name" |
97
|
1 |
|
); |
98
|
|
|
} |
99
|
11 |
|
$this->to = $to; |
100
|
|
|
|
101
|
11 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns the value of 'to'. |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
9 |
|
public function getTo() |
110
|
|
|
{ |
111
|
9 |
|
return $this->to; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns the value of 'from'. |
116
|
|
|
*/ |
117
|
10 |
|
public function getFrom() |
118
|
|
|
{ |
119
|
10 |
|
return $this->from; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Specifies the name of the class that implements the Converter for the given to and from parameters. |
124
|
|
|
* |
125
|
|
|
* @param string $class The name of the converter class |
126
|
|
|
* |
127
|
|
|
* @return \Mcustiel\Conversion\ConverterBuilder |
128
|
|
|
*/ |
129
|
|
|
public function withImplementation($class) |
130
|
|
|
{ |
131
|
8 |
|
$this->converter = function () use ($class) { |
132
|
8 |
|
$object = $this->getObjectFromClass($class); |
133
|
|
|
|
134
|
8 |
|
if (!is_subclass_of($object, Converter::class)) { |
135
|
3 |
|
throw new ObjectIsNotConverterException( |
136
|
3 |
|
'Object of type ' . get_class($object) . ' does not implement ' . Converter::class |
137
|
3 |
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
5 |
|
return $object; |
141
|
|
|
}; |
142
|
|
|
|
143
|
12 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Returns an instance of the converter. |
148
|
|
|
* |
149
|
|
|
* @return mixed the return value of the convert method in the converter implementation |
150
|
|
|
*/ |
151
|
8 |
|
public function getConverter() |
152
|
|
|
{ |
153
|
8 |
|
return call_user_func($this->converter); |
154
|
|
|
} |
155
|
|
|
|
156
|
8 |
|
private function getObjectFromClass($class) |
157
|
|
|
{ |
158
|
8 |
|
if (is_object($class)) { |
159
|
2 |
|
return $class; |
160
|
|
|
} |
161
|
|
|
|
162
|
6 |
|
return new $class(); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|