Model::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Scriptotek\Alma\Model;
4
5
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement;
6
use Scriptotek\Alma\Client;
7
8
/**
9
 * The Model class is our base class.
10
 */
11
abstract class Model implements \JsonSerializable
12
{
13
    /* @var Client */
14
    protected $client;
15
16
    /* @var \stdClass|array */
17
    protected $data;
18
19
    public function __construct(Client $client, $data = null)
20
    {
21
        $this->client = $client;
22
        $this->data = $data;
23
    }
24
25
    /**
26
     * @param Client $client
27
     * @param array  ...$params
28
     *
29
     * @return static
30
     */
31
    public static function make($client, ...$params)
32
    {
33
        return new static($client, ...$params);
34
    }
35
36
    /**
37
     * Load data onto this object. Chainable method.
38
     *
39
     * @param \stdClass|QuiteSimpleXMLElement $data
40
     *
41
     * @return self
42
     */
43
    public function init($data = null)
44
    {
45
        if (!is_null($data)) {
46
            $this->data = $data;
0 ignored issues
show
Documentation Bug introduced by
It seems like $data can also be of type object<Danmichaelo\Quite...\QuiteSimpleXMLElement>. However, the property $data is declared as type object<stdClass>|array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
47
        }
48
49
        return $this;
50
    }
51
52
    /**
53
     * Get the raw data object.
54
     */
55
    public function getData()
56
    {
57
        return $this->data;
58
    }
59
60
    /**
61
     * Magic!
62
     *
63
     * @param string $key
64
     *
65
     * @return mixed
66
     */
67
    public function __get($key)
68
    {
69
        // Convert electronic_collections to ElectronicCollections
70
        $key_s = implode('', array_map(function ($x) {
71
            return ucfirst($x);
72
        }, explode('_', $key)));
73
74
        // If there's a getter method, call it.
75
        $method = 'get' . ucfirst($key_s);
76
        if (method_exists($this, $method)) {
77
            return $this->$method();
78
        }
79
80
        // If the property is already defined on our data object, return it.
81
        if (isset($this->data->{$key})) {
82
            return $this->data->{$key};
83
        }
84
85
        $this->init();
86
87
        // If data comes from an XML response (Bib or Holding record)
88
        if (is_a($this->data, QuiteSimpleXMLElement::class)) {
89
            return $this->data->text($key);
90
        }
91
92
        // If the property is defined on our data object now, return it.
93
        if (isset($this->data->{$key})) {
94
            return $this->data->{$key};
95
        }
96
    }
97
98
    public function jsonSerialize()
99
    {
100
        return $this->data;
101
    }
102
}
103