Completed
Push — master ( 935a4f...80b369 )
by Veaceslav
01:53
created

ValueObject::jsonSerialize()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.5555
c 0
b 0
f 0
cc 5
nc 6
nop 0
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Aweapi\Openapi;
6
7
use stdClass;
8
9
abstract class ValueObject implements Definition
10
{
11 82
    public function jsonSerialize(): ?array
12
    {
13 82
        $properties = $this->normalizeRequiredProperties();
14
15 82
        foreach ($this->normalizeOptionalProperties() as $name => $value) {
16 77
            if ($value !== null && $value !== []) {
17 45
                $properties[$name] = $value;
18
            }
19
        }
20
21 82
        return $properties ?: null;
22
    }
23
24
    /**
25
     * Use this instead of empty maps for required properties to force JSON-object.
26
     */
27 5
    protected static function emptyObject(): stdClass
28
    {
29 5
        return new stdClass();
30
    }
31
32 5
    protected function normalizeOptionalProperties(): array
33
    {
34 5
        return [];
35
    }
36
37 42
    protected function normalizeRequiredProperties(): array
38
    {
39 42
        return [];
40
    }
41
}
42