Skip to content

Commit

Permalink
Merge pull request #3 from beatrycze-volk/implement-language
Browse files Browse the repository at this point in the history
Implement Language element
  • Loading branch information
beatrycze-volk authored Apr 9, 2024
2 parents 1462fad + d115ff6 commit 8355ee4
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 49 deletions.
55 changes: 34 additions & 21 deletions src/Mods/Element/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,18 @@
use Slub\Mods\Element\Common\BaseElement;
use Slub\Mods\Element\Specific\Language\LanguageTerm;
use Slub\Mods\Element\Specific\Language\ScriptTerm;
use Slub\Mods\Element\Xml\Element;

/**
* Language MODS metadata element class for the 'php-mods-reader' library.
* @see https://www.loc.gov/standards/mods/userguide/language.html
*
* @access public
*/
class Language extends BaseElement
{
use LanguageAttribute, IdAttribute, AltRepGroupAttribute, DisplayLabelAttribute, UsageAttribute;

/**
* @access private
* @var LanguageTerm
*/
private LanguageTerm $languageTerm;

/**
* @access private
* @var ScriptTerm
*/
private ScriptTerm $scriptTerm;

/**
* This extracts the essential MODS metadata from XML
*
Expand All @@ -57,7 +47,8 @@ public function __construct(\SimpleXMLElement $xml)
}

/**
* Get the value of objectPart
* Get the value of the 'objectPart' attribute.
* @see https://www.loc.gov/standards/mods/userguide/language.html#objectpart
*
* @access public
*
Expand All @@ -69,26 +60,48 @@ public function getObjectPart(): string
}

/**
* Get the value of languageTerm
* Get the array of the <languageTerm> elements.
* @see https://www.loc.gov/standards/mods/userguide/language.html#languageterm
*
* @access public
*
* @return LanguageTerm
* @param string $query The XPath query for metadata search
*
* @return LanguageTerm[]
*/
public function getLanguageTerm(): LanguageTerm
public function getLanguageTerms(string $query = ''): array
{
return $this->languageTerm;
$languageTerms = [];
$xpath = './mods:languageTerm' . $query;
$element = new Element($this->xml, $xpath);
if ($element->exists()) {
foreach ($element->getValues() as $value) {
$languageTerms[] = new LanguageTerm($value);
}
}
return $languageTerms;
}

/**
* Get the value of scriptTerm
* Get the array of the <scriptTerm> elements.
* @see https://www.loc.gov/standards/mods/userguide/language.html#scriptterm
*
* @access public
*
* @return ScriptTerm
* @param string $query The XPath query for metadata search
*
* @return ScriptTerm[]
*/
public function getScriptTerm(): ScriptTerm
public function getScriptTerms(string $query = ''): array
{
return $this->scriptTerm;
$scriptTerms = [];
$xpath = './mods:scriptTerm' . $query;
$element = new Element($this->xml, $xpath);
if ($element->exists()) {
foreach ($element->getValues() as $value) {
$scriptTerms[] = new ScriptTerm($value);
}
}
return $scriptTerms;
}
}
14 changes: 9 additions & 5 deletions src/Mods/ModsReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,26 @@ public function getIdentifiers(string $query = ''): array
}

/**
* Get
* Get the array of the <language> elements.
* @see https://www.loc.gov/standards/mods/userguide/language.html
*
* @access public
*
* @param string $query The XPath query for metadata search
*
* @return ?Language
* @return Language[]
*/
public function getLanguage(string $query = ''): ?Language
public function getLanguages(string $query = ''): array
{
$languages = [];
$xpath = './mods:language' . $query;
$element = new Element($this->xml, $xpath);
if ($element->exists()) {
return new Language($element->getValues()[0]);
foreach ($element->getValues() as $value) {
$languages[] = new Language($value);
}
}
return null;
return $languages;
}

