From 06859e493f55d48a412a86b1dfe384a9b7ea36b3 Mon Sep 17 00:00:00 2001 From: Beatrycze Volk Date: Thu, 11 Apr 2024 12:58:29 +0200 Subject: [PATCH 1/2] Implement functions in `RecordInfo` class and its child element classes --- src/Mods/Element/RecordInfo.php | 208 +++++++++++++++--- .../RecordInfo/LanguageOfCataloging.php | 52 +++-- .../Specific/RecordInfo/RecordIdentifier.php | 11 +- .../Specific/RecordInfo/RecordInfoNote.php | 3 +- 4 files changed, 215 insertions(+), 59 deletions(-) diff --git a/src/Mods/Element/RecordInfo.php b/src/Mods/Element/RecordInfo.php index 28b528a..f5c481e 100644 --- a/src/Mods/Element/RecordInfo.php +++ b/src/Mods/Element/RecordInfo.php @@ -24,9 +24,11 @@ use Slub\Mods\Element\Specific\RecordInfo\LanguageOfCataloging; use Slub\Mods\Element\Specific\RecordInfo\RecordIdentifier; use Slub\Mods\Element\Specific\RecordInfo\RecordInfoNote; +use Slub\Mods\Element\Xml\Element; /** * RecordInfo MODS metadata element class for the 'php-mods-reader' library. + * @see https://www.loc.gov/standards/mods/userguide/recordinfo.html * * @access public */ @@ -38,61 +40,213 @@ class RecordInfo extends BaseElement * @access private * @var AuthorityLanguageElement */ - private AuthorityLanguageElement $recordContentSource; + private AuthorityLanguageElement $descriptionStandard; /** - * @access private - * @var DateElement + * This extracts the essential MODS metadata from XML + * + * @access public + * + * @param \SimpleXMLElement $xml The XML to extract the metadata from + * + * @return void */ - private DateElement $recordCreationDate; + public function __construct(\SimpleXMLElement $xml) + { + parent::__construct($xml); + } /** - * @access private - * @var DateElement + * Get the array of the elements. + * @see https://www.loc.gov/standards/mods/userguide/recordinfo.html#recordcontentsource + * + * @access public + * + * @param string $query The XPath query for metadata search + * + * @return AuthorityLanguageElement[] */ - private DateElement $recordChangeDate; + public function getRecordContentSources(string $query = ''): array + { + return $this->getAuthorityLanguageElements('./mods:recordContentSource' . $query); + } /** - * @access private - * @var RecordIdentifier + * Get the array of the elements. + * @see https://www.loc.gov/standards/mods/userguide/recordinfo.html#recordcreationdate + * + * @access public + * + * @param string $query The XPath query for metadata search + * + * @return DateElement[] */ - private RecordIdentifier $recordIdentifier; + public function getRecordCreationDates(string $query = ''): array + { + return $this->getDateElements('./mods:recordCreationDate' . $query); + } /** - * @access private - * @var LanguageElement + * Get the array of the elements. + * @see https://www.loc.gov/standards/mods/userguide/recordinfo.html#recordchangedate + * + * @access public + * + * @param string $query The XPath query for metadata search + * + * @return DateElement[] */ - private LanguageElement $recordOrigin; + public function getRecordChangeDates(string $query = ''): array + { + return $this->getDateElements('./mods:recordChangeDate' . $query); + } /** - * @access private - * @var RecordInfoNote + * Get the array of the elements. + * @see https://www.loc.gov/standards/mods/userguide/recordinfo.html#recordidentifier + * + * @access public + * + * @param string $query The XPath query for metadata search + * + * @return RecordIdentifier[] */ - private RecordInfoNote $recordInfoNote; + public function getRecordIdentifiers(string $query = ''): array + { + $recordIdentifiers = []; + $xpath = './mods:recordIdentifier' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + foreach ($element->getValues() as $value) { + $recordIdentifiers[] = new RecordIdentifier($value); + } + } + return $recordIdentifiers; + } /** - * @access private - * @var LanguageOfCataloging + * Get the array of the elements. + * @see https://www.loc.gov/standards/mods/userguide/recordinfo.html#recordorigin + * + * @access public + * + * @param string $query The XPath query for metadata search + * + * @return LanguageElement[] */ - private LanguageOfCataloging $languageOfCataloging; + public function getRecordOrigins(string $query = ''): array + { + $recordOrigins = []; + $xpath = './mods:recordOrigin' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + foreach ($element->getValues() as $value) { + $recordOrigins[] = new LanguageElement($value); + } + } + return $recordOrigins; + } /** - * @access private - * @var AuthorityLanguageElement + * Get the array of the elements. + * @see https://www.loc.gov/standards/mods/userguide/recordinfo.html#recordidentifier + * + * @access public + * + * @param string $query The XPath query for metadata search + * + * @return RecordIdentifier[] */ - private AuthorityLanguageElement $descriptionStandard; + public function getRecordInfoNotes(string $query = ''): array + { + $recordInfoNotes = []; + $xpath = './mods:recordInfoNote' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + foreach ($element->getValues() as $value) { + $recordInfoNotes[] = new RecordInfoNote($value); + } + } + return $recordInfoNotes; + } /** - * This extracts the essential MODS metadata from XML + * Get the array of the elements. + * @see https://www.loc.gov/standards/mods/userguide/recordinfo.html#languageofcataloging * * @access public * - * @param \SimpleXMLElement $xml The XML to extract the metadata from + * @param string $query The XPath query for metadata search * - * @return void + * @return LanguageOfCataloging[] */ - public function __construct(\SimpleXMLElement $xml) + public function getLanguageOfCatalogings(string $query = ''): array { - parent::__construct($xml); + $languageOfCatalogings = []; + $xpath = './mods:languageOfCataloging' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + foreach ($element->getValues() as $value) { + $languageOfCatalogings[] = new LanguageOfCataloging($value); + } + } + return $languageOfCatalogings; + } + + /** + * Get the array of the elements. + * @see https://www.loc.gov/standards/mods/userguide/recordinfo.html#descriptionstandard + * + * @access public + * + * @param string $query The XPath query for metadata search + * + * @return AuthorityLanguageElement[] + */ + public function getDescriptionStandards(string $query = ''): array + { + return $this->getAuthorityLanguageElements('./mods:descriptionStandard' . $query); + } + + /** + * Get the array of the matching elements. + * + * @access public + * + * @param string $xpath The XPath for metadata search + * + * @return AuthorityLanguageElement[] + */ + private function getAuthorityLanguageElements(string $xpath): array + { + $elements = []; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + foreach ($element->getValues() as $value) { + $elements[] = new AuthorityLanguageElement($value); + } + } + return $elements; + } + + /** + * Get the array of the matching elements. + * + * @access public + * + * @param string $xpath The XPath for metadata search + * + * @return DateElement[] + */ + private function getDateElements(string $xpath): array + { + $elements = []; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + foreach ($element->getValues() as $value) { + $elements[] = new DateElement($value); + } + } + return $elements; } } diff --git a/src/Mods/Element/Specific/RecordInfo/LanguageOfCataloging.php b/src/Mods/Element/Specific/RecordInfo/LanguageOfCataloging.php index 73307a8..4022d57 100644 --- a/src/Mods/Element/Specific/RecordInfo/LanguageOfCataloging.php +++ b/src/Mods/Element/Specific/RecordInfo/LanguageOfCataloging.php @@ -19,9 +19,11 @@ use Slub\Mods\Element\Common\BaseElement; use Slub\Mods\Element\Specific\Language\LanguageTerm; use Slub\Mods\Element\Specific\Language\ScriptTerm; +use Slub\Mods\Element\Xml\Element; /** * LanguageOfCataloging MODS metadata element class for the 'php-mods-reader' library. + * @see https://www.loc.gov/standards/mods/userguide/recordinfo.html#languageofcataloging * * @access public */ @@ -29,18 +31,6 @@ class LanguageOfCataloging extends BaseElement { use IdAttribute, AltRepGroupAttribute, DisplayLabelAttribute, UsageAttribute; - /** - * @access private - * @var LanguageTerm - */ - private LanguageTerm $languageTerm; - - /** - * @access private - * @var ScriptTerm - */ - private ScriptTerm $scriptTerm; - /** * This extracts the essential MODS metadata from XML * @@ -53,13 +43,11 @@ class LanguageOfCataloging extends BaseElement public function __construct(\SimpleXMLElement $xml) { parent::__construct($xml); - - $this->languageTerm = new LanguageTerm($xml); - $this->scriptTerm = new ScriptTerm($xml); } /** - * Get the value of objectPart + * Get the value of 'objectPart' attribute. + * @see https://www.loc.gov/standards/mods/userguide/recordinfo.html#objectpart * * @access public * @@ -71,26 +59,42 @@ public function getObjectPart(): string } /** - * Get the value of languageTerm + * Get the value of the element. + * @see https://www.loc.gov/standards/mods/userguide/recordinfo.html#languageterm * * @access public * - * @return LanguageTerm + * @param string $query The XPath query for metadata search + * + * @return ?LanguageTerm */ - public function getLanguageTerm(): LanguageTerm + public function getLanguageTerm(string $query = ''): ?LanguageTerm { - return $this->languageTerm; + $xpath = './mods:languageTerm' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + return new LanguageTerm($element->getValues()[0]); + } + return null; } /** - * Get the value of scriptTerm + * Get the value of the element. + * @see https://www.loc.gov/standards/mods/userguide/recordinfo.html#scriptterm * * @access public * - * @return ScriptTerm + * @param string $query The XPath query for metadata search + * + * @return ?ScriptTerm */ - public function getScriptTerm(): ScriptTerm + public function getScriptTerm(string $query = ''): ?ScriptTerm { - return $this->scriptTerm; + $xpath = './mods:scriptTerm' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + return new ScriptTerm($element->getValues()[0]); + } + return null; } } diff --git a/src/Mods/Element/Specific/RecordInfo/RecordIdentifier.php b/src/Mods/Element/Specific/RecordInfo/RecordIdentifier.php index 316ebb6..2131a9c 100644 --- a/src/Mods/Element/Specific/RecordInfo/RecordIdentifier.php +++ b/src/Mods/Element/Specific/RecordInfo/RecordIdentifier.php @@ -17,6 +17,7 @@ /** * RecordIdentifier MODS metadata element class for the 'php-mods-reader' library. + * @see https://www.loc.gov/standards/mods/userguide/recordinfo.html#recordidentifier * * @access public */ @@ -39,7 +40,8 @@ public function __construct(\SimpleXMLElement $xml) } /** - * Get the value of source + * Get the value of the 'source' attribute. + * @see https://www.loc.gov/standards/mods/userguide/recordinfo.html#source * * @access public * @@ -47,11 +49,6 @@ public function __construct(\SimpleXMLElement $xml) */ public function getSource(): string { - $value = $this->xml->attributes()->source; - - if (!empty($value)) { - return $value; - } - return ''; + return $this->getStringAttribute('source'); } } diff --git a/src/Mods/Element/Specific/RecordInfo/RecordInfoNote.php b/src/Mods/Element/Specific/RecordInfo/RecordInfoNote.php index 21cea7e..85d0195 100644 --- a/src/Mods/Element/Specific/RecordInfo/RecordInfoNote.php +++ b/src/Mods/Element/Specific/RecordInfo/RecordInfoNote.php @@ -45,7 +45,8 @@ public function __construct(\SimpleXMLElement $xml) } /** - * Get the value of type + * Get the value of the 'type' attribute. + * @see https://www.loc.gov/standards/mods/userguide/recordinfo.html#type * * @access public * From a283dbeed5e68618ea599ae9ec8d878d13f3ec18 Mon Sep 17 00:00:00 2001 From: Beatrycze Volk Date: Thu, 11 Apr 2024 14:03:07 +0200 Subject: [PATCH 2/2] Implement tests for recordInfo element --- tests/Mods/ModsReaderTest.php | 106 ++++++++++++++++++++++++++------ tests/resources/mods_book.xml | 5 +- tests/resources/mods_serial.xml | 8 ++- 3 files changed, 93 insertions(+), 26 deletions(-) diff --git a/tests/Mods/ModsReaderTest.php b/tests/Mods/ModsReaderTest.php index 346f066..5da6d37 100644 --- a/tests/Mods/ModsReaderTest.php +++ b/tests/Mods/ModsReaderTest.php @@ -833,11 +833,22 @@ public function testGetRecordInfosForBookDocument() self::assertNotEmpty($recordInfos); self::assertEquals(1, count($recordInfos)); self::assertNotEmpty($recordInfos[0]->getValue()); - //self::assertEquals('', $recordInfos[0]->getRecordIdentifier()); - //self::assertNotEmpty($recordInfos[0]->getRecordOrigin()); - //self::assertNotEmpty($recordInfos[0]->getLanguageOfCataloging()); - - // TODO: implement reading of elements + self::assertNotEmpty($recordInfos[0]->getDescriptionStandards()); + self::assertEquals('aacr', $recordInfos[0]->getDescriptionStandards()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordContentSources()); + self::assertEquals('marcorg', $recordInfos[0]->getRecordContentSources()[0]->getAuthority()); + self::assertEquals('DLC', $recordInfos[0]->getRecordContentSources()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordCreationDates()); + self::assertEquals('marc', $recordInfos[0]->getRecordCreationDates()[0]->getEncoding()); + self::assertEquals('990730', $recordInfos[0]->getRecordCreationDates()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordChangeDates()); + self::assertEquals('iso8601', $recordInfos[0]->getRecordChangeDates()[0]->getEncoding()); + self::assertEquals('20060801143536.0', $recordInfos[0]->getRecordChangeDates()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordIdentifiers()); + self::assertEquals('DE-14', $recordInfos[0]->getRecordIdentifiers()[0]->getSource()); + self::assertEquals('11761548', $recordInfos[0]->getRecordIdentifiers()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordOrigins()); + self::assertStringContainsString('Converted from MARCXML to MODS', $recordInfos[0]->getRecordOrigins()[0]->getValue()); } public function testGetRecordInfosByQueryForBookDocument() @@ -846,11 +857,22 @@ public function testGetRecordInfosByQueryForBookDocument() self::assertNotEmpty($recordInfos); self::assertEquals(1, count($recordInfos)); self::assertNotEmpty($recordInfos[0]->getValue()); - //self::assertEquals('', $recordInfos[0]->getRecordIdentifier()); - //self::assertNotEmpty($recordInfos[0]->getRecordOrigin()); - //self::assertNotEmpty($recordInfos[0]->getLanguageOfCataloging()); - - // TODO: implement reading of elements + self::assertNotEmpty($recordInfos[0]->getDescriptionStandards()); + self::assertEquals('aacr', $recordInfos[0]->getDescriptionStandards()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordContentSources()); + self::assertEquals('marcorg', $recordInfos[0]->getRecordContentSources()[0]->getAuthority()); + self::assertEquals('DLC', $recordInfos[0]->getRecordContentSources()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordCreationDates()); + self::assertEquals('marc', $recordInfos[0]->getRecordCreationDates()[0]->getEncoding()); + self::assertEquals('990730', $recordInfos[0]->getRecordCreationDates()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordChangeDates()); + self::assertEquals('iso8601', $recordInfos[0]->getRecordChangeDates()[0]->getEncoding()); + self::assertEquals('20060801143536.0', $recordInfos[0]->getRecordChangeDates()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordIdentifiers()); + self::assertEquals('DE-14', $recordInfos[0]->getRecordIdentifiers()[0]->getSource()); + self::assertEquals('11761548', $recordInfos[0]->getRecordIdentifiers()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordOrigins()); + self::assertStringContainsString('Converted from MARCXML to MODS', $recordInfos[0]->getRecordOrigins()[0]->getValue()); } public function testGetNoRecordInfosByQueryForBookDocument() @@ -865,11 +887,33 @@ public function testGetRecordInfosForSerialDocument() self::assertNotEmpty($recordInfos); self::assertEquals(1, count($recordInfos)); self::assertNotEmpty($recordInfos[0]->getValue()); - //self::assertEquals('', $recordInfos[0]->getRecordIdentifier()); - //self::assertNotEmpty($recordInfos[0]->getRecordOrigin()); - //self::assertNotEmpty($recordInfos[0]->getLanguageOfCataloging()); - - // TODO: implement reading of elements + self::assertNotEmpty($recordInfos[0]->getDescriptionStandards()); + self::assertEquals('aacr', $recordInfos[0]->getDescriptionStandards()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordContentSources()); + self::assertEquals('marcorg', $recordInfos[0]->getRecordContentSources()[0]->getAuthority()); + self::assertEquals('NLC', $recordInfos[0]->getRecordContentSources()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordCreationDates()); + self::assertEquals('marc', $recordInfos[0]->getRecordCreationDates()[0]->getEncoding()); + self::assertEquals('021127', $recordInfos[0]->getRecordCreationDates()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordChangeDates()); + self::assertEquals('iso8601', $recordInfos[0]->getRecordChangeDates()[0]->getEncoding()); + self::assertEquals('20080910160139.0', $recordInfos[0]->getRecordChangeDates()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordIdentifiers()); + self::assertEquals('15446420', $recordInfos[0]->getRecordIdentifiers()[0]->getValue()); + self::assertStringContainsString('Converted from MARCXML to MODS', $recordInfos[0]->getRecordOrigins()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordInfoNotes()); + self::assertEquals(2, count($recordInfos[0]->getRecordInfoNotes())); + self::assertEquals('Some info', $recordInfos[0]->getRecordInfoNotes()[1]->getValue()); + $languages = $recordInfos[0]->getLanguageOfCatalogings(); + self::assertNotEmpty($languages); + self::assertNotNull($languages[0]->getLanguageTerm()); + self::assertEquals('code', $languages[0]->getLanguageTerm()->getType()); + self::assertEquals('iso639-2b', $languages[0]->getLanguageTerm()->getAuthority()); + self::assertEquals('eng', $languages[0]->getLanguageTerm()->getValue()); + self::assertNotNull($languages[0]->getScriptTerm()); + self::assertEquals('code', $languages[0]->getScriptTerm()->getType()); + self::assertEquals('iso15924', $languages[0]->getScriptTerm()->getAuthority()); + self::assertEquals('Latn', $languages[0]->getScriptTerm()->getValue()); } public function testGetRecordInfosByQueryForSerialDocument() @@ -878,11 +922,33 @@ public function testGetRecordInfosByQueryForSerialDocument() self::assertNotEmpty($recordInfos); self::assertEquals(1, count($recordInfos)); self::assertNotEmpty($recordInfos[0]->getValue()); - //self::assertEquals('', $recordInfos[0]->getRecordIdentifier()); - //self::assertNotEmpty($recordInfos[0]->getRecordOrigin()); - //self::assertNotEmpty($recordInfos[0]->getLanguageOfCataloging()); - - // TODO: implement reading of elements + self::assertNotEmpty($recordInfos[0]->getDescriptionStandards()); + self::assertEquals('aacr', $recordInfos[0]->getDescriptionStandards()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordContentSources()); + self::assertEquals('marcorg', $recordInfos[0]->getRecordContentSources()[0]->getAuthority()); + self::assertEquals('NLC', $recordInfos[0]->getRecordContentSources()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordCreationDates()); + self::assertEquals('marc', $recordInfos[0]->getRecordCreationDates()[0]->getEncoding()); + self::assertEquals('021127', $recordInfos[0]->getRecordCreationDates()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordChangeDates()); + self::assertEquals('iso8601', $recordInfos[0]->getRecordChangeDates()[0]->getEncoding()); + self::assertEquals('20080910160139.0', $recordInfos[0]->getRecordChangeDates()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordIdentifiers()); + self::assertEquals('15446420', $recordInfos[0]->getRecordIdentifiers()[0]->getValue()); + self::assertStringContainsString('Converted from MARCXML to MODS', $recordInfos[0]->getRecordOrigins()[0]->getValue()); + self::assertNotEmpty($recordInfos[0]->getRecordInfoNotes()); + self::assertEquals(2, count($recordInfos[0]->getRecordInfoNotes())); + self::assertEquals('Some info', $recordInfos[0]->getRecordInfoNotes()[1]->getValue()); + $languages = $recordInfos[0]->getLanguageOfCatalogings(); + self::assertNotEmpty($languages); + self::assertNotNull($languages[0]->getLanguageTerm()); + self::assertEquals('code', $languages[0]->getLanguageTerm()->getType()); + self::assertEquals('iso639-2b', $languages[0]->getLanguageTerm()->getAuthority()); + self::assertEquals('eng', $languages[0]->getLanguageTerm()->getValue()); + self::assertNotNull($languages[0]->getScriptTerm()); + self::assertEquals('code', $languages[0]->getScriptTerm()->getType()); + self::assertEquals('iso15924', $languages[0]->getScriptTerm()->getAuthority()); + self::assertEquals('Latn', $languages[0]->getScriptTerm()->getValue()); } public function testGetNoRecordInfosByQueryForSerialDocument() diff --git a/tests/resources/mods_book.xml b/tests/resources/mods_book.xml index a656b47..92f56d7 100644 --- a/tests/resources/mods_book.xml +++ b/tests/resources/mods_book.xml @@ -153,8 +153,7 @@ DLC 990730 20060801143536.0 - 11761548 - Converted from MARCXML to MODS version 3.8 using MARC21slim2MODS3-8_XSLT1-0.xsl - (Revision 1.172 20230208) + 11761548 + Converted from MARCXML to MODS version 3.8 using MARC21slim2MODS3-8_XSLT1-0.xsl (Revision 1.172 20230208) diff --git a/tests/resources/mods_serial.xml b/tests/resources/mods_serial.xml index 60ee312..58d3949 100644 --- a/tests/resources/mods_serial.xml +++ b/tests/resources/mods_serial.xml @@ -105,10 +105,12 @@ 021127 20080910160139.0 15446420 - Converted from MARCXML to MODS version 3.8 using MARC21slim2MODS3-8_XSLT1-0.xsl - (Revision 1.172 20230208) + Converted from MARCXML to MODS version 3.8 using MARC21slim2MODS3-8_XSLT1-0.xsl (Revision 1.172 20230208) + Random note + Some info - eng + eng + Latn