|
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\Schema; |
|
13
|
|
|
use KleijnWeb\PhpApi\Descriptions\Hydrator\Processors\AnyProcessor; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author John Kleijn <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class LooseSimpleObjectProcessor extends ObjectProcessor |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var AnyProcessor |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $anyProcessor; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* LooseSimpleObjectHydrator constructor. |
|
27
|
|
|
* @param ObjectSchema $schema |
|
28
|
|
|
* @param AnyProcessor $anyProcessor |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(ObjectSchema $schema, AnyProcessor $anyProcessor) |
|
31
|
|
|
{ |
|
32
|
|
|
parent::__construct($schema); |
|
33
|
|
|
$this->anyProcessor = $anyProcessor; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param \stdClass $input |
|
38
|
|
|
* @return \stdClass |
|
39
|
|
|
*/ |
|
40
|
|
|
public function hydrateObject(\stdClass $input) |
|
41
|
|
|
{ |
|
42
|
|
|
$output = (object)[]; |
|
43
|
|
|
|
|
44
|
|
|
/** @var ObjectSchema $objectSchema */ |
|
45
|
|
|
$objectSchema = $this->schema; |
|
46
|
|
|
|
|
47
|
|
|
/** @var Schema $propertySchema */ |
|
48
|
|
View Code Duplication |
foreach ($objectSchema->getPropertySchemas() as $name => $propertySchema) { |
|
|
|
|
|
|
49
|
|
|
if (!property_exists($input, $name)) { |
|
50
|
|
|
if (!isset($this->defaults[$name])) { |
|
51
|
|
|
continue; |
|
52
|
|
|
} |
|
53
|
|
|
$value = $this->defaults[$name]; |
|
54
|
|
|
} else { |
|
55
|
|
|
$value = $input->$name; |
|
56
|
|
|
} |
|
57
|
|
|
$output->$name = $this->hydrateProperty($name, $value); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
foreach ($input as $name => $value) { |
|
|
|
|
|
|
61
|
|
|
if (!isset($output->$name)) { |
|
62
|
|
|
$output->$name = $this->anyProcessor->hydrate($value); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $output; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param object $input |
|
71
|
|
|
* @return \stdClass |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function dehydrateObject($input): \stdClass |
|
74
|
|
|
{ |
|
75
|
|
|
/** @var ObjectSchema $objectSchema */ |
|
76
|
|
|
$objectSchema = $this->schema; |
|
77
|
|
|
|
|
78
|
|
|
/** @var Schema[] $propertySchemas */ |
|
79
|
|
|
$propertySchemas = []; |
|
80
|
|
|
|
|
81
|
|
|
$output = clone $input; |
|
82
|
|
|
|
|
83
|
|
|
foreach ($input as $name => $value) { |
|
84
|
|
|
if ($objectSchema->hasPropertySchema($name)) { |
|
85
|
|
|
$propertySchemas[$name] = $objectSchema->getPropertySchema($name); |
|
86
|
|
|
} else { |
|
87
|
|
|
$output->$name = $this->anyProcessor->dehydrate($value); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
foreach ($propertySchemas as $name => $propertySchema) { |
|
92
|
|
|
if ($this->shouldFilterOutputValue($propertySchema, $input->$name)) { |
|
93
|
|
|
continue; |
|
94
|
|
|
} else { |
|
95
|
|
|
$output->$name = $this->dehydrateProperty($name, $input->$name); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $output; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|