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

StrictSimpleObjectProcessor::dehydrateObject()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
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
14
/**
15
 * @author John Kleijn <[email protected]>
16
 */
17
class StrictSimpleObjectProcessor extends ObjectProcessor
18
{
19
    /**
20
     * @param \stdClass $input
21
     * @return \stdClass
22
     */
23
    public function hydrateObject(\stdClass $input)
24
    {
25
        $object = (object)[];
26
27
        /** @var ObjectSchema $objectSchema */
28
        $objectSchema = $this->schema;
29
30
        /**
31
         * @var string $name
32
         * @var Schema $propertySchema
33
         */
34 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...
35
            if (!isset($input->$name) && isset($this->defaults[$name])) {
36
                $value = $this->defaults[$name];
37
            } else {
38
                $value = $input->$name;
39
            }
40
            $object->$name = $this->hydrateProperty($name, $value);
41
        }
42
43
        return $object;
44
    }
45
46
    /**
47
     * @param object $object
48
     * @return \stdClass
49
     */
50
    protected function dehydrateObject($object): \stdClass
51
    {
52
        $node = (object)[];
53
        /** @var ObjectSchema $objectSchema */
54
        $objectSchema = $this->schema;
55
56
        foreach ($object as $name => $value) {
57
            if ($this->shouldFilterOutputValue($objectSchema->getPropertySchema($name), $value)) {
58
                continue;
59
            } else {
60
                $node->$name = $this->dehydrateProperty($name, $value);
61
            }
62
        }
63
64
        return $node;
65
    }
66
}
67