BaseEntity::__construct()   A
last analyzed

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
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Quantum\Hub\Entity;
6
7
use Platine\Stdlib\Helper\Str;
8
9
/**
10
 * @class BaseEntity
11
 * @package Quantum\Hub\Entity
12
 */
13
class BaseEntity
14
{
15
    /**
16
     * Create new instance
17
     * @param array<string, mixed> $data
18
     */
19
    public function __construct(array $data = [])
20
    {
21
        $this->hydrate($data);
22
    }
23
24
    /**
25
     * Fill the property using the provided data
26
     * @param array<string, mixed> $data
27
     * @return void
28
     */
29
    protected function hydrate(array $data = []): void
30
    {
31
        foreach ($data as $key => $value) {
32
            $keyName = Str::camel($key, true);
33
            $setterMethod = sprintf('set%s', ucfirst($keyName));
34
            if (method_exists($this, $setterMethod)) {
35
                $this->{$setterMethod}($value);
36
            } elseif (property_exists($this, $keyName)) {
37
                $this->{$keyName} = $value;
38
            }
39
        }
40
    }
41
}
42