Skip to content

Commit

Permalink
Make RelatedItem to extend ModsReader
Browse files Browse the repository at this point in the history
  • Loading branch information
beatrycze-volk committed Jun 14, 2024
1 parent a2335db commit cf5d4e4
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 57 deletions.
62 changes: 62 additions & 0 deletions src/Mods/Attribute/Common/Attribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* Copyright (C) 2024 Saxon State and University Library Dresden
*
* This file is part of the php-mods-reader.
*
* @license GNU General Public License version 3 or later.
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Slub\Mods\Attribute\Common;

/**
* Trait for two general types of attributes
*/
trait Attribute
{

/**
* 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;
}
}
44 changes: 2 additions & 42 deletions src/Mods/Element/Common/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Slub\Mods\Element\Common;

use Slub\Mods\Attribute\Common\Attribute;
use Slub\Mods\Element\Xml\Element;

/**
Expand All @@ -21,6 +22,7 @@
*/
class BaseElement
{
use Attribute;

/**
* @access protected
Expand Down Expand Up @@ -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.
*
Expand Down
9 changes: 4 additions & 5 deletions src/Mods/Element/RelatedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Mods/ModsReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 28 additions & 8 deletions tests/Mods/ModsReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1075,23 +1075,43 @@ 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()
{
$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()
Expand Down

0 comments on commit cf5d4e4

Please sign in to comment.