Skip to content

Commit b468805

Browse files
committed
Fix: Parsing of Holding records
As with Bib records, use XML instead of JSON for Holding records. This also allows for editing Holding records in the future. Fixes #9
1 parent ed3dbc4 commit b468805

File tree

5 files changed

+98
-3
lines changed

5 files changed

+98
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ $holding = $alma->bibs['990310361044702204']->holdings['22102913020002204'];
259259
$marcRecord = $holding->record;
260260
```
261261

262+
**Note:** Editing holding records is not yet supported. Will be added in the future.
263+
262264
Items can be listed from holdings in the same manner as holdings can be fetched from bibs.
263265
Here's an example:
264266

spec/Bibs/HoldingSpec.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Scriptotek\Alma\Bibs\Holding;
88
use Scriptotek\Alma\Bibs\Item;
99
use Scriptotek\Alma\Client as AlmaClient;
10+
use Scriptotek\Marc\Record;
1011
use spec\Scriptotek\Alma\SpecHelper;
1112

1213
class HoldingSpec extends ObjectBehavior
@@ -18,11 +19,28 @@ public function let(AlmaClient $client, Bib $bib)
1819
$this->beConstructedWith($client, $bib, $holdings_id);
1920
}
2021

22+
protected function expectRequest($client)
23+
{
24+
$client->getXML('/bibs/abc/holdings/123')
25+
->shouldBeCalled()
26+
->willReturn(SpecHelper::getDummyData('holding_response.xml'));
27+
}
28+
2129
public function it_is_initializable()
2230
{
2331
$this->shouldHaveType(Holding::class);
2432
}
2533

34+
public function it_fetches_record_data_when_needed(AlmaClient $client)
35+
{
36+
$this->expectRequest($client);
37+
38+
$this->created_by->shouldBe('import');
39+
$this->created_date->shouldBe('2015-11-05Z');
40+
41+
$this->record->shouldHaveType(Record::class);
42+
}
43+
2644
public function it_has_items(AlmaClient $client)
2745
{
2846
$client->getJSON('/bibs/abc/holdings/123/items')

spec/Bibs/HoldingsSpec.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,15 @@ public function it_provides_an_iterator_interface_to_holding_objects(AlmaClient
103103

104104
$this->current()->shouldHaveType(Holding::class);
105105
}
106+
107+
public function it_provides_basic_data_without_loading_the_full_record(AlmaClient $client)
108+
{
109+
$client->getJSON('/bibs/abc/holdings')
110+
->shouldBeCalledTimes(1)
111+
->willReturn(json_decode($this->sample));
112+
113+
$this->shouldHaveCount(2);
114+
$this->all()[0]->shouldHaveType(Holding::class);
115+
$this->all()[0]->library->desc->shouldBe('BURNS');
116+
}
106117
}

spec/data/holding_response.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
2+
<holding>
3+
<holding_id>22163771200002204</holding_id>
4+
<created_by>import</created_by>
5+
<created_date>2015-11-05Z</created_date>
6+
<last_modified_by>System</last_modified_by>
7+
<last_modified_date>2018-11-09Z</last_modified_date>
8+
<originating_system>ILS</originating_system>
9+
<originating_system_id>h449730-47bibsys_ubo</originating_system_id>
10+
<suppress_from_publishing>false</suppress_from_publishing>
11+
<record>
12+
<leader>00384nx a2200145ui 4500</leader>
13+
<controlfield tag="001">h449730-47bibsys_ubo</controlfield>
14+
<controlfield tag="004">000631221-47bibsys_ubo</controlfield>
15+
<controlfield tag="007">ta</controlfield>
16+
<controlfield tag="008">1511020l||||||||4 uu</controlfield>
17+
<controlfield tag="009">kat</controlfield>
18+
<controlfield tag="005">20181109134730.0</controlfield>
19+
<datafield ind1="1" ind2=" " tag="014">
20+
<subfield code="a">000631221</subfield>
21+
<subfield code="b">(NO-TrBIB)</subfield>
22+
</datafield>
23+
<datafield ind1=" " ind2=" " tag="035">
24+
<subfield code="a">00kj16342-47bibsys_ubo</subfield>
25+
</datafield>
26+
<datafield ind1="1" ind2=" " tag="852">
27+
<subfield code="b">1030300</subfield>
28+
<subfield code="c">k00041</subfield>
29+
<subfield code="h">839.82</subfield>
30+
<subfield code="i">Vol:Ban</subfield>
31+
</datafield>
32+
<datafield ind1=" " ind2=" " tag="876">
33+
<subfield code="a">00kj16342</subfield>
34+
<subfield code="d">20000529</subfield>
35+
<subfield code="j">kat</subfield>
36+
<subfield code="p">303011kj0</subfield>
37+
<subfield code="r">00kj16342</subfield>
38+
<subfield code="w">BOOK</subfield>
39+
</datafield>
40+
</record>
41+
</holding>

src/Bibs/Holding.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Scriptotek\Alma\Bibs;
44

5+
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement;
56
use Scriptotek\Alma\Client;
67
use Scriptotek\Alma\Model\LazyResource;
78
use Scriptotek\Marc\Record as MarcRecord;
@@ -31,16 +32,38 @@ public function __construct(Client $client, Bib $bib, $holding_id)
3132
$this->items = Items::make($this->client, $bib, $this);
3233
}
3334

35+
/**
36+
* Get the model data.
37+
*/
38+
protected function fetchData()
39+
{
40+
return $this->client->getXML($this->url());
41+
}
42+
3443
/**
3544
* Check if we have the full representation of our data object.
3645
*
37-
* @param \stdClass $data
46+
* @param $data
3847
*
3948
* @return bool
4049
*/
4150
protected function isInitialized($data)
4251
{
43-
return isset($data->anies);
52+
return is_a($data, QuiteSimpleXMLElement::class) && $data->has('record');
53+
}
54+
55+
/**
56+
* Load MARC record onto this Bib object. Chainable method.
57+
*
58+
* @param string $xml
59+
*
60+
* @return Holding
61+
*/
62+
public function setMarcRecord($xml)
63+
{
64+
$this->_marc = MarcRecord::fromString($xml);
65+
66+
return $this;
4467
}
4568

4669
/**
@@ -50,7 +73,7 @@ protected function isInitialized($data)
5073
*/
5174
protected function onData($data)
5275
{
53-
$this->_marc = MarcRecord::fromString($data->anies[0]);
76+
$this->setMarcRecord($data->first('record')->asXML());
5477
}
5578

5679
/**

0 commit comments

Comments
 (0)