Skip to content

Commit

Permalink
Merge pull request #14 from beatrycze-volk/validate-attributes
Browse files Browse the repository at this point in the history
Add `IncorrectValueInAttributeException` and use it for validation
  • Loading branch information
beatrycze-volk authored Apr 18, 2024
2 parents 19bf0f0 + c75ce69 commit 5bd5fbe
Show file tree
Hide file tree
Showing 14 changed files with 200 additions and 34 deletions.
47 changes: 39 additions & 8 deletions src/Mods/Attribute/Common/DateAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace Slub\Mods\Attribute\Common;

use Slub\Mods\Exception\IncorrectValueInAttributeException;

trait DateAttribute
{

Expand Down Expand Up @@ -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
*
Expand All @@ -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
*
Expand Down
15 changes: 13 additions & 2 deletions src/Mods/Attribute/Common/Miscellaneous/UsageAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace Slub\Mods\Attribute\Common\Miscellaneous;

use Slub\Mods\Exception\IncorrectValueInAttributeException;

/**
* Trait for usage common attribute
*/
Expand All @@ -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);
}
}
14 changes: 12 additions & 2 deletions src/Mods/Attribute/Specific/TypeAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace Slub\Mods\Attribute\Specific;

use Slub\Mods\Exception\IncorrectValueInAttributeException;

/**
* Trait for type specific attribute
*/
Expand All @@ -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);
}
}
14 changes: 12 additions & 2 deletions src/Mods/Element/RelatedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
18 changes: 14 additions & 4 deletions src/Mods/Element/Specific/Location/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -50,7 +51,8 @@ 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
*
Expand All @@ -62,7 +64,7 @@ public function getDateLastAccessed(): string
}

/**
* Get the value of note
* Get the value of the 'note' attribute.
*
* @access public
*
Expand All @@ -74,14 +76,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);
}
}
11 changes: 10 additions & 1 deletion src/Mods/Element/Specific/Name/AlternativeName.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
13 changes: 11 additions & 2 deletions src/Mods/Element/Specific/Name/NamePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
15 changes: 13 additions & 2 deletions src/Mods/Element/Specific/OriginInfo/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
}

/**
Expand Down
14 changes: 12 additions & 2 deletions src/Mods/Element/Specific/TitleInfo/NonSort.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
Loading

0 comments on commit 5bd5fbe

Please sign in to comment.