Navigator::navigate()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.9
cc 3
nc 4
nop 3
crap 3
1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Serializer\Navigator;
13
14
use Ivory\Serializer\Context\ContextInterface;
15
use Ivory\Serializer\Mapping\TypeMetadataInterface;
16
use Ivory\Serializer\Registry\TypeRegistry;
17
use Ivory\Serializer\Registry\TypeRegistryInterface;
18
use Ivory\Serializer\Type\Guesser\TypeGuesser;
19
use Ivory\Serializer\Type\Guesser\TypeGuesserInterface;
20
use Ivory\Serializer\Type\Type;
21
22
/**
23
 * @author GeLo <[email protected]>
24
 */
25
class Navigator implements NavigatorInterface
26
{
27
    /**
28
     * @var TypeRegistryInterface
29
     */
30
    private $typeRegistry;
31
32
    /**
33
     * @var TypeGuesserInterface
34
     */
35
    private $typeGuesser;
36
37
    /**
38
     * @param TypeRegistryInterface|null $typeRegistry
39
     * @param TypeGuesserInterface|null  $typeGuesser
40
     */
41 1492
    public function __construct(TypeRegistryInterface $typeRegistry = null, TypeGuesserInterface $typeGuesser = null)
42
    {
43 1492
        $this->typeRegistry = $typeRegistry ?: TypeRegistry::create();
44 1492
        $this->typeGuesser = $typeGuesser ?: new TypeGuesser();
45 1492
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 1472
    public function navigate($data, ContextInterface $context, TypeMetadataInterface $type = null)
51
    {
52 1472
        $type = $type ?: $this->typeGuesser->guess($data);
53 1472
        $name = $type->getName();
54
55 1472
        if ($data === null) {
56 188
            $name = Type::NULL;
57
        }
58
59 1472
        return $this->typeRegistry->getType($name, $context->getDirection())->convert($data, $type, $context);
60
    }
61
}
62