Completed
Push — master ( 531b96...098e87 )
by Stephen
03:06
created

Model::setData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6
Metric Value
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
namespace StarCitizen\Models;
3
4
/**
5
 * Class BaseModel
6
 *
7
 * @package StarCitizen\Models;
8
 */
9
abstract class Model
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $magicProperties = [];
15
16
    /**
17
     * @param array ...$types
18
     *
19
     * @return $this
20
     */
21 2
    public function with(...$types) {
22 2
        foreach ($types as $type) {
23 2
            if (method_exists($this, strtolower($type)))
24 2
                call_user_func([$this, $type]);
25
        }
26
27 2
        return $this;
28
    }
29
30
    /**
31
     * @param $name
32
     * @return mixed
33
     * @throws \Exception
34
     */
35 4
    public function __get($name)
36
    {
37 4
        if (in_array($name, $this->magicProperties)) {
38 4
            return call_user_func([$this, $name]);
39
        }
40
41 1
        throw new \Exception('Property ' . $name . ' does not exist in this class');
42
    }
43
44
45
    /**
46
     * @param $data
47
     * @return $this
48
     */
49
    public function setData($data)
50
    {
51
        foreach ($data as $key => $value) {
52
            $this->$key = $value;
53
        }
54
55
        return $this;
56
    }
57
}