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

BaseParam   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 39
rs 10
wmc 6

3 Methods

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