Completed
Pull Request — 1.x (#1)
by Dorian
02:37
created

ObjectParser::parse()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 5
nop 1
1
<?php
2
namespace MetaHydrator\Parser;
3
4
use MetaHydrator\Exception\HydratingException;
5
use MetaHydrator\Exception\ParsingException;
6
use MetaHydrator\Handler\HydratingHandlerInterface;
7
use MetaHydrator\MetaHydrator;
8
use MetaHydrator\Validator\ValidatorInterface;
9
use Mouf\Hydrator\Hydrator;
10
use Mouf\Hydrator\TdbmHydrator;
11
12
/**
13
 * A custom class parser based on the MetaHydrator behaviour
14
 *
15
 * Class ObjectParser
16
 * @package MetaHydrator\Parser
17
 */
18
class ObjectParser extends AbstractParser implements ParserInterface
19
{
20
    /** @var string */
21
    private $className;
22
    /** @var Hydrator */
23
    private $hydrator;
24
25
    /**
26
     * ObjectParser constructor.
27
     * @param string $className
28
     * @param Hydrator $hydrator
29
     * @param string $errorMessage
30
     */
31
    public function __construct(string $className, Hydrator $hydrator, string $errorMessage = "")
32
    {
33
        parent::__construct($errorMessage);
34
        $this->className = $className;
35
        $this->hydrator = $hydrator;
36
    }
37
38
39
    /**
40
     * @param $rawValue
41
     * @return mixed
42
     *
43
     * @throws ParsingException
44
     */
45
    public function parse($rawValue)
46
    {
47
        if ($rawValue === null) {
48
            return null;
49
        }
50
        if (!is_array($rawValue)) {
51
            $this->throw();
52
        }
53
54
        try {
55
            return $this->hydrator->hydrateNewObject($rawValue, $this->className);
56
        } catch (HydratingException $exception) {
57
            throw new ParsingException($exception->getErrorsMap());
58
        }
59
    }
60
}
61