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