Completed
Pull Request — 1.x (#1)
by Dorian
01:53
created

ObjectParser   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 48
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getClassName() 0 1 1
A setClassName() 0 1 1
A getHydrator() 0 1 1
A setHydrator() 0 1 1
A __construct() 0 6 1
A parse() 0 15 4
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
    public function getClassName() { return $this->className;}
23
    public function setClassName(string $className) { $this->className = $className; }
24
25
    /** @var Hydrator */
26
    private $hydrator;
27
    public function getHydrator() { return $this->hydrator; }
28
    public function setHydrator(Hydrator $hydrator) { $this->hydrator = $hydrator; }
29
30
    /**
31
     * ObjectParser constructor.
32
     * @param string $className
33
     * @param Hydrator $hydrator
34
     * @param string $errorMessage
35
     */
36
    public function __construct(string $className, Hydrator $hydrator, string $errorMessage = "")
37
    {
38
        parent::__construct($errorMessage);
39
        $this->className = $className;
40
        $this->hydrator = $hydrator;
41
    }
42
43
44
    /**
45
     * @param $rawValue
46
     * @return mixed
47
     *
48
     * @throws ParsingException
49
     */
50
    public function parse($rawValue)
51
    {
52
        if ($rawValue === null) {
53
            return null;
54
        }
55
        if (!is_array($rawValue)) {
56
            $this->throw();
57
        }
58
59
        try {
60
            return $this->hydrator->hydrateNewObject($rawValue, $this->className);
61
        } catch (HydratingException $exception) {
62
            throw new ParsingException($exception->getErrorsMap());
63
        }
64
    }
65
}
66