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
|
|
|
|