From cf5d4e41a02b453825f61695350dad3a47104ede Mon Sep 17 00:00:00 2001 From: Beatrycze Volk Date: Fri, 14 Jun 2024 14:40:10 +0200 Subject: [PATCH] Make `RelatedItem` to extend `ModsReader` --- src/Mods/Attribute/Common/Attribute.php | 62 +++++++++++++++++++++++++ src/Mods/Element/Common/BaseElement.php | 44 +----------------- src/Mods/Element/RelatedItem.php | 9 ++-- src/Mods/ModsReader.php | 4 +- tests/Mods/ModsReaderTest.php | 36 ++++++++++---- 5 files changed, 98 insertions(+), 57 deletions(-) create mode 100644 src/Mods/Attribute/Common/Attribute.php diff --git a/src/Mods/Attribute/Common/Attribute.php b/src/Mods/Attribute/Common/Attribute.php new file mode 100644 index 0000000..7a93986 --- /dev/null +++ b/src/Mods/Attribute/Common/Attribute.php @@ -0,0 +1,62 @@ +xml->attributes() != null) { + $value = $this->xml->attributes()->$attribute; + + if (!empty($value)) { + return $value; + } + } + return ''; + } + + /** + * Get the int value of attribute. + * + * @access protected + * + * @param string $attribute name + * + * @return int + */ + protected function getIntAttribute($attribute): int + { + if ($this->xml->attributes() != null) { + $value = $this->xml->attributes()->$attribute; + + if (!empty($value)) { + return (int) $value; + } + } + return 0; + } +} diff --git a/src/Mods/Element/Common/BaseElement.php b/src/Mods/Element/Common/BaseElement.php index c20b795..b8722be 100644 --- a/src/Mods/Element/Common/BaseElement.php +++ b/src/Mods/Element/Common/BaseElement.php @@ -12,6 +12,7 @@ namespace Slub\Mods\Element\Common; +use Slub\Mods\Attribute\Common\Attribute; use Slub\Mods\Element\Xml\Element; /** @@ -21,6 +22,7 @@ */ class BaseElement { + use Attribute; /** * @access protected @@ -54,48 +56,6 @@ public function getValue(): string return $this->xml[0]; } - /** - * Get the string value of attribute. - * - * @access protected - * - * @param string $attribute name - * - * @return string - */ - protected function getStringAttribute($attribute): string - { - if ($this->xml->attributes() != null) { - $value = $this->xml->attributes()->$attribute; - - if (!empty($value)) { - return $value; - } - } - return ''; - } - - /** - * Get the int value of attribute. - * - * @access protected - * - * @param string $attribute name - * - * @return int - */ - protected function getIntAttribute($attribute): int - { - if ($this->xml->attributes() != null) { - $value = $this->xml->attributes()->$attribute; - - if (!empty($value)) { - return (int) $value; - } - } - return 0; - } - /** * Get the array of the matching elements. * diff --git a/src/Mods/Element/RelatedItem.php b/src/Mods/Element/RelatedItem.php index 37185ff..85127cc 100644 --- a/src/Mods/Element/RelatedItem.php +++ b/src/Mods/Element/RelatedItem.php @@ -12,23 +12,22 @@ namespace Slub\Mods\Element; +use Slub\Mods\Attribute\Common\Attribute; use Slub\Mods\Attribute\Common\Linking\IdAttribute; use Slub\Mods\Attribute\Common\Linking\XlinkHrefAttribute; use Slub\Mods\Attribute\Common\Miscellaneous\DisplayLabelAttribute; use Slub\Mods\Attribute\Specific\OtherTypeAttribute; -use Slub\Mods\Element\Common\BaseElement; use Slub\Mods\Exception\IncorrectValueInAttributeException; +use Slub\Mods\ModsReader; /** * RelatedItem MODS metadata element class for the 'php-mods-reader' library. * * @access public */ -class RelatedItem extends BaseElement +class RelatedItem extends ModsReader { - use IdAttribute, XlinkHrefAttribute, DisplayLabelAttribute, OtherTypeAttribute; - - //TODO: all elements can appear + use Attribute, IdAttribute, XlinkHrefAttribute, DisplayLabelAttribute, OtherTypeAttribute; /** * @access private diff --git a/src/Mods/ModsReader.php b/src/Mods/ModsReader.php index 52e2bf7..b1d551e 100644 --- a/src/Mods/ModsReader.php +++ b/src/Mods/ModsReader.php @@ -43,10 +43,10 @@ class ModsReader { /** - * @access private + * @access protected * @var \SimpleXMLElement The metadata XML **/ - private $xml; + protected $xml; /** * This creates the MODS Reader for given XML diff --git a/tests/Mods/ModsReaderTest.php b/tests/Mods/ModsReaderTest.php index cb3a142..0da7f11 100644 --- a/tests/Mods/ModsReaderTest.php +++ b/tests/Mods/ModsReaderTest.php @@ -1075,11 +1075,21 @@ public function testGetRelatedItemsForSerialDocument() $relatedItems = $this->serialReader->getRelatedItems(); self::assertNotEmpty($relatedItems); self::assertEquals(1, count($relatedItems)); - self::assertNotEmpty($relatedItems[0]->getValue()); - self::assertNotEmpty($relatedItems[0]->getType()); - self::assertEquals('preceding', $relatedItems[0]->getType()); - // TODO: implement reading of elements + $relatedItem = $relatedItems[0]; + self::assertNotEmpty($relatedItem->getType()); + self::assertEquals('preceding', $relatedItem->getType()); + self::assertNotEmpty($relatedItem->getTitleInfos()); + self::assertEquals('Journal of southern academic and special librarianship', $relatedItem->getTitleInfos()[0]->getTitle()->getValue()); + self::assertNotEmpty($relatedItem->getOriginInfos()); + //TODO: implement back compatibility for 'publisher' element + //self::assertEquals('Journal of southern academic and special librarianship', $relatedItem->getOriginInfos()[0]->getPublisher()); + self::assertNotEmpty($relatedItem->getIdentifiers()); + self::assertEquals(4, count($relatedItem->getIdentifiers())); + self::assertEquals('1525-321X', $relatedItem->getIdentifiers()[0]->getValue()); + self::assertNotEmpty($relatedItem->getIdentifiers('[@type="local"]')); + self::assertEquals(3, count($relatedItem->getIdentifiers())); + self::assertEquals('(OCoLC)41477508', $relatedItem->getIdentifiers()[0]->getValue()); } public function testGetRelatedItemsByQueryForSerialDocument() @@ -1087,11 +1097,21 @@ public function testGetRelatedItemsByQueryForSerialDocument() $relatedItems = $this->serialReader->getRelatedItems('[./mods:identifier="1525-321X"]'); self::assertNotEmpty($relatedItems); self::assertEquals(1, count($relatedItems)); - self::assertNotEmpty($relatedItems[0]->getValue()); - self::assertNotEmpty($relatedItems[0]->getType()); - self::assertEquals('preceding', $relatedItems[0]->getType()); - // TODO: implement reading of elements + $relatedItem = $relatedItems[0]; + self::assertNotEmpty($relatedItem->getType()); + self::assertEquals('preceding', $relatedItem->getType()); + self::assertNotEmpty($relatedItem->getTitleInfos()); + self::assertEquals('Journal of southern academic and special librarianship', $relatedItem->getTitleInfos()[0]->getTitle()->getValue()); + self::assertNotEmpty($relatedItem->getOriginInfos()); + //TODO: implement back compatibility for 'publisher' element + //self::assertEquals('Journal of southern academic and special librarianship', $relatedItem->getOriginInfos()[0]->getPublisher()); + self::assertNotEmpty($relatedItem->getIdentifiers()); + self::assertEquals(4, count($relatedItem->getIdentifiers())); + self::assertEquals('1525-321X', $relatedItem->getIdentifiers()[0]->getValue()); + self::assertNotEmpty($relatedItem->getIdentifiers('[@type="local"]')); + self::assertEquals(3, count($relatedItem->getIdentifiers())); + self::assertEquals('(OCoLC)41477508', $relatedItem->getIdentifiers()[0]->getValue()); } public function testGetNoRelatedItemsByQueryForSerialDocument()