Completed
Push — master ( c12050...bcb499 )
by Dan Michael O.
03:24
created

Model::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Scriptotek\Alma\Model;
4
5
use Scriptotek\Alma\Client;
6
7
/**
8
 * The Model class is our base class.
9
 */
10
abstract class Model implements \JsonSerializable
11
{
12
    /* @var Client */
13
    protected $client;
14
15
    /* @var \stdClass|array */
16
    protected $data;
17
18
    public function __construct(Client $client, $data = null)
19
    {
20
        $this->client = $client;
21
        $this->data = $data;
22
    }
23
24
    /**
25
     * @param Client $client
26
     * @param array ...$params
27
     * @return static
28
     */
29
    public static function make($client, ...$params)
30
    {
31
        return new static($client, ...$params);
32
    }
33
34
    /**
35
     * Load data onto this object. Chainable method.
36
     *
37
     * @param \stdClass $data
38
     *
39
     * @return self
40
     */
41
    public function init($data = null)
42
    {
43
        $this->data = $data;
44
45
        return $this;
46
    }
47
48
    /**
49
     * Get the raw data object.
50
     */
51
    public function getData()
52
    {
53
        return $this->data;
54
    }
55
56
    /**
57
     * Magic!
58
     * @param string $key
59
     * @return mixed
60
     */
61
    public function __get($key)
62
    {
63
        // If there's a getter method, call it.
64
        $method = 'get' . ucfirst($key);
65
        if (method_exists($this, $method)) {
66
            return $this->$method();
67
        }
68
69
        $this->init();
70
71
        // If the property is defined in our data object, return it.
72
        if (isset($this->data->{$key})) {
73
            return $this->data->{$key};
74
        }
75
76
        return null;
77
    }
78
79
    public function jsonSerialize()
80
    {
81
        return $this->data;
82
    }
83
}
84