Skip to content

Commit

Permalink
Add functions to get the elements by index of value
Browse files Browse the repository at this point in the history
Move readers to traits to increase code readability
  • Loading branch information
beatrycze-volk committed Jun 20, 2024
1 parent 1996268 commit e3ff7f2
Show file tree
Hide file tree
Showing 21 changed files with 1,861 additions and 1,131 deletions.
1,160 changes: 29 additions & 1,131 deletions src/Mods/ModsReader.php

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions src/Mods/Reader/AbstractReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* Copyright (C) 2024 Saxon State and University Library Dresden
*
* This file is part of the php-mods-reader.
*
* @license GNU General Public License version 3 or later.
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Slub\Mods\Reader;

use Slub\Mods\Element\AbstractElement;
use Slub\Mods\Element\Xml\Element;

/**
* Trait for reading Abstract element
*/
trait AbstractReader
{

/**
* Get the value of the <abstract> element.
* @see https://www.loc.gov/standards/mods/userguide/abstract.html
*
* @access public
*
* @param string $query for metadata search
*
* @return ?AbstractElement
*/
public function getAbstract(string $query = ''): ?AbstractElement
{
$xpath = './mods:abstract' . $query;
$element = new Element($this->xml, $xpath);
if ($element->exists()) {
return new AbstractElement($element->getFirstValue());
}
return null;
}
}
97 changes: 97 additions & 0 deletions src/Mods/Reader/AccessConditionReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/**
* Copyright (C) 2024 Saxon State and University Library Dresden
*
* This file is part of the php-mods-reader.
*
* @license GNU General Public License version 3 or later.
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Slub\Mods\Reader;

use Slub\Mods\Element\AccessCondition;

/**
* Trait for reading AccessCondition element
*/
trait AccessConditionReader
{

/**
* Get the the array of the <accessCondition> elements.
* @see https://www.loc.gov/standards/mods/userguide/accesscondition.html
*
* @access public
*
* @param string $query for metadata search
*
* @return AccessCondition[]
*/
public function getAccessConditions(string $query = ''): array
{
$accessConditions = [];
$values = $this->getValues('./mods:accessCondition' . $query);
foreach ($values as $value) {
$accessConditions[] = new AccessCondition($value);
}
return $accessConditions;
}

/**
* Get the matching <accessCondition> element.
* @see https://www.loc.gov/standards/mods/userguide/accesscondition.html
*
* @access public
*
* @param int $index of the searched element
* @param string $query for metadata search
*
* @return ?AccessCondition
*/
public function getAccessCondition(int $index, string $query = ''): ?AccessCondition
{
$values = $this->getValues('./mods:accessCondition' . $query);
if (array_key_exists($index, $values)) {
return new AccessCondition($values[$index]);
}
return null;
}

/**
* Get the the first matching <accessCondition> element.
* @see https://www.loc.gov/standards/mods/userguide/accesscondition.html
*
* @access public
*
* @param string $query for metadata search
*
* @return ?AccessCondition
*/
public function getFirstAccessCondition(string $query = ''): ?AccessCondition
{
return $this->getAccessCondition(0, $query);
}

/**
* Get the the last matching <accessCondition> element.
* @see https://www.loc.gov/standards/mods/userguide/accesscondition.html
*
* @access public
*
* @param string $query for metadata search
*
* @return ?AccessCondition
*/
public function getLastAccessCondition(string $query = ''): ?AccessCondition
{
$elements = $this->getAccessConditions($query);
$count = count($elements);
if ($count > 0) {
return $elements[$count - 1];
}
return null;
}
}
96 changes: 96 additions & 0 deletions src/Mods/Reader/ClassificationReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/**
* Copyright (C) 2024 Saxon State and University Library Dresden
*
* This file is part of the php-mods-reader.
*
* @license GNU General Public License version 3 or later.
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Slub\Mods\Reader;

use Slub\Mods\Element\Classification;

/**
* Trait for reading Classification element
*/
trait ClassificationReader
{
/**
* Get the the array of the <classification> elements.
* @see https://www.loc.gov/standards/mods/userguide/classification.html
*
* @access public
*
* @param string $query for metadata search
*
* @return Classification[]
*/
public function getClassifications(string $query = ''): array
{
$classifications = [];
$values = $this->getValues('./mods:classification' . $query);
foreach ($values as $value) {
$classifications[] = new Classification($value);
}
return $classifications;
}

/**
* Get the matching <classification> element.
* @see https://www.loc.gov/standards/mods/userguide/classification.html
*
* @access public
*
* @param int $index of the searched element
* @param string $query for metadata search
*
* @return ?Classification
*/
public function getClassification(int $index, string $query = ''): ?Classification
{
$values = $this->getValues('./mods:classification' . $query);
if (array_key_exists($index, $values)) {
return new Classification($values[$index]);
}
return null;
}

/**
* Get the the first matching <classification> element.
* @see https://www.loc.gov/standards/mods/userguide/classification.html
*
* @access public
*
* @param string $query for metadata search
*
* @return ?Classification
*/
public function getFirstClassification(string $query = ''): ?Classification
{
return $this->getClassification(0, $query);
}

/**
* Get the the last matching <classification> element.
* @see https://www.loc.gov/standards/mods/userguide/classification.html
*
* @access public
*
* @param string $query for metadata search
*
* @return ?Classification
*/
public function getLastClassification(string $query = ''): ?Classification
{
$elements = $this->getClassifications($query);
$count = count($elements);
if ($count > 0) {
return $elements[$count - 1];
}
return null;
}
}
98 changes: 98 additions & 0 deletions src/Mods/Reader/ExtensionReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/**
* Copyright (C) 2024 Saxon State and University Library Dresden
*
* This file is part of the php-mods-reader.
*
* @license GNU General Public License version 3 or later.
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Slub\Mods\Reader;

use Slub\Mods\Element\Extension;
use Slub\Mods\Element\Xml\Element;

/**
* Trait for reading Extension element
*/
trait ExtensionReader
{

/**
* Get the the array of the <extension> elements.
* @see https://www.loc.gov/standards/mods/userguide/extension.html
*
* @access public
*
* @param string $query for metadata search
*
* @return Extension[]
*/
public function getExtensions(string $query = ''): array
{
$extensions = [];
$values = $this->getValues('./mods:extension' . $query);
foreach ($values as $value) {
$extensions[] = new Extension($value);
}
return $extensions;
}

/**
* Get the matching <extension> element.
* @see https://www.loc.gov/standards/mods/userguide/extension.html
*
* @access public
*
* @param int $index of the searched element
* @param string $query for metadata search
*
* @return ?Extension
*/
public function getExtension(int $index, string $query = ''): ?Extension
{
$values = $this->getValues('./mods:extension' . $query);
if (array_key_exists($index, $values)) {
return new Extension($values[$index]);
}
return null;
}

/**
* Get the the first matching <extension> element.
* @see https://www.loc.gov/standards/mods/userguide/extension.html
*
* @access public
*
* @param string $query for metadata search
*
* @return ?Extension
*/
public function getFirstExtension(string $query = ''): ?Extension
{
return $this->getExtension(0, $query);
}

/**
* Get the the last matching <extension> element.
* @see https://www.loc.gov/standards/mods/userguide/extension.html
*
* @access public
*
* @param string $query for metadata search
*
* @return ?Extension
*/
public function getLastExtension(string $query = ''): ?Extension
{
$elements = $this->getExtensions($query);
$count = count($elements);
if ($count > 0) {
return $elements[$count - 1];
}
return null;
}
}
Loading

0 comments on commit e3ff7f2

Please sign in to comment.