Passed
Branch develop (9a7c76)
by Freddie
01:53
created

Entity   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 117
rs 10
c 0
b 0
f 0
wmc 13

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __set() 0 7 1
A __get() 0 3 1
A __call() 0 9 2
A snakeCase() 0 3 1
A camelCase() 0 7 1
A toArray() 0 11 3
A hydrate() 0 7 2
A __toString() 0 3 1
1
<?php
2
3
namespace FlexPHP\Entities;
4
5
/**
6
 * Class Entity
7
 * @package FlexPHP\Entities
8
 */
9
abstract class Entity implements EntityInterface
10
{
11
    /**
12
     * Save attribute names hydrate on instance
13
     *
14
     * @var array
15
     */
16
    private $attributesHydrated = [];
17
18
    /**
19
     * Entity constructor.
20
     * @param array $attributes
21
     */
22
    public function __construct(array $attributes = [])
23
    {
24
        $this->hydrate($attributes);
25
    }
26
27
    public function toArray(): array
28
    {
29
        $toArray = [];
30
31
        foreach ($this->attributesHydrated as $index => $attribute) {
32
            if (\property_exists($this, $attribute)) {
33
                $toArray[$this->camelCase($attribute)] = $this->{$attribute};
34
            }
35
        }
36
37
        return $toArray;
38
    }
39
40
    /**
41
     * @param  array $attributes
42
     * @return $this
43
     */
44
    private function hydrate(array $attributes): self
45
    {
46
        foreach ($attributes as $attribute => $value) {
47
            $this->{$this->snakeCase($attribute)} = $value;
48
        }
49
50
        return $this;
51
    }
52
53
    /**
54
     * @param string $name
55
     * @param mixed $value
56
     * @return $this
57
     */
58
    public function __set($name, $value)
59
    {
60
        $this->{$name} = $value;
61
62
        $this->attributesHydrated[] = $name;
63
64
        return $this;
65
    }
66
67
    /**
68
     * @param string $name
69
     * @return mixed
70
     */
71
    public function __get($name)
72
    {
73
        return $this->{$name} ?? null;
74
    }
75
76
    /**
77
     * @param string $name
78
     * @param array $arguments
79
     * @return $this|mixed
80
     */
81
    public function __call(string $name, array $arguments)
82
    {
83
        $attribute = $this->snakeCase($name);
84
85
        if (\count($arguments) > 0) {
86
            return $this->__set($attribute, $arguments[0]);
87
        }
88
89
        return $this->__get($attribute);
90
    }
91
92
    /**
93
     * Entity to json string
94
     *
95
     * @return string
96
     */
97
    public function __toString()
98
    {
99
        return (string)\json_encode($this->toArray(), JSON_ERROR_NONE);
100
    }
101
102
    /**
103
     * Change to snake_case attribute name
104
     *
105
     * @param string $attribute
106
     * @return string
107
     */
108
    private function snakeCase(string $attribute): string
109
    {
110
        return \mb_strtolower(\preg_replace('~(?<=\\w)([A-Z])~', '_$1', $attribute) ?? $attribute);
111
    }
112
113
    /**
114
     * Change to camelCase attribute name
115
     *
116
     * @param string $attribute
117
     * @return string
118
     */
119
    private function camelCase(string $attribute): string
120
    {
121
        $string = \preg_replace_callback('/_(.?)/', function ($matches) {
122
            return \ucfirst($matches[1]);
123
        }, $attribute);
124
125
        return $string ?? $attribute;
126
    }
127
}
128