From 21e2ba512b6ce4361c5100849eda24837c488ab3 Mon Sep 17 00:00:00 2001 From: Ere Maijala Date: Fri, 25 Nov 2022 08:43:38 +0200 Subject: [PATCH] Ensure that the subfields key is always present. (#6) ISO 2709 and MARCXML can contain fields without subfields, so we need to ensure that the unserialized array contains the subfields key. --- CHANGELOG.md | 23 ++++++++++++ src/Serialization/Iso2709.php | 3 +- src/Serialization/MarcXml.php | 3 +- tests/MarcReaderTest.php | 66 +++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b391edc..fd82540 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,29 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. +## 1.0.2 - 2022-11-25 + +### Added + +- Nothing. + +### Changed + +- Nothing. + +### Deprecated + +- Nothing. + +### Removed + +- Nothing. + +### Fixed + +- Ensure that the subfields element is added to the field array when a MARCXML or + ISO 2709 field without subfields is unserialized. + ## 1.0.1 - 2022-11-18 ### Added diff --git a/src/Serialization/Iso2709.php b/src/Serialization/Iso2709.php index 33d6df0..18fda9e 100644 --- a/src/Serialization/Iso2709.php +++ b/src/Serialization/Iso2709.php @@ -163,7 +163,8 @@ public static function fromString(string $marc): array // an indicator: $newField = [ 'ind1' => mb_substr($tagData . ' ', 0, 1, 'UTF-8'), - 'ind2' => mb_substr($tagData . ' ', 1, 1, 'UTF-8') + 'ind2' => mb_substr($tagData . ' ', 1, 1, 'UTF-8'), + 'subfields' => [] ]; $subfields = explode( self::SUBFIELD_INDICATOR, diff --git a/src/Serialization/MarcXml.php b/src/Serialization/MarcXml.php index 9c9585e..f314ec5 100644 --- a/src/Serialization/MarcXml.php +++ b/src/Serialization/MarcXml.php @@ -151,7 +151,8 @@ public static function fromString(string $marc): array foreach ($xml->datafield as $field) { $newField = [ 'ind1' => str_pad((string)$field['ind1'], 1), - 'ind2' => str_pad((string)$field['ind2'], 1) + 'ind2' => str_pad((string)$field['ind2'], 1), + 'subfields' => [] ]; foreach ($field->subfield as $subfield) { $newField['subfields'][] diff --git a/tests/MarcReaderTest.php b/tests/MarcReaderTest.php index 4267a5e..f9a2bf9 100644 --- a/tests/MarcReaderTest.php +++ b/tests/MarcReaderTest.php @@ -162,6 +162,35 @@ public function testMarcReader() ); } + /** + * Test empty field in ISO2709 + * + * @return void + */ + public function testEmptyFieldInSO2709() + { + $marc = "00047 00037 245000200000\x1e12\x1e\x1d"; + + $reader = new MarcReader($marc); + $field = $reader->getField('245'); + $this->assertEquals([], $reader->getSubfields($field, 'b')); + $this->assertEquals( + [ + 'leader' => '00000 00000 ', + 'fields' => [ + [ + '245' => [ + 'ind1' => '1', + 'ind2' => '2', + 'subfields' => [] + ] + ] + ] + ], + json_decode($reader->toFormat('JSON'), true) + ); + } + /** * Test empty subfield in ISO2709 * @@ -176,6 +205,43 @@ public function testEmptySubfieldInSO2709() $this->assertEquals([], $reader->getSubfields($field, 'b')); } + /** + * Test empty field in MARCXML serialization + * + * @return void + */ + public function testEmptyFieldInMarcXmlSerialization() + { + $input = << + + 00047cam a22000374i 4500 + + + + +EOT; + + $reader = new MarcReader($input); + $field = $reader->getField('245'); + $this->assertEquals([], $reader->getSubfields($field, 'b')); + $this->assertEquals( + [ + 'leader' => '00000cam a22000004i 4500', + 'fields' => [ + [ + '245' => [ + 'ind1' => '1', + 'ind2' => '2', + 'subfields' => [] + ] + ] + ] + ], + json_decode($reader->toFormat('JSON'), true) + ); + } + /** * Test empty subfield in MARCXML serialization *