123 lines
3.9 KiB
PHP
123 lines
3.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace OaiSymfony\OAI;
|
|
|
|
use OaiSymfony\OAI\RecordOaiInterface;
|
|
use OaiSymfony\OAI\Exception\IdentifierNotFoundException;
|
|
use Symfony\Component\Serializer\Encoder\XmlEncoder;
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
|
use Symfony\Component\Serializer\Serializer;
|
|
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
|
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
|
|
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
|
|
use Symfony\Component\Serializer\Attribute\SerializedName;
|
|
|
|
/**
|
|
* Serializes a record (DB data) in the `oai_dc` metadata format
|
|
* @class RecordOaiDc
|
|
*/
|
|
final class RecordOaiDc implements RecordOaiInterface
|
|
{
|
|
private int $id;
|
|
private string $identifier;
|
|
private string $verb;
|
|
|
|
#[SerializedName('dc:title')]
|
|
public string $title;
|
|
#[SerializedName('dc:rights')]
|
|
public array $rights;
|
|
#[SerializedName('dc:description')]
|
|
public ?string $description;
|
|
#[SerializedName('dc:date')]
|
|
public int $date;
|
|
#[SerializedName('dc:language')]
|
|
public string $language;
|
|
#[SerializedName('dc:type')]
|
|
public array $recordType;
|
|
#[SerializedName('dc:subject')]
|
|
public array $subject;
|
|
#[SerializedName('dc:creator')]
|
|
public $creator;
|
|
#[SerializedName('dc:identifier')]
|
|
public array $oaiIdentifier;
|
|
#[SerializedName('dc:format')]
|
|
public string $format;
|
|
#[SerializedName('dc:source')]
|
|
public array|string $source;
|
|
|
|
public function __construct(
|
|
string $identifier,
|
|
string $verb = 'GetRecord',
|
|
private Serializer $serializer = new Serializer(),
|
|
)
|
|
{
|
|
$this->verb = $verb;
|
|
$this->identifier = $identifier;
|
|
preg_match("/oai:www.yourdomain.com(:|\/)(?P<res>.*):(?P<id>\d+)$/", $identifier, $matches);
|
|
|
|
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
|
|
$metadataAwareNameConverter = new MetadataAwareNameConverter($classMetadataFactory);
|
|
|
|
$this->serializer = new Serializer(
|
|
[new ObjectNormalizer($classMetadataFactory, $metadataAwareNameConverter)],
|
|
[new XmlEncoder()]
|
|
);
|
|
}
|
|
/**
|
|
* Map DB record data to object attributes for serialization
|
|
*/
|
|
private function mapData(array $record): void
|
|
{
|
|
// Implement
|
|
}
|
|
/**
|
|
* Return an XML representation of a DB record
|
|
* in a given format
|
|
*/
|
|
public function toXML(): string
|
|
{
|
|
$record = []; // This represents a record from the database
|
|
if ($record === null) {
|
|
throw new IdentifierNotFoundException($this->identifier);
|
|
}
|
|
$this->mapData($record);
|
|
$normal = $this->serializer->normalize($this);
|
|
|
|
$data = [
|
|
'@xmlns:oai_dc' => "http://www.openarchives.org/OAI/2.0/oai_dc/",
|
|
'@xmlns:dc' => "http://purl.org/dc/elements/1.1/",
|
|
'#' => $normal
|
|
];
|
|
|
|
$oai_dc = ['oai_dc:dc' => $data];
|
|
if (isset($record['relation'])) {
|
|
$oai_dc['oai_dc:dc']['dc:relation'] = $record['relation'];
|
|
}
|
|
$header = [
|
|
'identifier' => $this->identifier,
|
|
'datestamp' => $record['datestamp'],
|
|
'setSpec' => $record['setSpec'],
|
|
];
|
|
// Adapt for ListRecords...
|
|
$record = $this->verb === 'GetRecord' ?
|
|
['record' => ['header' => $header, 'metadata' => $oai_dc]] :
|
|
['header' => $header, 'metadata' => $oai_dc];
|
|
|
|
$content = $this->serializer->encode(
|
|
$record,
|
|
'xml',
|
|
[
|
|
'xml_format_output' => true,
|
|
'xml_root_node_name' => $this->verb === 'GetRecord' ? $this->verb : 'record',
|
|
// Don't include the XML declaration, handled by OaiBase
|
|
'encoder_ignored_node_types' => [
|
|
\XML_PI_NODE,
|
|
],
|
|
]
|
|
);
|
|
|
|
return $content;
|
|
}
|
|
} |