-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace Shuble\Slurpy; | ||
|
||
use Shuble\Slurpy\Operation\OperationArgument\PageRange; | ||
use Shuble\Slurpy\InputFile; | ||
use Shuble\Slurpy\Operation\CatOperation; | ||
|
||
class Factory | ||
{ | ||
/** | ||
* Path to pdftk binary | ||
* | ||
* @var string | ||
*/ | ||
protected $binary; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param string $binary | ||
*/ | ||
public function __construct($binary) | ||
{ | ||
$this->binary = $binary; | ||
} | ||
|
||
/** | ||
* Creates a slurpy instance initialized with a cat operation | ||
* | ||
* @param array $inputs An array of input files | ||
* array( | ||
* 'path/to/file1.pdf', | ||
* 'path/to/file2.pdf', | ||
* ... | ||
* ) | ||
* or | ||
* array( | ||
* array( | ||
* 'filepath' => 'path/to/file1.pdf', | ||
* 'password' => 'pa$$word', | ||
* 'start_page' => 1, | ||
* 'end_page' => end, | ||
* 'qualifier' => 'odd', | ||
* 'rotation' => 'east', | ||
* ), | ||
* ... | ||
* ) | ||
* @param string $output | ||
*/ | ||
public function cat(array $inputs, $output = null) | ||
{ | ||
$slurpy = new Slurpy($this->binary); | ||
|
||
$operation = new CatOperation(); | ||
|
||
foreach ($inputs as $index => $input) { | ||
$inputFile = new InputFile(); | ||
|
||
if (is_array($input) && empty($input['filepath'])) { | ||
throw new \InvalidArgumentException('You must specify a filepath for each input files'); | ||
} | ||
|
||
// InputFile data | ||
$handle = $this->generateFileHandle($index); | ||
$filePath = is_array($input) ? $input['filepath'] : $input; | ||
$filePassword = is_array($input) && !empty($input['password']) | ||
? $input['password'] | ||
: null | ||
; | ||
|
||
$inputFile->setHandle($handle); | ||
$inputFile->setFilePath($filePath); | ||
$inputFile->setPassword(null); | ||
|
||
$slurpy->addInput($inputFile); | ||
|
||
// PageRange data | ||
$startPage = is_array($input) && !empty($input['start_page']) ? $input['start_page'] : null; | ||
$endPage = is_array($input) && !empty($input['end_page']) ? $input['end_page'] : null; | ||
$qualifier = is_array($input) && !empty($input['qualifier']) ? $input['qualifier'] : null; | ||
$rotation = is_array($input) && !empty($input['rotation']) ? $input['rotation'] : null; | ||
|
||
$pageRange = new PageRange($handle, $startPage, $endPage, $qualifier, $rotation); | ||
$operation->addPageRange($pageRange); | ||
|
||
$slurpy->setOperation($operation); | ||
} | ||
|
||
return $slurpy; | ||
} | ||
|
||
/** | ||
* Generate a unique file handle | ||
* | ||
* @param int $index | ||
* | ||
* @return string | ||
*/ | ||
protected function generateFileHandle($index) | ||
{ | ||
return chr(65 + floor($index/26) % 26) . chr(65 + $index % 26); | ||
} | ||
} |