Test Failed
Push — develop ( 64b6b0...ddb817 )
by nguereza
02:24
created

BaseParam::__get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Platine\Framework\Demo\Form\Param;
10
11
use Platine\Stdlib\Helper\Str;
12
13
class BaseParam
14
{
15
16
    /**
17
     * Create new instance
18
     * @param array<string, mixed> $data
19
     */
20
    public function __construct(array $data = [])
21
    {
22
        $params = array_merge($this->getDefault(), $data);
23
        $this->load($params);
24
    }
25
26
    /**
27
     * Load the field data
28
     * @param array<string, mixed> $data
29
     * @return void
30
     */
31
    public function load(array $data): void
32
    {
33
        foreach ($data as $name => $value) {
34
            $key = Str::camel($name, true);
35
36
            $setterMethod = 'set' . ucfirst($key);
37
            if (method_exists($this, $setterMethod)) {
38
                $this->{$setterMethod}($value);
39
            } elseif (property_exists($this, $key)) {
40
                $this->{$key} = $value;
41
            }
42
        }
43
    }
44
45
    /**
46
     * Return the fields default values
47
     * @return array<string, mixed>
48
     */
49
    public function getDefault(): array
50
    {
51
        return [];
52
    }
53
54
    /**
55
     * Return the value for the given property
56
     * @param string $name
57
     * @return mixed|null
58
     */
59
    public function __get($name)
60
    {
61
        if (property_exists($this, $name)) {
62
            return $this->{$name};
63
        }
64
65
        return null;
66
    }
67
}
68