From b457be7b0a26cbb126dedeb539c7ada4764e3f93 Mon Sep 17 00:00:00 2001 From: Beatrycze Volk Date: Mon, 8 Apr 2024 19:39:17 +0200 Subject: [PATCH 1/2] Implement functions in `TitleInfo` class and its child element classes --- .../Element/Specific/TitleInfo/NonSort.php | 1 + src/Mods/Element/TitleInfo.php | 121 ++++++++++-------- 2 files changed, 66 insertions(+), 56 deletions(-) diff --git a/src/Mods/Element/Specific/TitleInfo/NonSort.php b/src/Mods/Element/Specific/TitleInfo/NonSort.php index b305d70..bf6af03 100644 --- a/src/Mods/Element/Specific/TitleInfo/NonSort.php +++ b/src/Mods/Element/Specific/TitleInfo/NonSort.php @@ -17,6 +17,7 @@ /** * NonSort MODS metadata element class for the 'php-mods-reader' library. + * @see https://www.loc.gov/standards/mods/userguide/titleinfo.html#nonsort * * @access public */ diff --git a/src/Mods/Element/TitleInfo.php b/src/Mods/Element/TitleInfo.php index 78f29a2..d5d57fe 100644 --- a/src/Mods/Element/TitleInfo.php +++ b/src/Mods/Element/TitleInfo.php @@ -27,9 +27,11 @@ use Slub\Mods\Element\Common\BaseElement; use Slub\Mods\Element\Common\LanguageElement; use Slub\Mods\Element\Specific\TitleInfo\NonSort; +use Slub\Mods\Element\Xml\Element; /** * TitleInfo MODS metadata element class for the 'php-mods-reader' library. + * @see https://www.loc.gov/standards/mods/userguide/titleinfo.html * * @access public */ @@ -48,36 +50,6 @@ class TitleInfo extends BaseElement 'uniform' ]; - /** - * @access private - * @var LanguageElement - */ - private LanguageElement $title; - - /** - * @access private - * @var LanguageElement - */ - private LanguageElement $subTitle; - - /** - * @access private - * @var LanguageElement - */ - private LanguageElement $partNumber; - - /** - * @access private - * @var LanguageElement - */ - private LanguageElement $partName; - - /** - * @access private - * @var NonSort - */ - private NonSort $nonSort; - /** * This extracts the essential MODS metadata from XML * @@ -90,16 +62,11 @@ class TitleInfo extends BaseElement public function __construct(\SimpleXMLElement $xml) { parent::__construct($xml); - - $this->title = new LanguageElement($xml); - $this->subTitle = new LanguageElement($xml); - $this->partNumber = new LanguageElement($xml); - $this->partName = new LanguageElement($xml); - $this->nonSort = new NonSort($xml); } /** - * Get the value of type + * Get the value of the 'type' attribute. + * @see https://www.loc.gov/standards/mods/userguide/titleinfo.html#type * * @access public * @@ -111,62 +78,104 @@ public function getType(): string } /** - * Get the value of title + * Get the value of the element. + * @see https://www.loc.gov/standards/mods/userguide/titleinfo.html#title * * @access public * - * @return LanguageElement + * @param string $query The XPath query for metadata search + * + * @return ?LanguageElement */ - public function getTitle(): LanguageElement + public function getTitle(string $query = ''): ?LanguageElement { - return $this->title; + $xpath = './mods:title' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + return new LanguageElement($element->getValues()[0]); + } + return null; } /** - * Get the value of subTitle + * Get the value of the <subTitle> element. + * @see https://www.loc.gov/standards/mods/userguide/titleinfo.html#subtitle * * @access public * - * @return LanguageElement + * @param string $query The XPath query for metadata search + * + * @return ?LanguageElement */ - public function getSubTitle(): LanguageElement + public function getSubTitle(string $query = ''): ?LanguageElement { - return $this->subTitle; + $xpath = './mods:subTitle' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + return new LanguageElement($element->getValues()[0]); + } + return null; } /** - * Get the value of partNumber + * Get the value of the <partNumber> element. + * @see https://www.loc.gov/standards/mods/userguide/titleinfo.html#partnumber * * @access public * + * @param string $query The XPath query for metadata search + * * @return LanguageElement */ - public function getPartNumber(): LanguageElement + public function getPartNumber(string $query = ''): LanguageElement { - return $this->partNumber; + $xpath = './mods:partNumber' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + return new LanguageElement($element->getValues()[0]); + } + return null; } /** - * Get the value of partName + * Get the the array of the <partName> elements. + * @see https://www.loc.gov/standards/mods/userguide/titleinfo.html#partname * * @access public * - * @return LanguageElement + * @param string $query The XPath query for metadata search + * + * @return LanguageElement[] */ - public function getPartName(): LanguageElement + public function getPartName(string $query = ''): array { - return $this->partName; + $partNames = []; + $xpath = './mods:partName' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + foreach ($element->getValues() as $value) { + $partNames[] = new LanguageElement($value); + } + } + return $partNames; } /** - * Get the value of nonSort + * Get the value of the <nonSort> element. * * @access public * - * @return NonSort + * @param string $query The XPath query for metadata search + * + * @return ?NonSort */ - public function getNonSort(): NonSort + public function getNonSort(string $query = ''): ?NonSort { - return $this->nonSort; + $xpath = './mods:nonSort' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + return new NonSort($element->getValues()[0]); + } + return null; } } From 671721cfb1b0ecf347c3d897d1045d455fbefb32 Mon Sep 17 00:00:00 2001 From: Beatrycze Volk <beatrycze.volk@slub-dresden.de> Date: Tue, 9 Apr 2024 15:31:48 +0200 Subject: [PATCH 2/2] Add tests for titleInfo element --- tests/Mods/ModsReaderTest.php | 39 +++++++++++++++++++++++------------ tests/resources/mods_book.xml | 7 ++++++- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/tests/Mods/ModsReaderTest.php b/tests/Mods/ModsReaderTest.php index e368f55..62b3647 100644 --- a/tests/Mods/ModsReaderTest.php +++ b/tests/Mods/ModsReaderTest.php @@ -918,12 +918,26 @@ public function testGetTitleInfosForBookDocument() { $titleInfos = $this->bookReader->getTitleInfos(); self::assertNotEmpty($titleInfos); - self::assertEquals(1, count($titleInfos)); + self::assertEquals(2, count($titleInfos)); self::assertNotEmpty($titleInfos[0]->getValue()); - //self::assertNotEmpty($titleInfos[0]->getTitle()); - //self::assertNotEmpty($titleInfos[0]->getSubTitle()); + self::assertEquals('Sound and fury', $titleInfos[0]->getTitle()->getValue()); + self::assertEquals('the making of the punditocracy', $titleInfos[0]->getSubTitle()->getValue()); + } - // TODO: implement reading of elements + public function testGetTitleInfosByQueryForBookDocument() + { + $titleInfos = $this->bookReader->getTitleInfos('[@xml:lang="fr"]'); + self::assertNotEmpty($titleInfos); + self::assertEquals(1, count($titleInfos)); + self::assertNotEmpty($titleInfos[0]->getValue()); + self::assertNotEmpty($titleInfos[0]->getType()); + self::assertEquals('translated', $titleInfos[0]->getType()); + self::assertNotEmpty($titleInfos[0]->getNonSort()); + self::assertEquals('Le', $titleInfos[0]->getNonSort()->getValue()); + self::assertNotEmpty($titleInfos[0]->getTitle()); + self::assertEquals('bruit et la fureur', $titleInfos[0]->getTitle()->getValue()); + self::assertNotEmpty($titleInfos[0]->getSubTitle()); + self::assertEquals('la création de la punditocratie', $titleInfos[0]->getSubTitle()->getValue()); } public function testGetTitleInfosForSerialDocument() @@ -932,22 +946,21 @@ public function testGetTitleInfosForSerialDocument() self::assertNotEmpty($titleInfos); self::assertEquals(3, count($titleInfos)); self::assertNotEmpty($titleInfos[0]->getValue()); - //self::assertNotEmpty($titleInfos[0]->getTitle()); - //self::assertNotEmpty($titleInfos[0]->getSubTitle()); - - // TODO: implement reading of elements + self::assertNotEmpty($titleInfos[0]->getTitle()); + self::assertEquals('E-JASL', $titleInfos[0]->getTitle()->getValue()); + self::assertNotEmpty($titleInfos[0]->getSubTitle()); + self::assertEquals('the electronic journal of academic and special librarianship', $titleInfos[0]->getSubTitle()->getValue()); } public function testGetTitleInfosByQueryForSerialDocument() { - $titleInfos = $this->serialReader->getTitleInfos('[@type="alternative"]'); + $titleInfos = $this->serialReader->getTitleInfos('[@type="abbreviated"]'); self::assertNotEmpty($titleInfos); self::assertEquals(1, count($titleInfos)); self::assertNotEmpty($titleInfos[0]->getValue()); - //self::assertNotEmpty($titleInfos[0]->getTitle()); - //self::assertEmpty($titleInfos[0]->getSubTitle()); - - // TODO: implement reading of elements + self::assertEquals('E-JASL', $titleInfos[0]->getTitle()->getValue()); + self::assertNotEmpty($titleInfos[0]->getSubTitle()); + self::assertEquals('(Athabasca)', $titleInfos[0]->getSubTitle()->getValue()); } public function testGetNoTitleInfosByQueryForSerialDocument() diff --git a/tests/resources/mods_book.xml b/tests/resources/mods_book.xml index 77a97f9..4f49875 100644 --- a/tests/resources/mods_book.xml +++ b/tests/resources/mods_book.xml @@ -1,11 +1,16 @@ <?xml version="1.0" encoding="UTF-8"?> -<mods xmlns="http://www.loc.gov/mods/v3" +<mods xmlns="http://www.loc.gov/mods/v3" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:zs="http://docs.oasis-open.org/ns/search-ws/sruResponse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.8" xsi:schemaLocation="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-8.xsd"> <titleInfo> <title>Sound and fury the making of the punditocracy + + Le + bruit et la fureur + la création de la punditocratie + Alterman, Eric.