1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scriptotek\Alma\Model; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\UriInterface; |
6
|
|
|
use Scriptotek\Alma\Client; |
7
|
|
|
use Scriptotek\Alma\Exception\ResourceNotFound; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A LazyResource is anything that has its own URL and therefore can be lazy-loaded. |
11
|
|
|
* This class implements the basic ghost model functionality. |
12
|
|
|
* |
13
|
|
|
* The $initialized property indicates if the model is loaded or not. |
14
|
|
|
*/ |
15
|
|
|
abstract class LazyResource extends Model |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* This class is a ghost object that lazy loads the full record only when needed. |
19
|
|
|
* If $initialized is false, it means we haven't yet loaded the full record. |
20
|
|
|
* We can still have incomplete data from a search response. |
21
|
|
|
*/ |
22
|
|
|
protected $initialized = false; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array Query string parameters |
26
|
|
|
*/ |
27
|
|
|
protected $params = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Set request query string parameters. |
31
|
|
|
* @param $params array |
32
|
|
|
* @return $this |
33
|
|
|
*/ |
34
|
|
|
public function setParams($params) |
35
|
|
|
{ |
36
|
|
|
$this->params = $params; |
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the request query string parameters. |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
public function getParams() |
45
|
|
|
{ |
46
|
|
|
return $this->params; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Check if we have the full representation of our data object. |
51
|
|
|
* |
52
|
|
|
* @param \stdClass $data |
53
|
|
|
* @return boolean |
54
|
|
|
*/ |
55
|
|
|
abstract protected function isInitialized($data); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Load data onto this object. Chainable method. |
59
|
|
|
* |
60
|
|
|
* @param \stdClass $data |
61
|
|
|
* |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function init($data = null) |
65
|
|
|
{ |
66
|
|
|
if ($this->initialized) { |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (is_null($data)) { |
71
|
|
|
$data = $this->fetchData(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (!is_null($data) && $this->isInitialized($data)) { |
75
|
|
|
$this->initialized = true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->data = $data; |
79
|
|
|
if ($this->initialized) { |
80
|
|
|
$this->onData($data); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the model data. |
88
|
|
|
*/ |
89
|
|
|
protected function fetchData() |
90
|
|
|
{ |
91
|
|
|
return $this->client->getJSON($this->url()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Called when data is available to be processed. |
96
|
|
|
* The resource classes can use this method to process the data. |
97
|
|
|
* |
98
|
|
|
* @param mixed $data |
99
|
|
|
*/ |
100
|
|
|
protected function onData($data) |
101
|
|
|
{ |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the raw data object. |
106
|
|
|
*/ |
107
|
|
|
public function getData() |
108
|
|
|
{ |
109
|
|
|
return $this->init()->data; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Check if the object exists. |
114
|
|
|
*/ |
115
|
|
|
public function exists() |
116
|
|
|
{ |
117
|
|
|
try { |
118
|
|
|
$this->init(); |
119
|
|
|
} catch (ResourceNotFound $ex) { |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->initialized; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Generate the base URL for this resource. |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
abstract protected function urlBase(); |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Build a full URL for a resource. |
134
|
|
|
* |
135
|
|
|
* @param string $url |
136
|
|
|
* @param array $query |
137
|
|
|
* @return UriInterface |
138
|
|
|
*/ |
139
|
|
|
protected function url($url = '', $query = []) |
140
|
|
|
{ |
141
|
|
|
$query = array_merge($this->params, $query); |
142
|
|
|
|
143
|
|
|
return $this->client->buildUrl($this->urlBase() . $url, $query); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|