/**
Expand Down
99 changes: 77 additions & 22 deletions tests/Mods/ModsReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,28 +379,83 @@ public function testGetNoIdentifiersByQueryForSerialDocument()
self::assertEmpty($identifiers);
}

public function testGetLanguageForBookDocument()
{
$language = $this->bookReader->getLanguage();
self::assertNotNull($language);
// TODO: implement reading of languageTerm and scriptTerm
// self::assertNotEmpty($language->getLanguageTerm());
// self::assertNotEmpty($language->getScriptTerm());

$language = $this->bookReader->getLanguage('[@type="text"]');
self::assertNull($language);
}

public function testGetLanguageForSerialDocument()
{
$language = $this->serialReader->getLanguage();
self::assertNotNull($language);
// TODO: implement reading of languageTerm and scriptTerm
// self::assertNotEmpty($language->getLanguageTerm());
// self::assertNotEmpty($language->getScriptTerm());

$language = $this->serialReader->getLanguage('[@type="text"]');
self::assertNull($language);
public function testGetLanguagesForBookDocument()
{
$languages = $this->bookReader->getLanguages();
self::assertNotEmpty($languages);
self::assertEquals(2, count($languages));
self::assertEmpty($languages[0]->getObjectPart());
self::assertNotEmpty($languages[0]->getValue());
self::assertNotEmpty($languages[0]->getLanguageTerms());
self::assertNotEmpty($languages[0]->getLanguageTerms()[0]->getValue());
self::assertEquals('code', $languages[0]->getLanguageTerms()[0]->getType());
self::assertEquals('iso639-2b', $languages[0]->getLanguageTerms()[0]->getAuthority());
self::assertEquals('eng', $languages[0]->getLanguageTerms()[0]->getValue());
self::assertNotEmpty($languages[0]->getScriptTerms());
self::assertEquals('code', $languages[0]->getScriptTerms()[0]->getType());
self::assertEquals('iso15924', $languages[0]->getScriptTerms()[0]->getAuthority());
self::assertEquals('Latn', $languages[0]->getScriptTerms()[0]->getValue());
}

public function testGetLanguagesByQueryForBookDocument()
{
$languages = $this->bookReader->getLanguages('[@objectPart="summary"]');
self::assertNotEmpty($languages);
self::assertEquals(1, count($languages));
self::assertNotEmpty($languages[0]->getObjectPart());
self::assertEquals('summary', $languages[0]->getObjectPart());
self::assertNotEmpty($languages[0]->getValue());
self::assertNotEmpty($languages[0]->getLanguageTerms());
self::assertNotEmpty($languages[0]->getLanguageTerms()[0]->getValue());
self::assertEquals('code', $languages[0]->getLanguageTerms()[0]->getType());
self::assertEquals('iso639-2b', $languages[0]->getLanguageTerms()[0]->getAuthority());
self::assertEquals('spa', $languages[0]->getLanguageTerms()[0]->getValue());
self::assertNotEmpty($languages[0]->getScriptTerms());
self::assertEquals('code', $languages[0]->getScriptTerms()[0]->getType());
self::assertEquals('iso15924', $languages[0]->getScriptTerms()[0]->getAuthority());
self::assertEquals('Latn', $languages[0]->getScriptTerms()[0]->getValue());
}

public function testGetNoLanguagesByQueryForBookDocument()
{
$languages = $this->bookReader->getLanguages('[@objectPart="abstract"]');
self::assertEmpty($languages);
}

public function testGetLanguagesForSerialDocument()
{
$languages = $this->serialReader->getLanguages();
self::assertNotEmpty($languages);
self::assertEquals(1, count($languages));
self::assertEmpty($languages[0]->getObjectPart());
self::assertNotEmpty($languages[0]->getValue());
self::assertNotEmpty($languages[0]->getLanguageTerms());
self::assertNotEmpty($languages[0]->getLanguageTerms()[0]->getValue());
self::assertEquals('code', $languages[0]->getLanguageTerms()[0]->getType());
self::assertEquals('iso639-2b', $languages[0]->getLanguageTerms()[0]->getAuthority());
self::assertEquals('eng', $languages[0]->getLanguageTerms()[0]->getValue());
self::assertEmpty($languages[0]->getScriptTerms());
}

public function testGetLanguagesByQueryForSerialDocument()
{
$languages = $this->serialReader->getLanguages('[./mods:languageTerm[@type="code"]]');
self::assertNotEmpty($languages);
self::assertEquals(1, count($languages));
self::assertEmpty($languages[0]->getObjectPart());
self::assertNotEmpty($languages[0]->getValue());
self::assertNotEmpty($languages[0]->getLanguageTerms());
self::assertNotEmpty($languages[0]->getLanguageTerms()[0]->getValue());
self::assertEquals('code', $languages[0]->getLanguageTerms()[0]->getType());
self::assertEquals('iso639-2b', $languages[0]->getLanguageTerms()[0]->getAuthority());
self::assertEquals('eng', $languages[0]->getLanguageTerms()[0]->getValue());
self::assertEmpty($languages[0]->getScriptTerms());
}

public function testGetNoLanguagesByQueryForSerialDocument()
{
$languages = $this->serialReader->getLanguages('[@objectPart="summary"]');
self::assertEmpty($languages);
}

public function testGetLocationsForBookDocument()
Expand Down
7 changes: 6 additions & 1 deletion tests/resources/mods_book.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@
<dateIssued>c1999</dateIssued>
</originInfo>
<language>
<languageTerm authority="iso639-2b" type="code">eng</languageTerm>
<languageTerm type="code" authority="iso639-2b">eng</languageTerm>
<scriptTerm type="code" authority="iso15924">Latn</scriptTerm>
</language>
<language objectPart="summary">
<languageTerm type="code" authority="iso639-2b">spa</languageTerm>
<scriptTerm type="code" authority="iso15924">Latn</scriptTerm>
</language>
<physicalDescription>
<form authority="marcform">print</form>
Expand Down

0 comments on commit 8355ee4

Please sign in to comment.