Skip to content

Commit

Permalink
Ensure that the subfields key is always present. (#6)
Browse files Browse the repository at this point in the history
ISO 2709 and MARCXML can contain fields without subfields, so we need to ensure that the unserialized array contains the subfields key.
  • Loading branch information
EreMaijala authored Nov 25, 2022
1 parent d9e62b4 commit 21e2ba5
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/Serialization/Iso2709.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/Serialization/MarcXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'][]
Expand Down
66 changes: 66 additions & 0 deletions tests/MarcReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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 = <<<EOT
<collection xmlns="http://www.loc.gov/MARC21/slim">
<record>
<leader>00047cam a22000374i 4500</leader>
<datafield tag="245" ind1="1" ind2="2">
</datafield>
</record>
</collection>
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
*
Expand Down

0 comments on commit 21e2ba5

Please sign in to comment.