Completed
Push — master ( f40ae6...d9f7fe )
by Igor
03:59
created

TDataInit::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
/**
3
 * @license MIT
4
 * @author Igor Sorokin <[email protected]>
5
 */
6
namespace Dspbee\Bundle\Data;
7
8
/**
9
 * Initialize class properties from array.
10
 *
11
 * Class TDataFilter
12
 * @package Dspbee\Core
13
 */
14
trait TDataInit
15
{
16
    /**
17
     * Init class members.
18
     *
19
     * @param array $data
20
     */
21
    public function initFromArray(array $data)
22
    {
23
        foreach ($data as $name => $value) {
24
            $method = 'set' . ucfirst($name);
25
            if (method_exists($this, $method)) {
26
                call_user_func_array([$this, $method], [$value]);
27
            } else {
28
                if (property_exists(get_called_class(), $name)) {
29
                    $this->$name = $value;
30
                }
31
            }
32
        }
33
    }
34
35
    /**
36
     * Init class members from $_POST array.
37
     *
38
     * @param null $callback
39
     */
40
    public function initFromPost($callback = null)
41
    {
42
        $input = filter_input_array(INPUT_POST);
43
        if (null !== $callback) {
44
            $input = array_map($callback, $input);
45
        }
46
        $this->initFromArray($input);
47
    }
48
49
    /**
50
     * Get item property.
51
     *
52
     * @param $name
53
     * @param $argumentList
54
     * @return null
55
     */
56
    public function __call($name, $argumentList)
57
    {
58
        if (property_exists(get_called_class(), $name)) {
59
            return $this->$name;
60
        }
61
        return null;
62
    }
63
}