From ee78b6526b6456f1013cfbb3702b62c092d08551 Mon Sep 17 00:00:00 2001 From: Beatrycze Volk Date: Tue, 16 Apr 2024 17:22:25 +0200 Subject: [PATCH] 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 | 18 +++++-- .../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, 146 insertions(+), 26 deletions(-) diff --git a/src/Mods/Attribute/Common/DateAttribute.php b/src/Mods/Attribute/Common/DateAttribute.php index a3a090b..3c3ae3b 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..41da487 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..fa4da23 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..c67fdda 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..47a248f 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,7 +84,7 @@ public function getNote(): string } /** - * Get the value of access + * Get the value of the 'access' attribute. * * @access public * diff --git a/src/Mods/Element/Specific/Name/AlternativeName.php b/src/Mods/Element/Specific/Name/AlternativeName.php index 400deb5..3f993da 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..3be97ad 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..740dc95 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..872738d 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..102d153 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); } /**