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 MetaHydrator implements ParserInterface |
19
|
|
|
{ |
20
|
|
|
/** @var string */ |
21
|
|
|
private $className; |
22
|
|
|
|
23
|
|
|
/** @var mixed */ |
24
|
|
|
private $errorMessage; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* ObjectParser constructor. |
28
|
|
|
* @param string $className |
29
|
|
|
* @param HydratingHandlerInterface[] $handlers |
30
|
|
|
* @param ValidatorInterface[] $validators |
31
|
|
|
* @param mixed $errorMessage |
32
|
|
|
* @param Hydrator $simpleHydrator |
33
|
|
|
*/ |
34
|
|
|
public function __construct(string $className, array $handlers = [], array $validators = [], string $errorMessage = "", Hydrator $simpleHydrator = null) |
35
|
|
|
{ |
36
|
|
|
parent::__construct($handlers, $validators, $simpleHydrator); |
37
|
|
|
$this->className = $className; |
38
|
|
|
$this->errorMessage = $errorMessage; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param $rawValue |
44
|
|
|
* @return mixed |
45
|
|
|
* |
46
|
|
|
* @throws ParsingException |
47
|
|
|
*/ |
48
|
|
|
public function parse($rawValue) |
49
|
|
|
{ |
50
|
|
|
if ($rawValue === null) { |
51
|
|
|
return null; |
52
|
|
|
} |
53
|
|
|
if (!is_array($rawValue)) { |
54
|
|
|
throw new ParsingException($this->errorMessage); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
try { |
58
|
|
|
return $this->hydrateNewObject($rawValue, $this->className); |
59
|
|
|
} catch (HydratingException $exception) { |
60
|
|
|
throw new ParsingException($exception->getErrorsMap()); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|