Skip to content

Commit e8fba91

Browse files
committed
Add getClassifications to Record
1 parent e171e09 commit e8fba91

11 files changed

+186
-23
lines changed

src/BibliographicRecord.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Scriptotek\Marc;
44

55
use Scriptotek\Marc\Exceptions\UnknownRecordType;
6+
use Scriptotek\Marc\Fields\Classification;
67
use Scriptotek\Marc\Fields\Isbn;
78
use Scriptotek\Marc\Fields\Person;
89
use Scriptotek\Marc\Fields\Subject;
@@ -14,7 +15,7 @@ class BibliographicRecord extends Record
1415
/**
1516
* @var array List of properties to be included when serializing the record using the `toArray()` method.
1617
*/
17-
public $properties = ['id', 'isbns', 'title', 'subjects', 'creators'];
18+
public $properties = ['id', 'isbns', 'title', 'creators', 'subjects', 'classifications'];
1819

1920
/**
2021
* Get the descriptive cataloging form value from LDR/18. Returns any of
@@ -80,6 +81,22 @@ public function getSubjects($vocabulary = null, $tag = null)
8081
}));
8182
}
8283

84+
/**
85+
* Get an array of the 080, 082, 083, 084 fields as `Classification` objects, optionally
86+
* filtered by scheme and/or tag.
87+
*
88+
* @param string $scheme
89+
* @return Classification[]
90+
*/
91+
public function getClassifications($scheme = null)
92+
{
93+
return array_values(array_filter(Classification::get($this), function ($classifications) use ($scheme) {
94+
$a = is_null($scheme) || $scheme == $classifications->getScheme();
95+
96+
return $a;
97+
}));
98+
}
99+
83100
/**
84101
* Get an array of the 100 and 700 fields as `Person` objects, optionally
85102
* filtered by tag.

src/Fields/Classification.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Scriptotek\Marc\Fields;
4+
5+
use Scriptotek\Marc\Record;
6+
7+
class Classification extends Subfield implements \JsonSerializable
8+
{
9+
use SerializableField;
10+
11+
/**
12+
* @var array List of properties to be included when serializing the record using the `toArray()` method.
13+
*/
14+
15+
public $properties = ['scheme', 'number', 'heading', 'edition', 'assigning_agency', 'id'];
16+
17+
public static function get(Record $record)
18+
{
19+
$out = [];
20+
foreach ($record->getFields('08[0234]', true) as $field) {
21+
foreach ($field->getSubfields('a') as $sfa) {
22+
$out[] = new Classification($field, $sfa);
23+
}
24+
}
25+
return $out;
26+
}
27+
28+
public function getScheme()
29+
{
30+
$typeMap = [
31+
'080' => 'udc',
32+
'082' => 'ddc',
33+
'083' => 'ddc',
34+
];
35+
36+
$tag = $this->field->getTag();
37+
38+
if ($tag == '084') {
39+
return $this->field->sf('2');
40+
}
41+
42+
return $typeMap[$tag];
43+
}
44+
45+
public function getEdition()
46+
{
47+
if (in_array($this->field->getTag(), ['080', '082', '083'])) {
48+
return $this->field->sf('2');
49+
}
50+
}
51+
52+
public function getNumber()
53+
{
54+
return $this->subfield->getData();
55+
}
56+
57+
public function getAssigningVocabulary()
58+
{
59+
return $this->field->sf('q');
60+
}
61+
62+
public function getId()
63+
{
64+
// NOTE: Both $a and $0 are repeatable, but there's no examples of how that would look like.
65+
// I'm guessing that they would alternate: $a ... $0 ... $a ... $0 ... , but not sure.
66+
return $this->field->sf('0');
67+
}
68+
69+
public function __toString()
70+
{
71+
return $this->getNumber();
72+
}
73+
}

src/Fields/Field.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class Field implements \JsonSerializable
88
{
9+
use SerializableField;
10+
911
/**
1012
* @var array List of properties to be included when serializing the record using the `toArray()` method.
1113
*/

src/Fields/SerializableField.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Scriptotek\Marc\Fields;
4+
5+
use Scriptotek\Marc\Record;
6+
7+
trait SerializableField
8+
{
9+
public function jsonSerialize()
10+
{
11+
if (count($this->properties)) {
12+
$o = [];
13+
foreach ($this->properties as $prop) {
14+
$value = $this->$prop;
15+
if (is_object($value)) {
16+
$o[$prop] = $value->jsonSerialize();
17+
} elseif ($value) {
18+
$o[$prop] = $value;
19+
}
20+
}
21+
return $o;
22+
}
23+
return (string) $this;
24+
}
25+
}

src/Fields/Subfield.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
abstract class Subfield implements \JsonSerializable
66
{
7-
protected $subfield;
87
protected $field;
8+
protected $subfield;
99

10-
public function __construct(\File_MARC_Data_Field $field, \File_MARC_Subfield $subfield)
10+
public function __construct(Field $field, \File_MARC_Subfield $subfield)
1111
{
1212
$this->field = $field;
1313
$this->subfield = $subfield;

src/Fields/Subject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static function get(Record $record)
4848
foreach (parent::makeFieldObjects($record, '6..', true) as $subject) {
4949
if ($subject->getTag() == '653') {
5050
foreach ($subject->getSubfields('a') as $sfa) {
51-
$subjects[] = new UncontrolledSubject($subject->getField(), $sfa);
51+
$subjects[] = new UncontrolledSubject($subject, $sfa);
5252
}
5353
} else {
5454
$subjects[] = $subject;

src/Fields/UncontrolledSubject.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,4 @@ public function jsonSerialize()
4444
'term' => $this->getTerm(),
4545
];
4646
}
47-
4847
}

tests/ExamplesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testExample($filename) {
1717
$record = Record::fromFile($filename);
1818
$jsonFilename = substr($filename, 0, strrpos($filename, ".")) . '.json';
1919
if (!file_exists($jsonFilename)) {
20-
file_put_contents($jsonFilename, json_encode($record, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
20+
file_put_contents($jsonFilename, json_encode($record, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
2121
} else {
2222
$jsonData = file_get_contents($jsonFilename);
2323
$this->assertJsonStringEqualsJsonString($jsonData, json_encode($record));

tests/data/examples/bibliographic.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,11 @@
2525
"vocabulary": "lcsh",
2626
"term": "Nuclear reactions : Addresses, essays, lectures"
2727
}
28+
],
29+
"classifications": [
30+
{
31+
"scheme": "msc",
32+
"number": "81"
33+
}
2834
]
2935
}

tests/data/examples/bibliographic2.json

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,31 @@
6060
"type": "653",
6161
"term": "komponenter"
6262
}
63+
],
64+
"classifications": [
65+
{
66+
"scheme": "udc",
67+
"number": "535"
68+
},
69+
{
70+
"scheme": "udc",
71+
"number": "681.7"
72+
},
73+
{
74+
"scheme": "udc",
75+
"number": "534"
76+
},
77+
{
78+
"scheme": "inspec",
79+
"number": "b4170"
80+
},
81+
{
82+
"scheme": "inspec",
83+
"number": "a7820h"
84+
},
85+
{
86+
"scheme": "inspec",
87+
"number": "a4280"
88+
}
6389
]
64-
}
90+
}

0 commit comments

Comments
 (0)