Passed
Branch develop (943532)
by Mariano
02:19
created

ConversionService::convert()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 7
nop 3
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
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
    /**
38
     * @param SingletonConverterContainer $container
39
     */
40
    public function __construct(ConverterContainer $container = null)
41
    {
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
     *
52
     * @throws ConverterDoesNotExistException
53
     */
54
    public function convert($object, $toClass, $iterateParents = false)
55
    {
56
        $from = $this->getTypeOf($object);
57
        $originalType = $from;
58
        do {
59
            try {
60
                $converter = $this->container->getConverter($from, $toClass);
61
62
                return $converter->convert($object);
63
            } catch (ConverterDoesNotExistException $e) {
64
                if (!$iterateParents) {
65
                    throw $e;
66
                } elseif (class_exists($from)) {
67
                    $from = get_parent_class($from);
68
                }
69
            }
70
        } while ($from);
71
        throw new ConverterDoesNotExistException(
72
            "Converter from {$originalType} to {$toClass} does not exist"
73
        );
74
    }
75
76
    /**
77
     * Registers a converter in the library for future use.
78
     *
79
     * @param ConverterBuilder $builder The converter builder that creates the converter to register
80
     */
81
    public function registerConverter(ConverterBuilder $builder)
82
    {
83
        $this->container->addConverter($builder);
84
    }
85
86
    /**
87
     * @param mixed $object
88
     *
89
     * @throws TryingInvalidConversionException
90
     *
91
     * @return string
92
     */
93
    private function getTypeOf($object)
94
    {
95
        $type = gettype($object);
96
        if ($type === 'string' || $type === 'array') {
97
            return $type;
98
        }
99
        if ($type === 'object') {
100
            return get_class($object);
101
        }
102
        throw new TryingInvalidConversionException(
103
            "Trying to convert from '{$type}'. Can only convert from string, array or object"
104
        );
105
    }
106
}
107