Initial commit

This commit is contained in:
2021-03-23 21:58:40 +01:00
commit 61623f5a35
30 changed files with 1936 additions and 0 deletions

43
src/PDF/Document.php Normal file
View File

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

53
src/PDF/Metadata.php Normal file
View File

@@ -0,0 +1,53 @@
<?php
namespace PDF;
use Cocur\Slugify\Slugify;
class Metadata
{
public ?string $abbreviation = null;
public ?string $author = null;
public ?string $creationdate = null;
public ?string $creator = null;
public ?string $encrypted = null;
public ?string $form = null;
public ?string $javascript = null;
public ?string $keywords = null;
public ?string $linearized = null;
public ?string $moddate = null;
public ?string $optimized = null;
public ?string $page_rot = null;
public ?string $page_size = null;
public ?string $pages = null;
public ?string $pdf_subtype = null;
public ?string $pdf_version = null;
public ?string $producer = null;
public ?string $standard = null;
public ?string $subject = null;
public ?string $subtitle = null;
public ?string $suspects = null;
public ?string $tagged = null;
public ?string $title = null;
public ?string $userproperties = null;
public function fillWith(array $array): Metadata
{
$slugify = new Slugify(['separator' => '_']);
$array = array_filter($array, static fn(string $v) => trim($v) !== '');
foreach ($array as $key => $value) {
$key = $slugify->slugify($key);
if (property_exists(__CLASS__, $key)) {
$this->{$key} = trim($value);
}
}
return $this;
}
public function toArray(): array
{
return get_object_vars($this);
}
}