1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scriptotek\Alma\Model; |
4
|
|
|
|
5
|
|
|
use Scriptotek\Alma\Client; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The LazyResourceList extends the LazyResource class with functionality for |
9
|
|
|
* working with lists of resources, such as holdings, items, loans, etc. |
10
|
|
|
*/ |
11
|
|
|
abstract class LazyResourceList extends LazyResource implements \Countable |
12
|
|
|
{ |
13
|
|
|
/* @var array */ |
14
|
|
|
protected $resources = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The key in the response object that points to the resource list. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $responseKey; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* LazyResourceList constructor. |
25
|
|
|
* |
26
|
|
|
* @param Client $client |
27
|
|
|
* @param string $responseKey |
28
|
|
|
*/ |
29
|
|
|
public function __construct(Client $client, $responseKey) |
30
|
|
|
{ |
31
|
|
|
parent::__construct($client); |
32
|
|
|
$this->responseKey = $responseKey; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Convert a data element to a resource object. |
37
|
|
|
* |
38
|
|
|
* @param $data |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
|
|
abstract protected function convertToResource($data); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Called when data is available to be processed. |
45
|
|
|
* |
46
|
|
|
* @param mixed $data |
47
|
|
|
*/ |
48
|
|
|
public function onData($data) |
49
|
|
|
{ |
50
|
|
|
$this->resources = ($data->total_record_count === 0) ? [] : array_map( |
51
|
|
|
function (\stdClass $res) { |
52
|
|
|
return $this->convertToResource($res); |
53
|
|
|
}, |
54
|
|
|
$data->{$this->responseKey} |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Check if we have the full representation of our data object. |
60
|
|
|
* |
61
|
|
|
* @param \stdClass $data |
62
|
|
|
* @return boolean |
63
|
|
|
*/ |
64
|
|
|
protected function isInitialized($data) |
65
|
|
|
{ |
66
|
|
|
return isset($data->total_record_count); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get all the resources. |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function all() |
75
|
|
|
{ |
76
|
|
|
return $this->init()->resources; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Number of resources. |
81
|
|
|
* |
82
|
|
|
* @link http://php.net/manual/en/countable.count.php |
83
|
|
|
* |
84
|
|
|
* @return int The number of resources as an integer. |
85
|
|
|
*/ |
86
|
|
|
public function count() |
87
|
|
|
{ |
88
|
|
|
return count($this->all()); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|