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