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

View File

@@ -0,0 +1,12 @@
<?php
namespace Filter;
use PDF\Document;
interface DocumentFilter
{
public function allows(Document $document): bool;
public function __toString(): string;
}

View File

@@ -0,0 +1,15 @@
<?php
namespace Filter;
class FilterFactory
{
public function createFromString(string $string): DocumentFilter
{
if (preg_match('/^.+=.*$/', $string)) {
[$prop, $term] = explode('=', $string);
return new SpecificFilter(trim($prop), trim($term));
}
return new GenericFilter($string);
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace Filter;
use PDF\Document;
class GenericFilter implements DocumentFilter
{
private string $term;
public function __construct(string $term)
{
$this->term = $term;
}
public function allows(Document $document): bool
{
if ($this->term === '') {
return true;
}
foreach ($document->getProperties() as $key => $value) {
if (stripos($value, $this->term) !== false) {
return true;
}
}
return false;
}
public function __toString(): string
{
return sprintf('[*] contains \'%s\'', $this->term);
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace Filter;
use PDF\Document;
use RuntimeException;
class SpecificFilter implements DocumentFilter
{
private string $property;
private string $term;
public function __construct(string $property, string $term)
{
$this->property = strtolower($property);
$this->term = strtolower($term);
}
public function allows(Document $document): bool
{
if ($this->property === '') {
return true;
}
try {
$property = $document->getProperty($this->property);
if ($this->term === '' && !empty($property)) {
// Filter is "prop=", which only checks if it exists.
return true;
}
return stripos($property, $this->term) !== false;
} catch (RuntimeException $e) {
// No such property exists, we don't pass
return false;
}
}
public function __toString(): string
{
return sprintf('property \'%s\' contains \'%s\'', $this->property, $this->term);
}
}