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

TDataInit   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 50
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initFromArray() 0 13 4
A initFromPost() 0 8 2
A __call() 0 7 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
}