From 5334f87708c20416fb616d0f320c629e811a5eb3 Mon Sep 17 00:00:00 2001 From: Beatrycze Volk Date: Tue, 16 Apr 2024 17:21:40 +0200 Subject: [PATCH 1/3] Add `IncorrectValueInAttributeException` --- .../IncorrectValueInAttributeException.php | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/Mods/Exception/IncorrectValueInAttributeException.php diff --git a/src/Mods/Exception/IncorrectValueInAttributeException.php b/src/Mods/Exception/IncorrectValueInAttributeException.php new file mode 100644 index 0000000..f25a66b --- /dev/null +++ b/src/Mods/Exception/IncorrectValueInAttributeException.php @@ -0,0 +1,34 @@ + Date: Tue, 16 Apr 2024 17:22:25 +0200 Subject: [PATCH 2/3] Use the custom exception for validating attributes --- src/Mods/Attribute/Common/DateAttribute.php | 47 +++++++++++++++---- .../Common/Miscellaneous/UsageAttribute.php | 15 +++++- src/Mods/Attribute/Specific/TypeAttribute.php | 14 +++++- src/Mods/Element/RelatedItem.php | 14 +++++- src/Mods/Element/Specific/Location/Url.php | 28 +++++++++-- .../Element/Specific/Name/AlternativeName.php | 11 ++++- src/Mods/Element/Specific/Name/NamePart.php | 13 ++++- .../Element/Specific/OriginInfo/Agent.php | 15 +++++- .../Element/Specific/TitleInfo/NonSort.php | 14 +++++- src/Mods/Element/TitleInfo.php | 11 ++++- 10 files changed, 155 insertions(+), 27 deletions(-) diff --git a/src/Mods/Attribute/Common/DateAttribute.php b/src/Mods/Attribute/Common/DateAttribute.php index a3a090b..8eb03db 100644 --- a/src/Mods/Attribute/Common/DateAttribute.php +++ b/src/Mods/Attribute/Common/DateAttribute.php @@ -12,6 +12,8 @@ namespace Slub\Mods\Attribute\Common; +use Slub\Mods\Exception\IncorrectValueInAttributeException; + trait DateAttribute { @@ -47,31 +49,50 @@ trait DateAttribute ]; /** - * Get the value of encoding + * Get the value of the 'encoding' attribute. + * @see https://www.loc.gov/standards/mods/userguide/attributes.html#encoding * * @access public * * @return string + * + * @throws IncorrectValueInAttributeException */ public function getEncoding(): string { - return $this->getStringAttribute('encoding'); + $encoding = $this->getStringAttribute('encoding'); + + if (empty($encoding) || in_array($encoding, $this->allowedEncodings)) { + return $encoding; + } + + throw new IncorrectValueInAttributeException('encoding', $encoding); } /** - * Get the value of point + * Get the value of the 'point' attribute. + * @see https://www.loc.gov/standards/mods/userguide/attributes.html#point * * @access public * * @return string + * + * @throws IncorrectValueInAttributeException */ public function getPoint(): string { - return $this->getStringAttribute('point'); + $point = $this->getStringAttribute('point'); + + if (empty($point) || in_array($point, $this->allowedPoints)) { + return $point; + } + + throw new IncorrectValueInAttributeException('point', $point); } /** - * Get the value of keyDate + * Get the value of the 'keyDate' attribute. + * @see https://www.loc.gov/standards/mods/userguide/attributes.html#keyDate * * @access public * @@ -83,19 +104,29 @@ public function isKeyDate(): bool } /** - * Get the value of qualifier + * Get the value of the 'qualifier' attribute. + * @see https://www.loc.gov/standards/mods/userguide/attributes.html#qualifier * * @access public * * @return string + * + * @throws IncorrectValueInAttributeException */ public function getQualifier(): string { - return $this->getStringAttribute('qualifier'); + $qualifier = $this->getStringAttribute('qualifier'); + + if (empty($qualifier) || in_array($qualifier, $this->allowedQualifiers)) { + return $qualifier; + } + + throw new IncorrectValueInAttributeException('qualifier', $qualifier); } /** - * Get the value of calendar + * Get the value of the 'calendar' attribute. + * @see https://www.loc.gov/standards/mods/userguide/attributes.html#calendar * * @access public * diff --git a/src/Mods/Attribute/Common/Miscellaneous/UsageAttribute.php b/src/Mods/Attribute/Common/Miscellaneous/UsageAttribute.php index 5a67daf..51bc020 100644 --- a/src/Mods/Attribute/Common/Miscellaneous/UsageAttribute.php +++ b/src/Mods/Attribute/Common/Miscellaneous/UsageAttribute.php @@ -12,6 +12,8 @@ namespace Slub\Mods\Attribute\Common\Miscellaneous; +use Slub\Mods\Exception\IncorrectValueInAttributeException; + /** * Trait for usage common attribute */ @@ -28,14 +30,23 @@ trait UsageAttribute ]; /** - * Get the value of usage + * Get the value of the 'usage' attribute. + * @see https://www.loc.gov/standards/mods/userguide/attributes.html#usage * * @access public * * @return string + * + * @throws IncorrectValueInAttributeException */ public function getUsage(): string { - return $this->getStringAttribute('usage'); + $usage = $this->getStringAttribute('usage'); + + if (empty($usage) || in_array($usage, $this->allowedUsages)) { + return $usage; + } + + throw new IncorrectValueInAttributeException('usage', $usage); } } diff --git a/src/Mods/Attribute/Specific/TypeAttribute.php b/src/Mods/Attribute/Specific/TypeAttribute.php index 85c097f..1145974 100644 --- a/src/Mods/Attribute/Specific/TypeAttribute.php +++ b/src/Mods/Attribute/Specific/TypeAttribute.php @@ -12,6 +12,8 @@ namespace Slub\Mods\Attribute\Specific; +use Slub\Mods\Exception\IncorrectValueInAttributeException; + /** * Trait for type specific attribute */ @@ -28,14 +30,22 @@ trait TypeAttribute ]; /** - * Get the value of type + * Get the value of the 'type' attribute. * * @access public * * @return string + * + * @throws IncorrectValueInAttributeException */ public function getType(): string { - return $this->getStringAttribute('type'); + $type = $this->getStringAttribute('type'); + + if (empty($type) || in_array($type, $this->allowedTypes)) { + return $type; + } + + throw new IncorrectValueInAttributeException('type', $type); } } diff --git a/src/Mods/Element/RelatedItem.php b/src/Mods/Element/RelatedItem.php index 027d8ff..37185ff 100644 --- a/src/Mods/Element/RelatedItem.php +++ b/src/Mods/Element/RelatedItem.php @@ -17,6 +17,7 @@ use Slub\Mods\Attribute\Common\Miscellaneous\DisplayLabelAttribute; use Slub\Mods\Attribute\Specific\OtherTypeAttribute; use Slub\Mods\Element\Common\BaseElement; +use Slub\Mods\Exception\IncorrectValueInAttributeException; /** * RelatedItem MODS metadata element class for the 'php-mods-reader' library. @@ -62,14 +63,23 @@ 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/relateditem.html#type * * @access public * * @return string + * + * @throws IncorrectValueInAttributeException */ public function getType(): string { - return $this->getStringAttribute('type'); + $type = $this->getStringAttribute('type'); + + if (empty($type) || in_array($type, $this->allowedTypes)) { + return $type; + } + + throw new IncorrectValueInAttributeException('type', $type); } } diff --git a/src/Mods/Element/Specific/Location/Url.php b/src/Mods/Element/Specific/Location/Url.php index 4a891f5..0491db1 100644 --- a/src/Mods/Element/Specific/Location/Url.php +++ b/src/Mods/Element/Specific/Location/Url.php @@ -15,6 +15,7 @@ use Slub\Mods\Attribute\Common\Miscellaneous\DisplayLabelAttribute; use Slub\Mods\Attribute\Common\Miscellaneous\UsageAttribute; use Slub\Mods\Element\Common\BaseElement; +use Slub\Mods\Exception\IncorrectValueInAttributeException; /** * Url MODS metadata element class for the 'php-mods-reader' library. @@ -50,19 +51,28 @@ public function __construct(\SimpleXMLElement $xml) } /** - * Get the value of dateLastAccessed + * Get the value of the 'dateLastAccessed' attribute. + * @see https://www.loc.gov/standards/mods/userguide/location.html#datelastaccessed * * @access public * * @return string + * + * @throws IncorrectValueInAttributeException */ public function getDateLastAccessed(): string { - return $this->getStringAttribute('dateLastAccessed'); + $dateLastAccessed = $this->getStringAttribute('dateLastAccessed'); + + if (empty($dateLastAccessed) || in_array($dateLastAccessed, $this->allowedAccess)) { + return $dateLastAccessed; + } + + throw new IncorrectValueInAttributeException('dateLastAccessed', $dateLastAccessed); } /** - * Get the value of note + * Get the value of the 'note' attribute. * * @access public * @@ -74,14 +84,22 @@ public function getNote(): string } /** - * Get the value of access + * Get the value of the 'access' attribute. * * @access public * * @return string + * + * @throws IncorrectValueInAttributeException */ public function getAccess(): string { - return $this->getStringAttribute('access'); + $access = $this->getStringAttribute('access'); + + if (empty($access) || in_array($access, $this->allowedAccess)) { + return $access; + } + + throw new IncorrectValueInAttributeException('access', $access); } } diff --git a/src/Mods/Element/Specific/Name/AlternativeName.php b/src/Mods/Element/Specific/Name/AlternativeName.php index 400deb5..b4a9f12 100644 --- a/src/Mods/Element/Specific/Name/AlternativeName.php +++ b/src/Mods/Element/Specific/Name/AlternativeName.php @@ -16,6 +16,7 @@ use Slub\Mods\Attribute\Common\Linking\IdAttribute; use Slub\Mods\Attribute\Common\Linking\XlinkHrefAttribute; use Slub\Mods\Attribute\Common\Miscellaneous\DisplayLabelAttribute; +use Slub\Mods\Exception\IncorrectValueInAttributeException; /** * AlternativeName MODS metadata element class for the 'php-mods-reader' library. @@ -60,9 +61,17 @@ public function __construct(\SimpleXMLElement $xml) * @access public * * @return string + * + * @throws IncorrectValueInAttributeException */ public function getAlternativeType(): string { - return $this->getStringAttribute('altType'); + $altType = $this->getStringAttribute('altType'); + + if (empty($altType) || in_array($altType, $this->allowedAlternativeTypes)) { + return $altType; + } + + throw new IncorrectValueInAttributeException('altType', $altType); } } diff --git a/src/Mods/Element/Specific/Name/NamePart.php b/src/Mods/Element/Specific/Name/NamePart.php index a5edb1a..c477165 100644 --- a/src/Mods/Element/Specific/Name/NamePart.php +++ b/src/Mods/Element/Specific/Name/NamePart.php @@ -16,6 +16,7 @@ use Slub\Mods\Attribute\Common\Linking\AltRepGroupAttribute; use Slub\Mods\Attribute\Common\Miscellaneous\DisplayLabelAttribute; use Slub\Mods\Element\Common\BaseElement; +use Slub\Mods\Exception\IncorrectValueInAttributeException; /** * NamePart MODS metadata element class for the 'php-mods-reader' library. @@ -53,15 +54,23 @@ public function __construct(\SimpleXMLElement $xml) } /** - * Get the value of the 'tpe' attribute. + * Get the value of the 'type' attribute. * @see https://www.loc.gov/standards/mods/userguide/name.html#nameParttype * * @access public * * @return string + * + * @throws IncorrectValueInAttributeException */ public function getType(): string { - return $this->getStringAttribute('type'); + $type = $this->getStringAttribute('type'); + + if (empty($type) || in_array($type, $this->allowedTypes)) { + return $type; + } + + throw new IncorrectValueInAttributeException('type', $type); } } diff --git a/src/Mods/Element/Specific/OriginInfo/Agent.php b/src/Mods/Element/Specific/OriginInfo/Agent.php index a1e2058..418bb9c 100644 --- a/src/Mods/Element/Specific/OriginInfo/Agent.php +++ b/src/Mods/Element/Specific/OriginInfo/Agent.php @@ -27,9 +27,11 @@ use Slub\Mods\Element\Specific\Name\NameIdentifier; use Slub\Mods\Element\Specific\Name\NamePart; use Slub\Mods\Element\Specific\Name\Role; +use Slub\Mods\Exception\IncorrectValueInAttributeException; /** * Agent MODS metadata element class for the 'php-mods-reader' library. + * @see https://www.loc.gov/standards/mods/userguide/origininfo.html#agent * * @access public */ @@ -120,15 +122,24 @@ 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 * * @return string + * + * @throws IncorrectValueInAttributeException */ public function getType(): string { - return $this->getStringAttribute('type'); + $type = $this->getStringAttribute('type'); + + if (empty($type) || in_array($type, $this->allowedTypes)) { + return $type; + } + + throw new IncorrectValueInAttributeException('type', $type); } /** diff --git a/src/Mods/Element/Specific/TitleInfo/NonSort.php b/src/Mods/Element/Specific/TitleInfo/NonSort.php index bf6af03..220cb01 100644 --- a/src/Mods/Element/Specific/TitleInfo/NonSort.php +++ b/src/Mods/Element/Specific/TitleInfo/NonSort.php @@ -14,6 +14,7 @@ use Slub\Mods\Attribute\Common\LanguageAttribute; use Slub\Mods\Element\Common\BaseElement; +use Slub\Mods\Exception\IncorrectValueInAttributeException; /** * NonSort MODS metadata element class for the 'php-mods-reader' library. @@ -49,14 +50,23 @@ public function __construct(\SimpleXMLElement $xml) } /** - * Get the value of xmlSpace + * Get the value of the 'xmlSpace' attribute. + * @see https://www.loc.gov/standards/mods/userguide/attributes.html#xmlspace * * @access public * * @return string + * + * @throws IncorrectValueInAttributeException */ public function getXmlSpace(): string { - return $this->getStringAttribute('xmlSpace'); + $xmlSpace = $this->getStringAttribute('xml:space'); + + if (empty($xmlSpace) || in_array($xmlSpace, $this->allowedXmlSpaces)) { + return $xmlSpace; + } + + throw new IncorrectValueInAttributeException('xml:space', $xmlSpace); } } diff --git a/src/Mods/Element/TitleInfo.php b/src/Mods/Element/TitleInfo.php index d5d57fe..696cb5f 100644 --- a/src/Mods/Element/TitleInfo.php +++ b/src/Mods/Element/TitleInfo.php @@ -28,6 +28,7 @@ use Slub\Mods\Element\Common\LanguageElement; use Slub\Mods\Element\Specific\TitleInfo\NonSort; use Slub\Mods\Element\Xml\Element; +use Slub\Mods\Exception\IncorrectValueInAttributeException; /** * TitleInfo MODS metadata element class for the 'php-mods-reader' library. @@ -71,10 +72,18 @@ public function __construct(\SimpleXMLElement $xml) * @access public * * @return string + * + * @throws IncorrectValueInAttributeException */ public function getType(): string { - return $this->getStringAttribute('type'); + $type = $this->getStringAttribute('type'); + + if (empty($type) || in_array($type, $this->allowedTypes)) { + return $type; + } + + throw new IncorrectValueInAttributeException('type', $type); } /** From c75ce69ff1892300902a483311f366a766d686a0 Mon Sep 17 00:00:00 2001 From: Beatrycze Volk Date: Tue, 16 Apr 2024 18:12:20 +0200 Subject: [PATCH 3/3] Add tests for exception --- src/Mods/Element/Specific/Location/Url.php | 10 +--------- tests/Mods/ModsReaderTest.php | 22 +++++++++++++++++----- tests/resources/mods_book.xml | 2 +- tests/resources/mods_serial.xml | 4 ++-- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/Mods/Element/Specific/Location/Url.php b/src/Mods/Element/Specific/Location/Url.php index 0491db1..b1b8302 100644 --- a/src/Mods/Element/Specific/Location/Url.php +++ b/src/Mods/Element/Specific/Location/Url.php @@ -57,18 +57,10 @@ public function __construct(\SimpleXMLElement $xml) * @access public * * @return string - * - * @throws IncorrectValueInAttributeException */ public function getDateLastAccessed(): string { - $dateLastAccessed = $this->getStringAttribute('dateLastAccessed'); - - if (empty($dateLastAccessed) || in_array($dateLastAccessed, $this->allowedAccess)) { - return $dateLastAccessed; - } - - throw new IncorrectValueInAttributeException('dateLastAccessed', $dateLastAccessed); + return $this->getStringAttribute('dateLastAccessed'); } /** diff --git a/tests/Mods/ModsReaderTest.php b/tests/Mods/ModsReaderTest.php index 894e925..d0c61ba 100644 --- a/tests/Mods/ModsReaderTest.php +++ b/tests/Mods/ModsReaderTest.php @@ -12,6 +12,7 @@ namespace Slub\Mods; use PHPUnit\Framework\TestCase; +use Slub\Mods\Exception\IncorrectValueInAttributeException; class ModsReaderTest extends TestCase { @@ -497,6 +498,9 @@ public function testGetLocationsByQueryForBookDocument() self::assertEquals('http://www.slub-dresden.de/some-url', $urls[0]->getValue()); self::assertEquals('preview', $urls[1]->getAccess()); self::assertEquals('http://www.slub-dresden.de/some-url/SLO-0000', $urls[1]->getValue()); + + $this->expectException(IncorrectValueInAttributeException::class); + $urls[3]->getAccess(); } public function testGetNoLocationsByQueryForBookDocument() @@ -512,18 +516,18 @@ public function testGetLocationsForSerialDocument() self::assertEquals(2, count($locations)); self::assertNotEmpty($locations[0]->getUrls()); self::assertEquals('electronic resource', $locations[0]->getUrls()[0]->getDisplayLabel()); - self::assertEquals('primary display', $locations[0]->getUrls()[0]->getUsage()); + self::assertEquals('primaryDisplay', $locations[0]->getUrls()[0]->getUsage()); self::assertEquals('http://bibpurl.oclc.org/web/7085', $locations[0]->getUrls()[0]->getValue()); } public function testGetLocationsByQueryForSerialDocument() { - $locations = $this->serialReader->getLocations('[./mods:url[@usage="primary display"]]'); + $locations = $this->serialReader->getLocations('[./mods:url[@usage="primaryDisplay"]]'); self::assertNotEmpty($locations); self::assertEquals(1, count($locations)); self::assertNotEmpty($locations[0]->getUrls()); self::assertEquals('electronic resource', $locations[0]->getUrls()[0]->getDisplayLabel()); - self::assertEquals('primary display', $locations[0]->getUrls()[0]->getUsage()); + self::assertEquals('primaryDisplay', $locations[0]->getUrls()[0]->getUsage()); self::assertEquals('http://bibpurl.oclc.org/web/7085', $locations[0]->getUrls()[0]->getValue()); } @@ -968,7 +972,10 @@ public function testGetRecordInfosForSerialDocument() self::assertEquals('marcorg', $recordInfos[0]->getRecordContentSources()[0]->getAuthority()); self::assertEquals('NLC', $recordInfos[0]->getRecordContentSources()[0]->getValue()); self::assertNotEmpty($recordInfos[0]->getRecordCreationDates()); - self::assertEquals('marc', $recordInfos[0]->getRecordCreationDates()[0]->getEncoding()); + + $this->expectException(IncorrectValueInAttributeException::class); + $recordInfos[0]->getRecordCreationDates()[0]->getEncoding(); + self::assertEquals('021127', $recordInfos[0]->getRecordCreationDates()[0]->getValue()); self::assertNotEmpty($recordInfos[0]->getRecordChangeDates()); self::assertEquals('iso8601', $recordInfos[0]->getRecordChangeDates()[0]->getEncoding()); @@ -979,6 +986,7 @@ public function testGetRecordInfosForSerialDocument() self::assertNotEmpty($recordInfos[0]->getRecordInfoNotes()); self::assertEquals(2, count($recordInfos[0]->getRecordInfoNotes())); self::assertEquals('Some info', $recordInfos[0]->getRecordInfoNotes()[1]->getValue()); + $languages = $recordInfos[0]->getLanguageOfCatalogings(); self::assertNotEmpty($languages); self::assertNotNull($languages[0]->getLanguageTerm()); @@ -1003,7 +1011,10 @@ public function testGetRecordInfosByQueryForSerialDocument() self::assertEquals('marcorg', $recordInfos[0]->getRecordContentSources()[0]->getAuthority()); self::assertEquals('NLC', $recordInfos[0]->getRecordContentSources()[0]->getValue()); self::assertNotEmpty($recordInfos[0]->getRecordCreationDates()); - self::assertEquals('marc', $recordInfos[0]->getRecordCreationDates()[0]->getEncoding()); + + $this->expectException(IncorrectValueInAttributeException::class); + $recordInfos[0]->getRecordCreationDates()[0]->getEncoding(); + self::assertEquals('021127', $recordInfos[0]->getRecordCreationDates()[0]->getValue()); self::assertNotEmpty($recordInfos[0]->getRecordChangeDates()); self::assertEquals('iso8601', $recordInfos[0]->getRecordChangeDates()[0]->getEncoding()); @@ -1014,6 +1025,7 @@ public function testGetRecordInfosByQueryForSerialDocument() self::assertNotEmpty($recordInfos[0]->getRecordInfoNotes()); self::assertEquals(2, count($recordInfos[0]->getRecordInfoNotes())); self::assertEquals('Some info', $recordInfos[0]->getRecordInfoNotes()[1]->getValue()); + $languages = $recordInfos[0]->getLanguageOfCatalogings(); self::assertNotEmpty($languages); self::assertNotNull($languages[0]->getLanguageTerm()); diff --git a/tests/resources/mods_book.xml b/tests/resources/mods_book.xml index 662fb22..4a7b90e 100644 --- a/tests/resources/mods_book.xml +++ b/tests/resources/mods_book.xml @@ -137,7 +137,7 @@ http://www.slub-dresden.de/some-url http://www.slub-dresden.de/some-url/SLO-0000 http://www.slub-dresden.de/some-url/SLO-0000 - http://www.slub-dresden.de/some-url/SLO-0000 + http://www.slub-dresden.de/some-url/SLO-0000 Use of this public-domain resource is unrestricted. diff --git a/tests/resources/mods_serial.xml b/tests/resources/mods_serial.xml index b353e1b..937c19a 100644 --- a/tests/resources/mods_serial.xml +++ b/tests/resources/mods_serial.xml @@ -84,7 +84,7 @@ 027.7/05 - http://bibpurl.oclc.org/web/7085 + http://bibpurl.oclc.org/web/7085 http://collection.nlc-bnc.ca/100/201/300/ejasl/index.html @@ -109,7 +109,7 @@ aacr NLC - 021127 + 021127 20080910160139.0 15446420 Converted from MARCXML to MODS version 3.8 using MARC21slim2MODS3-8_XSLT1-0.xsl (Revision 1.172 20230208)