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

Dto   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 27
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A camelToSnake() 0 3 1
A jsonSerialize() 0 3 1
A toArray() 0 3 1
A serialize() 0 10 3
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