Completed
Push — master ( 638b8d...de83b5 )
by John
01:47 queued 17s
created

LooseSimpleObjectProcessor::dehydrateObject()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
cc 5
eloc 15
nc 9
nop 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the KleijnWeb\PhpApi\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\Hydrator\Processors\Object;
10
11
use KleijnWeb\PhpApi\Descriptions\Description\Schema\ObjectSchema;
12
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema;
13
use KleijnWeb\PhpApi\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) {
0 ignored issues
show
Bug introduced by
The expression $objectSchema->getPropertySchemas() of type object<stdClass> is not traversable.
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The expression $input of type object<stdClass> is not traversable.
Loading history...
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