pdf-finder/src/PDF/Document.php

46 lines
1013 B
PHP

<?php
namespace PDF;
use IO\Filesystem\File;
use RuntimeException;
class Document
{
public File $file;
public Metadata $metadata;
public function __construct(File $file, Metadata $metadata)
{
$this->file = $file;
$this->metadata = $metadata;
}
public function getProperty(string $prop): ?string
{
if (in_array($prop, ['path', 'filepath'])) {
return (string)$this->file;
}
if (in_array($prop, ['file', 'name', 'filename'])) {
return $this->file->getInfo()->getBasename();
}
if (property_exists($this->metadata, $prop)) {
return $this->metadata->{$prop};
}
throw new RuntimeException('No such property');
}
public function getProperties(): array
{
$info = $this->file->getInfo();
return [
'filepath' => $info->getPath(),
'filename' => $info->getBasename(),
] + $this->metadata->toArray();
}
}