Test Failed
Push — master ( c6b6d3...528954 )
by Dan Michael O.
03:21
created

Model::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Scriptotek\Alma;
4
5
abstract class Model
6
{
7
    /* @var Client */
8
    protected $client;
9
10
    /* @var \stdClass */
11
    protected $data;
12
13
    public function __construct(Client $client, $data = null)
14
    {
15
        $this->client = $client;
16
        $this->data = $data;
17
    }
18
19
    /**
20
     * @param Client $client
21
     * @param array ...$params
22
     * @return static
23
     */
24
	static function make($client, ...$params) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25
		return new static($client, ...$params);
26
	}
27
28
    /**
29
     * Load data onto this object. Chainable method.
30
     *
31
     * @param \stdClass $data
32
     *
33
     * @return self
34
     */
35
    public function init($data = null)
36
    {
37
        $this->data = $data;
38
39
        return $this;
40
    }
41
42
    /**
43
     * Get the raw data object.
44
     */
45
    public function getData()
46
    {
47
        return $this->data;
48
    }
49
50
    /**
51
     * Magic!
52
     * @param string $key
53
     * @return mixed
54
     */
55
    public function __get($key)
56
    {
57
        // If there's a getter method, call it.
58
        $method = 'get' . ucfirst($key);
59
        if (method_exists($this, $method)) {
60
            return $this->$method();
61
        }
62
63
        $this->init();
64
65
        // If the property is defined in our data object, return it.
66
        if (isset($this->data->{$key})) {
67
            return $this->data->{$key};
68
        }
69
70
        return null;
71
    }
72
}