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; } } diff --git a/tests/Mods/ModsReaderTest.php b/tests/Mods/ModsReaderTest.php index 7ba18da..723d26b 100644 --- a/tests/Mods/ModsReaderTest.php +++ b/tests/Mods/ModsReaderTest.php @@ -970,12 +970,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() @@ -984,22 +998,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 84442df..d2a5a07 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.