Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make RelatedItem to extend ModsReader #18

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
40 changes: 32 additions & 8 deletions tests/Mods/ModsReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1075,23 +1075,47 @@ 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());

$localIdentifiers = $relatedItem->getIdentifiers('[@type="local"]');
self::assertNotEmpty($localIdentifiers);
self::assertEquals(3, count($localIdentifiers));
self::assertEquals('(OCoLC)41477508', $localIdentifiers[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());

$localIdentifiers = $relatedItem->getIdentifiers('[@type="local"]');
self::assertNotEmpty($localIdentifiers);
self::assertEquals(3, count($localIdentifiers));
self::assertEquals('(OCoLC)41477508', $localIdentifiers[0]->getValue());
}

public function testGetNoRelatedItemsByQueryForSerialDocument()
Expand Down
Loading