|
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\ConverterDoesNotExistException; |
|
22
|
|
|
use Mcustiel\Conversion\Exception\TryingInvalidConversionException; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Service class for PhpSimpleConversion. It's used to access all the service |
|
26
|
|
|
* provided by the library, which basically are register converters and do conversions. |
|
27
|
|
|
* |
|
28
|
|
|
* @author mcustiel |
|
29
|
|
|
*/ |
|
30
|
|
|
class ConversionService |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var ConverterContainer |
|
34
|
|
|
*/ |
|
35
|
|
|
private $container; |
|
36
|
|
|
|
|
37
|
10 |
|
/** |
|
38
|
|
|
* @param SingletonConverterContainer $container |
|
39
|
10 |
|
*/ |
|
40
|
10 |
|
public function __construct(ConverterContainer $container = null) |
|
41
|
10 |
|
{ |
|
42
|
|
|
$this->container = $container ?: SingletonConverterContainer::getInstance(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Converts a given object to another type. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string|array|object $object the object to convert |
|
49
|
|
|
* @param string $toClass The name of the class to which the object will be converted |
|
50
|
|
|
* @param bool $iterateParents Whether or not to search for a converter for the parent class |
|
51
|
9 |
|
* |
|
52
|
|
|
* @throws ConverterDoesNotExistException |
|
53
|
9 |
|
*/ |
|
54
|
8 |
|
public function convert($object, $toClass, $iterateParents = false) |
|
55
|
|
|
{ |
|
56
|
|
|
$from = $this->getTypeOf($object); |
|
57
|
8 |
|
$originalType = $from; |
|
58
|
6 |
|
do { |
|
59
|
3 |
|
try { |
|
60
|
3 |
|
$converter = $this->container->getConverter($from, $toClass); |
|
61
|
1 |
|
|
|
62
|
2 |
|
return $converter->convert($object); |
|
63
|
2 |
|
} catch (ConverterDoesNotExistException $e) { |
|
64
|
|
|
$from = $this->manageException($iterateParents, $from, $e); |
|
65
|
|
|
} |
|
66
|
2 |
|
} while ($from); |
|
67
|
1 |
|
|
|
68
|
1 |
|
throw new ConverterDoesNotExistException( |
|
69
|
|
|
"Converter from {$originalType} to {$toClass} does not exist" |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Registers a converter in the library for future use. |
|
75
|
|
|
* |
|
76
|
|
|
* @param ConverterBuilder $builder The converter builder that creates the converter to register |
|
77
|
2 |
|
*/ |
|
78
|
|
|
public function registerConverter(ConverterBuilder $builder) |
|
79
|
2 |
|
{ |
|
80
|
2 |
|
$this->container->addConverter($builder); |
|
81
|
|
|
} |
|
82
|
9 |
|
|
|
83
|
|
|
/** |
|
84
|
9 |
|
* @param bool $iterateParents |
|
85
|
|
|
* @param string $from |
|
86
|
9 |
|
* @param \Exception $e |
|
87
|
|
|
*/ |
|
88
|
8 |
|
private function manageException($iterateParents, $from, $e) |
|
89
|
2 |
|
{ |
|
90
|
7 |
|
if (!$iterateParents) { |
|
91
|
6 |
|
throw $e; |
|
92
|
|
|
} elseif (class_exists($from)) { |
|
93
|
1 |
|
$from = get_parent_class($from); |
|
94
|
1 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $from; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param mixed $object |
|
101
|
|
|
* |
|
102
|
|
|
* @throws TryingInvalidConversionException |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
private function getTypeOf($object) |
|
107
|
|
|
{ |
|
108
|
|
|
$type = gettype($object); |
|
109
|
|
|
if ($type === 'string' || $type === 'array') { |
|
110
|
|
|
return $type; |
|
111
|
|
|
} |
|
112
|
|
|
if ($type === 'object') { |
|
113
|
|
|
return get_class($object); |
|
114
|
|
|
} |
|
115
|
|
|
throw new TryingInvalidConversionException( |
|
116
|
|
|
"Trying to convert from '{$type}'. Can only convert from string, array or object" |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|