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

BaseParam   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 53
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 10 4
A __construct() 0 4 1
A getDefault() 0 3 1
A __get() 0 7 2
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