Passed
Push — master ( 956624...05ed8c )
by Bertrand
08:49
created

Dto::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
4
namespace App\Src\UseCases\Domain\Shared\Model;
5
6
7
abstract class Dto implements \JsonSerializable
8
{
9
    public function jsonSerialize()
10
    {
11
        return $this->serialize(get_object_vars($this));
12
    }
13
14
    private function serialize(array $properties)
15
    {
16
        foreach ($properties as $key => $property){
17
            if(is_object($property)){
18
                $params[$this->camelToSnake($key)] = $this->serialize(get_object_vars($property));
19
            }else {
20
                $params[$this->camelToSnake($key)] = $property;
21
            }
22
        }
23
        return $params;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $params seems to be defined by a foreach iteration on line 16. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
24
    }
25
26
    private function camelToSnake($input)
27
    {
28
        return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $input));
29
    }
30
31
    public function toArray():array
32
    {
33
        return $this->serialize(get_object_vars($this));
34
    }
35
}
36