diff --git a/src/Mods/Element/Name.php b/src/Mods/Element/Name.php index 32dda89..098656a 100644 --- a/src/Mods/Element/Name.php +++ b/src/Mods/Element/Name.php @@ -20,83 +20,21 @@ use Slub\Mods\Attribute\Common\Linking\XlinkHrefAttribute; use Slub\Mods\Attribute\Common\Miscellaneous\DisplayLabelAttribute; use Slub\Mods\Attribute\Common\Miscellaneous\UsageAttribute; -use Slub\Mods\Element\Common\AuthorityLanguageElement; -use Slub\Mods\Element\Common\BaseElement; use Slub\Mods\Element\Common\LanguageElement; use Slub\Mods\Element\Specific\Name\AlternativeName; -use Slub\Mods\Element\Specific\Name\NameIdentifier; -use Slub\Mods\Element\Specific\Name\NamePart; -use Slub\Mods\Element\Specific\Name\Role; +use Slub\Mods\Element\Specific\Name\BaseNameElement; use Slub\Mods\Element\Xml\Element; /** * Name MODS metadata element class for the 'php-mods-reader' library. + * @see https://www.loc.gov/standards/mods/userguide/name.html * * @access public */ -class Name extends BaseElement +class Name extends BaseNameElement { use AuthorityAttribute, LanguageAttribute, IdAttribute, XlinkHrefAttribute, AltRepGroupAttribute, NameTitleGroupAttribute, UsageAttribute, DisplayLabelAttribute; - /** - * @access private - * @var array - */ - private array $allowedTypes = [ - 'personal', - 'corporate', - 'conference', - 'family' - ]; - - /** - * @access private - * @var array - */ - private array $nameParts = []; - - /** - * @access private - * @var array - */ - private array $alternativeNames = []; - - /** - * @access private - * @var array - */ - private array $nameIdentifiers = []; - - /** - * @access private - * @var array - */ - private array $displayForms = []; - - /** - * @access private - * @var array - */ - private array $affiliations = []; - - /** - * @access private - * @var array - */ - private array $roles = []; - - /** - * @access private - * @var array - */ - private array $descriptions = []; - - /** - * @access private - * @var LanguageElement - */ - private LanguageElement $etal; - /** * This extracts the essential MODS metadata from XML * @@ -112,7 +50,8 @@ public function __construct(\SimpleXMLElement $xml) } /** - * Get the value of type + * Get the value of the 'type' attribute. + * @see https://www.loc.gov/standards/mods/userguide/name.html#type * * @access public * @@ -124,32 +63,8 @@ public function getType(): string } /** - * Get the value of namePart - * - * @access public - * - * @param string $query The XPath query for metadata search - * - * @return NamePart[] - */ - public function getNameParts(string $query = ''): array - { - if (empty($this->nameParts)) { - $xpath = './mods:namePart' . $query; - $element = new Element($this->xml, $xpath); - - if ($element->exists()) { - foreach ($element->getValues() as $value) { - $this->nameParts[] = new NamePart($value); - } - } - } - - return $this->nameParts; - } - - /** - * Get the value of alternativeName + * Get the the array of the elements. + * @see https://www.loc.gov/standards/mods/userguide/name.html#alternativename * * @access public * @@ -159,154 +74,34 @@ public function getNameParts(string $query = ''): array */ public function getAlternativeNames(string $query = ''): array { - if (empty($this->alternativeNames)) { - $xpath = './mods:alternativeName' . $query; - $element = new Element($this->xml, $xpath); - - if ($element->exists()) { - foreach ($element->getValues() as $value) { - $this->alternativeNames[] = new AlternativeName($value); - } - } - } - - return $this->alternativeNames; - } - - /** - * Get the value of nameIdentifier - * - * @access public - * - * @param string $query The XPath query for metadata search - * - * @return NameIdentifier - */ - public function getNameIdentifier(string $query): array - { - if (empty($this->nameIdentifiers)) { - $xpath = './mods:nameIdentifier' . $query; - $element = new Element($this->xml, $xpath); - - if ($element->exists()) { - foreach ($element->getValues() as $value) { - $this->nameIdentifiers[] = new NameIdentifier($value); - } - } - } - - return $this->nameIdentifiers; - } - - /** - * Get the value of displayForm - * - * @access public - * - * @param string $query The XPath query for metadata search - * - * @return LanguageElement[] - */ - public function getDisplayForms(string $query = ''): array - { - if (empty($this->displayForms)) { - $xpath = './mods:displayForm' . $query; - $element = new Element($this->xml, $xpath); - - if ($element->exists()) { - foreach ($element->getValues() as $value) { - $this->displayForms[] = new LanguageElement($value); - } + $alternativeNames = []; + $xpath = './mods:alternativeName' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + foreach ($element->getValues() as $value) { + $alternativeNames[] = new AlternativeName($value); } } - - return $this->displayForms; + return $alternativeNames; } /** - * Get the value of affiliation + * Get the value of the element. + * @see https://www.loc.gov/standards/mods/userguide/name.html#etal * * @access public * * @param string $query The XPath query for metadata search * - * @return AuthorityLanguageElement[] + * @return ?LanguageElement */ - public function getAffiliations(string $query = ''): array + public function getEtal(string $query = ''): ?LanguageElement { - if (empty($this->affiliations)) { - $xpath = './mods:affiliation' . $query; - $element = new Element($this->xml, $xpath); - - if ($element->exists()) { - foreach ($element->getValues() as $value) { - $this->affiliations[] = new AuthorityLanguageElement($value); - } - } + $xpath = './mods:etal' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + return new LanguageElement($element->getValues()[0]); } - - return $this->affiliations; - } - - /** - * Get the array of roles - * - * @access public - * - * @param string $query The XPath query for metadata search - * - * @return Role[] - */ - public function getRoles(string $query = ''): array - { - if (empty($this->roles)) { - $xpath = './mods:role' . $query; - $element = new Element($this->xml, $xpath); - - if ($element->exists()) { - foreach ($element->getValues() as $value) { - $this->roles[] = new AuthorityLanguageElement($value); - } - } - } - - return $this->roles; - } - - /** - * Get the value of description - * - * @access public - * - * @param string $query The XPath query for metadata search - * - * @return LanguageElement[] - */ - public function getDescriptions(string $query = ''): array - { - if (empty($this->descriptions)) { - $xpath = './mods:description' . $query; - $element = new Element($this->xml, $xpath); - - if ($element->exists()) { - foreach ($element->getValues() as $value) { - $this->descriptions[] = new LanguageElement($value); - } - } - } - - return $this->descriptions; - } - - /** - * Get the value of etal - * - * @access public - * - * @return LanguageElement - */ - public function getEtal(): LanguageElement - { - return $this->etal; + return null; } } diff --git a/src/Mods/Element/Specific/Name/AlternativeName.php b/src/Mods/Element/Specific/Name/AlternativeName.php index 6db11fe..400deb5 100644 --- a/src/Mods/Element/Specific/Name/AlternativeName.php +++ b/src/Mods/Element/Specific/Name/AlternativeName.php @@ -16,26 +16,17 @@ use Slub\Mods\Attribute\Common\Linking\IdAttribute; use Slub\Mods\Attribute\Common\Linking\XlinkHrefAttribute; use Slub\Mods\Attribute\Common\Miscellaneous\DisplayLabelAttribute; -use Slub\Mods\Element\Common\AuthorityLanguageElement; -use Slub\Mods\Element\Common\BaseElement; -use Slub\Mods\Element\Common\LanguageElement; -use Slub\Mods\Element\Specific\Name\NameIdentifier; /** * AlternativeName MODS metadata element class for the 'php-mods-reader' library. + * @see https://www.loc.gov/standards/mods/userguide/name.html#alternativename * * @access public */ -class AlternativeName extends BaseElement +class AlternativeName extends BaseNameElement { use LanguageAttribute, IdAttribute, XlinkHrefAttribute, DisplayLabelAttribute; - /** - * @access private - * @var string - */ - private string $alternativeType; - /** * @access private * @var array @@ -48,42 +39,6 @@ class AlternativeName extends BaseElement 'no specific type' ]; - /** - * @access private - * @var NamePart - */ - private NamePart $namePart; - - /** - * @access private - * @var NameIdentifier - */ - private NameIdentifier $nameIdentifier; - - /** - * @access private - * @var LanguageElement - */ - private LanguageElement $displayForm; - - /** - * @access private - * @var AuthorityLanguageElement - */ - private AuthorityLanguageElement $affiliation; - - /** - * @access private - * @var Role - */ - private Role $role; - - /** - * @access private - * @var LanguageElement - */ - private LanguageElement $description; - /** * This extracts the essential MODS metadata from XML * @@ -96,17 +51,11 @@ class AlternativeName extends BaseElement public function __construct(\SimpleXMLElement $xml) { parent::__construct($xml); - - $this->namePart = new NamePart($xml); - $this->nameIdentifier = new NameIdentifier($xml); - $this->displayForm = new LanguageElement($xml); - $this->affiliation = new AuthorityLanguageElement($xml); - $this->role = new Role($xml); - $this->description = new LanguageElement($xml); } /** - * Get the value of alternative type + * Get the value of the 'altType' attribute. + * @see https://www.loc.gov/standards/mods/userguide/name.html#altType * * @access public * @@ -114,78 +63,6 @@ public function __construct(\SimpleXMLElement $xml) */ public function getAlternativeType(): string { - return $this->getStringAttribute('alternativeType'); - } - - /** - * Get the value of namePart - * - * @access public - * - * @return NamePart - */ - public function getNamePart(): NamePart - { - return $this->namePart; - } - - /** - * Get the value of nameIdentifier - * - * @access public - * - * @return NameIdentifier - */ - public function getNameIdentifier(): NameIdentifier - { - return $this->nameIdentifier; - } - - /** - * Get the value of displayForm - * - * @access public - * - * @return LanguageElement - */ - public function getDisplayForm(): LanguageElement - { - return $this->displayForm; - } - - /** - * Get the value of affiliation - * - * @access public - * - * @return AuthorityLanguageElement - */ - public function getAffiliation(): AuthorityLanguageElement - { - return $this->affiliation; - } - - /** - * Get the value of role - * - * @access public - * - * @return Role - */ - public function getRole(): Role - { - return $this->role; - } - - /** - * Get the value of description - * - * @access public - * - * @return LanguageElement - */ - public function getDescription(): LanguageElement - { - return $this->description; + return $this->getStringAttribute('altType'); } } diff --git a/src/Mods/Element/Specific/Name/BaseNameElement.php b/src/Mods/Element/Specific/Name/BaseNameElement.php new file mode 100644 index 0000000..48854a2 --- /dev/null +++ b/src/Mods/Element/Specific/Name/BaseNameElement.php @@ -0,0 +1,178 @@ + elements. + * @see https://www.loc.gov/standards/mods/userguide/name.html#namepart + * + * @access public + * + * @param string $query The XPath query for metadata search + * + * @return NamePart[] + */ + public function getNameParts(string $query = ''): array + { + $nameParts = []; + $xpath = './mods:namePart' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + foreach ($element->getValues() as $value) { + $nameParts[] = new NamePart($value); + } + } + return $nameParts; + } + + /** + * Get the the array of the elements. + * @see https://www.loc.gov/standards/mods/userguide/name.html#nameidentifier + * + * @access public + * + * @param string $query The XPath query for metadata search + * + * @return NameIdentifier[] + */ + public function getNameIdentifiers(string $query): array + { + $nameIdentifiers = []; + $xpath = './mods:nameIdentifier' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + foreach ($element->getValues() as $value) { + $nameIdentifiers[] = new NameIdentifier($value); + } + } + return $nameIdentifiers; + } + + /** + * Get the the array of the elements. + * @see https://www.loc.gov/standards/mods/userguide/name.html#displayform + * + * @access public + * + * @param string $query The XPath query for metadata search + * + * @return LanguageElement[] + */ + public function getDisplayForms(string $query = ''): array + { + $displayForms = []; + $xpath = './mods:displayForm' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + foreach ($element->getValues() as $value) { + $displayForms[] = new LanguageElement($value); + } + } + return $displayForms; + } + + /** + * Get the the array of the elements. + * @see https://www.loc.gov/standards/mods/userguide/name.html#displayform + * + * @access public + * + * @param string $query The XPath query for metadata search + * + * @return AuthorityLanguageElement[] + */ + public function getAffiliations(string $query = ''): array + { + $affiliations = []; + $xpath = './mods:affiliation' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + foreach ($element->getValues() as $value) { + $affiliations[] = new AuthorityLanguageElement($value); + } + } + return $affiliations; + } + + /** + * Get the the array of the elements. + * @see https://www.loc.gov/standards/mods/userguide/name.html#role + * + * @access public + * + * @param string $query The XPath query ''):for metadata search + * + * @return Role[] + */ + public function getRoles(string $query = ''): array + { + $roles = []; + $xpath = './mods:role' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + foreach ($element->getValues() as $value) { + $roles[] = new Role($value); + } + } + return $roles; + } + + /** + * Get the the array of the elements. + * @see https://www.loc.gov/standards/mods/userguide/name.html#description + * + * @access public + * + * @param string $query The XPath query for metadata search + * + * @return LanguageElement[] + */ + public function getDescriptions(string $query = ''): array + { + $descriptions = []; + $xpath = './mods:description' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + foreach ($element->getValues() as $value) { + $descriptions[] = new LanguageElement($value); + } + } + return $descriptions; + } +} diff --git a/src/Mods/Element/Specific/Name/NameIdentifier.php b/src/Mods/Element/Specific/Name/NameIdentifier.php index 8fd5dcd..9d1ecc6 100644 --- a/src/Mods/Element/Specific/Name/NameIdentifier.php +++ b/src/Mods/Element/Specific/Name/NameIdentifier.php @@ -22,6 +22,7 @@ /** * NameIdentifier MODS metadata element class for the 'php-mods-reader' library. + * @see https://www.loc.gov/standards/mods/userguide/name.html#nameidentifier * * @access public */ @@ -44,7 +45,8 @@ public function __construct(\SimpleXMLElement $xml) } /** - * Get the value of type + * Get the value of the 'type' attribute. + * @see https://www.loc.gov/standards/mods/userguide/identifier.html#type * * @access public * diff --git a/src/Mods/Element/Specific/Name/NamePart.php b/src/Mods/Element/Specific/Name/NamePart.php index d16683a..a5edb1a 100644 --- a/src/Mods/Element/Specific/Name/NamePart.php +++ b/src/Mods/Element/Specific/Name/NamePart.php @@ -19,6 +19,7 @@ /** * NamePart MODS metadata element class for the 'php-mods-reader' library. + * @see https://www.loc.gov/standards/mods/userguide/name.html#namepart * * @access public */ @@ -52,7 +53,8 @@ public function __construct(\SimpleXMLElement $xml) } /** - * Get the value of type + * Get the value of the 'tpe' attribute. + * @see https://www.loc.gov/standards/mods/userguide/name.html#nameParttype * * @access public * @@ -60,11 +62,6 @@ public function __construct(\SimpleXMLElement $xml) */ public function getType(): string { - $value = $this->xml->attributes()->type; - - if (!empty($value)) { - return $value; - } - return ''; + return $this->getStringAttribute('type'); } } diff --git a/src/Mods/Element/Specific/Name/Role.php b/src/Mods/Element/Specific/Name/Role.php index e8319fc..151647f 100644 --- a/src/Mods/Element/Specific/Name/Role.php +++ b/src/Mods/Element/Specific/Name/Role.php @@ -18,18 +18,13 @@ /** * Role MODS metadata element class for the 'php-mods-reader' library. + * @see https://www.loc.gov/standards/mods/userguide/name.html#role * * @access public */ class Role extends BaseElement { - /** - * @access private - * @var array - */ - private array $roleTerms = []; - /** * This extracts the essential MODS metadata from XML * @@ -45,7 +40,8 @@ public function __construct(\SimpleXMLElement $xml) } /** - * Get the value of role term + * Get the the array of the elements. + * @see https://www.loc.gov/standards/mods/userguide/name.html#roleterm * * @access public * @@ -55,17 +51,14 @@ public function __construct(\SimpleXMLElement $xml) */ public function getRoleTerms(string $query = ''): array { - if (empty($this->roleTerms)) { - $xpath = './mods:roleTerm' . $query; - $element = new Element($this->xml, $xpath); - - if ($element->exists()) { - foreach ($element->getValues() as $value) { - $this->roleTerms[] = new RoleTerm($value); - } + $roleTerms = []; + $xpath = './mods:roleTerm' . $query; + $element = new Element($this->xml, $xpath); + if ($element->exists()) { + foreach ($element->getValues() as $value) { + $roleTerms[] = new RoleTerm($value); } } - - return $this->roleTerms; + return $roleTerms; } } diff --git a/src/Mods/Element/Specific/Name/RoleTerm.php b/src/Mods/Element/Specific/Name/RoleTerm.php index 9702f22..c376471 100644 --- a/src/Mods/Element/Specific/Name/RoleTerm.php +++ b/src/Mods/Element/Specific/Name/RoleTerm.php @@ -19,6 +19,7 @@ /** * RoleTerm MODS metadata element class for the 'php-mods-reader' library. + * @see https://www.loc.gov/standards/mods/userguide/name.html#roleterm * * @access public */ diff --git a/tests/Mods/ModsReaderTest.php b/tests/Mods/ModsReaderTest.php index 723d26b..346f066 100644 --- a/tests/Mods/ModsReaderTest.php +++ b/tests/Mods/ModsReaderTest.php @@ -537,7 +537,46 @@ public function testGetNamesForBookDocument() $names = $this->bookReader->getNames(); self::assertNotEmpty($names); self::assertEquals(2, count($names)); + self::assertNotEmpty($names[0]->getType()); + self::assertEquals('personal', $names[0]->getType()); + self::assertNotEmpty($names[0]->getUsage()); + self::assertEquals('primary', $names[0]->getUsage()); + self::assertNotEmpty($names[0]->getValue()); + self::assertNotEmpty($names[0]->getNameParts()); + self::assertEquals('Alterman, Eric.', $names[0]->getNameParts()[0]->getValue()); + $roles = $names[0]->getRoles(); + self::assertNotEmpty($roles); + self::assertNotEmpty($roles[0]->getRoleTerms()); + self::assertEquals('text', $roles[0]->getRoleTerms()[0]->getType()); + self::assertEquals('marcrelator', $roles[0]->getRoleTerms()[0]->getAuthority()); + self::assertEquals('creator', $roles[0]->getRoleTerms()[0]->getValue()); + } + + public function testGetNamesByQueryForBookDocument() + { + $names = $this->bookReader->getNames('[@type="personal" and not(@usage="primary")]'); + self::assertNotEmpty($names); + self::assertEquals(1, count($names)); + self::assertNotEmpty($names[0]->getType()); + self::assertEquals('personal', $names[0]->getType()); + self::assertEmpty($names[0]->getUsage()); + self::assertNotEmpty($names[0]->getValue()); + self::assertNotEmpty($names[0]->getNameParts()); + self::assertEquals(2, count($names[0]->getNameParts())); + self::assertEquals('given', $names[0]->getNameParts()[0]->getType()); + self::assertEquals('Aron', $names[0]->getNameParts()[0]->getValue()); + $roles = $names[0]->getRoles(); + self::assertNotEmpty($roles); + self::assertNotEmpty($roles[0]->getRoleTerms()); + self::assertEquals('text', $roles[0]->getRoleTerms()[0]->getType()); + self::assertEquals('marcrelator', $roles[0]->getRoleTerms()[0]->getAuthority()); + self::assertEquals('author', $roles[0]->getRoleTerms()[0]->getValue()); + } + public function testGetNoNamesByQueryForBookDocument() + { + $names = $this->bookReader->getNames('[@type="corporate"]'); + self::assertEmpty($names); } public function testGetNamesForSerialDocument() @@ -545,7 +584,27 @@ public function testGetNamesForSerialDocument() $names = $this->serialReader->getNames(); self::assertNotEmpty($names); self::assertEquals(1, count($names)); + self::assertNotEmpty($names[0]->getValue()); + self::assertNotEmpty($names[0]->getNameParts()); + self::assertEquals(1, count($names[0]->getNameParts())); + self::assertEquals('International Consortium for the Advancement of Academic Publication.', $names[0]->getNameParts()[0]->getValue()); + } + public function testGetNamesByQueryForSerialDocument() + { + $names = $this->serialReader->getNames('[@type="corporate"]'); + self::assertNotEmpty($names); + self::assertEquals(1, count($names)); + self::assertNotEmpty($names[0]->getValue()); + self::assertNotEmpty($names[0]->getNameParts()); + self::assertEquals(1, count($names[0]->getNameParts())); + self::assertEquals('International Consortium for the Advancement of Academic Publication.', $names[0]->getNameParts()[0]->getValue()); + } + + public function testGetNoNamesByQueryForSerialDocument() + { + $names = $this->serialReader->getNames('[@type="personal"]'); + self::assertEmpty($names); } public function testGetNotesForBookDocument() diff --git a/tests/resources/mods_book.xml b/tests/resources/mods_book.xml index d2a5a07..a656b47 100644 --- a/tests/resources/mods_book.xml +++ b/tests/resources/mods_book.xml @@ -14,7 +14,7 @@ Alterman, Eric. - creator + creator cre