Initial commit
This commit is contained in:
12
src/Filter/DocumentFilter.php
Normal file
12
src/Filter/DocumentFilter.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Filter;
|
||||
|
||||
use PDF\Document;
|
||||
|
||||
interface DocumentFilter
|
||||
{
|
||||
public function allows(Document $document): bool;
|
||||
|
||||
public function __toString(): string;
|
||||
}
|
||||
15
src/Filter/FilterFactory.php
Normal file
15
src/Filter/FilterFactory.php
Normal 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);
|
||||
}
|
||||
}
|
||||
34
src/Filter/GenericFilter.php
Normal file
34
src/Filter/GenericFilter.php
Normal 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);
|
||||
}
|
||||
}
|
||||
42
src/Filter/SpecificFilter.php
Normal file
42
src/Filter/SpecificFilter.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user