Test Failed
Push — develop ( 833877...64b6b0 )
by nguereza
02:36
created

BaseParam::getDefault()   A

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 0
dl 0
loc 3
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