Skip to content

Commit

Permalink
Merge pull request #5 from beatrycze-volk/implement-titleinfo
Browse files Browse the repository at this point in the history
Implement functions in TitleInfo class
  • Loading branch information
beatrycze-volk authored Apr 10, 2024
2 parents c39e2ba + 671721c commit 3988fcc
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 70 deletions.
1 change: 1 addition & 0 deletions src/Mods/Element/Specific/TitleInfo/NonSort.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
121 changes: 65 additions & 56 deletions src/Mods/Element/TitleInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
*
Expand All @@ -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
*
Expand All @@ -111,62 +78,104 @@ public function getType(): string
}

/**
* Get the value of title
* Get the value of the <title> 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;
}
}
39 changes: 26 additions & 13 deletions tests/Mods/ModsReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
7 changes: 6 additions & 1 deletion tests/resources/mods_book.xml
Original file line number Diff line number Diff line change
@@ -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</title>
<subTitle>the making of the punditocracy</subTitle>
</titleInfo>
<titleInfo xml:lang="fr" type="translated">
<nonSort>Le</nonSort>
<title>bruit et la fureur</title>
<subTitle>la création de la punditocratie</subTitle>
</titleInfo>
<name type="personal" usage="primary">
<namePart>Alterman, Eric.</namePart>
<role>
Expand Down

0 comments on commit 3988fcc

Please sign in to comment.