1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\PhpApi\Descriptions\Hydrator package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\PhpApi\Descriptions\Hydrator\Processors\Object; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Schema\ObjectSchema; |
12
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Schema\ScalarSchema; |
13
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema; |
14
|
|
|
use KleijnWeb\PhpApi\Descriptions\Hydrator\Processors\Processor; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author John Kleijn <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
abstract class ObjectProcessor extends Processor |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var \KleijnWeb\PhpApi\Descriptions\Hydrator\Processors\Processor[] |
23
|
|
|
*/ |
24
|
|
|
protected $propertyProcessors; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $defaults = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* ObjectHydrator constructor. |
33
|
|
|
* @param ObjectSchema $schema |
34
|
|
|
*/ |
35
|
|
|
public function __construct(ObjectSchema $schema) |
36
|
|
|
{ |
37
|
|
|
parent::__construct($schema); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string $name |
41
|
|
|
* @var Schema $propertySchema |
42
|
|
|
*/ |
43
|
|
|
foreach ($schema->getPropertySchemas() as $name => $propertySchema) { |
|
|
|
|
44
|
|
|
if (!null !== $default = $propertySchema->getDefault()) { |
45
|
|
|
$this->defaults[$name] = $default; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param mixed $node |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
public function hydrate($node) |
55
|
|
|
{ |
56
|
|
|
return $this->hydrateObject($node); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param mixed $node |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
public function dehydrate($node) |
64
|
|
|
{ |
65
|
|
|
return $this->dehydrateObject($node); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param \KleijnWeb\PhpApi\Descriptions\Hydrator\Processors\Processor $child |
70
|
|
|
* @return ObjectProcessor |
71
|
|
|
*/ |
72
|
|
|
public function setPropertyProcessor(string $key, Processor $child): ObjectProcessor |
73
|
|
|
{ |
74
|
|
|
$this->propertyProcessors[$key] = $child; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param mixed $data |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
protected function hydrateProperty($key, $data) |
84
|
|
|
{ |
85
|
|
|
$next = $this->propertyProcessors[$key]; |
86
|
|
|
|
87
|
|
|
return $next->hydrate($data); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param mixed $data |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
|
|
protected function dehydrateProperty($key, $data) |
95
|
|
|
{ |
96
|
|
|
$next = $this->propertyProcessors[$key]; |
97
|
|
|
|
98
|
|
|
return $next->dehydrate($data); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param Schema $valueSchema |
103
|
|
|
* @param mixed $value |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
protected function shouldFilterOutputValue(Schema $valueSchema, $value): bool |
107
|
|
|
{ |
108
|
|
|
return $value === null && (!$valueSchema instanceof ScalarSchema || !$valueSchema->isType(Schema::TYPE_NULL)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param \stdClass $input |
113
|
|
|
* @return object |
114
|
|
|
*/ |
115
|
|
|
abstract protected function hydrateObject(\stdClass $input); |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param object $input |
119
|
|
|
* @return \stdClass |
120
|
|
|
*/ |
121
|
|
|
abstract protected function dehydrateObject($input): \stdClass; |
122
|
|
|
} |
123
|
|
|
|