Items::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Scriptotek\Alma\Bibs;
4
5
use Scriptotek\Alma\Client;
6
use Scriptotek\Alma\Model\IterableCollection;
7
use Scriptotek\Alma\Model\LazyResourceList;
8
use Scriptotek\Alma\Model\ReadOnlyArrayAccess;
9
10
/**
11
 * Iterable collection of Item resources belonging to some Holding resource.
12
 */
13
class Items extends LazyResourceList implements \Countable, \Iterator, \ArrayAccess
14
{
15
    use ReadOnlyArrayAccess;
16
    use IterableCollection;
17
18
    /**
19
     * The Bib this Items list belongs to.
20
     *
21
     * @var Bib
22
     */
23
    public $bib;
24
25
    /**
26
     * The Holding this Items list belongs to.
27
     *
28
     * @var Holding
29
     */
30
    public $holding;
31
32
    /**
33
     * Items constructor.
34
     *
35
     * @param Client  $client
36
     * @param Bib     $bib
37
     * @param Holding $holding
38
     */
39
    public function __construct(Client $client, Bib $bib = null, Holding $holding = null)
40
    {
41
        parent::__construct($client, 'item');
42
        $this->bib = $bib;
43
        $this->holding = $holding;
44
    }
45
46
    /**
47
     * Convert a data element to a resource object.
48
     *
49
     * @param $data
50
     *
51
     * @return Item
52
     */
53
    protected function convertToResource($data)
54
    {
55
        return Item::make($this->client, $this->bib, $this->holding, $data->item_data->pid)
56
            ->init($data);
57
    }
58
59
    /**
60
     * Generate the base URL for this resource.
61
     *
62
     * @return string
63
     */
64
    protected function urlBase()
65
    {
66
        return "/bibs/{$this->bib->mms_id}/holdings/{$this->holding->holding_id}/items";
67
    }
68
69
    /**
70
     * Get an Item object from a barcode.
71
     *
72
     * @param string $barcode
73
     *
74
     * @return Item|null
75
     */
76
    public function fromBarcode($barcode)
77
    {
78
        $destinationUrl = $this->client->getRedirectLocation('/items', ['item_barcode' => $barcode]);
79
80
        // Extract the MMS ID from the redirect target URL.
81
        // Example: https://api-eu.hosted.exlibrisgroup.com/almaws/v1/bibs/999211285764702204/holdings/22156746440002204/items/23156746430002204
82
        if (!is_null($destinationUrl) && preg_match('$bibs/([0-9]+)/holdings/([0-9]+)/items/([0-9]+)$', $destinationUrl, $matches)) {
83
            $mms_id = $matches[1];
84
            $holding_id = $matches[2];
85
            $item_id = $matches[3];
86
87
            $bib = Bib::make($this->client, $mms_id);
88
            $holding = Holding::make($this->client, $bib, $holding_id);
89
90
            return Item::make($this->client, $bib, $holding, $item_id);
91
        }
92
    }
93
}
94