Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
maztch committed Apr 26, 2017
2 parents a005341 + 4e396a2 commit cfaedc3
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 92 deletions.
54 changes: 54 additions & 0 deletions samples/try_catch_errors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
//include the autoloader
require_once('../vendor/autoload.php');

use Ilovepdf\Ilovepdf;
use Ilovepdf\SplitTask;


try {
// start the manager classy
$ilovepdf = new Ilovepdf("PUBLIC_KEY", "SECRET_KEY");

// and get the task tool
$myTask = $ilovepdf->newTask('split');

// or you can call task class directly, this set the same tool as before
$myTask = new SplitTask("PUBLIC_KEY", "SECRET_KEY");


// file var keeps info about server file id, name...
// it can be used latter to cancel file
$file = $myTask->addFile('/path/to/file/document.pdf');

// set ranges to split the document
$myTask->setRanges("2-4,6-8");

// and set name for output file.
// in this case it will output a zip file, so we set the package name.
$myTask->setPackagedFilename('split_documents');

// and name for splitted document (inside the zip file)
$myTask->setOutputFilename('split');

// process files
// time var will have info about time spent in process
$time = $myTask->execute();

// and finally download file. If no path is set, it will be donwloaded on current folder
$myTask->download('path/to/download');

} catch (\Ilovepdf\Exceptions\StartException $e) {
echo "An error occured on start: " . $e->getMessage() . " ";
} catch (\Ilovepdf\Exceptions\AuthException $e) {
echo "An error occured on auth: " . $e->getMessage() . " ";
echo implode(', ', $e->getErrors());
} catch (\Ilovepdf\Exceptions\UploadException $e) {
echo "An error occured on upload: " . $e->getMessage() . " ";
echo implode(', ', $e->getErrors());
} catch (\Ilovepdf\Exceptions\ProcessException $e) {
echo "An error occured on process: " . $e->getMessage() . " ";
echo implode(', ', $e->getErrors());
} catch (\Exception $e) {
echo "An error occured: " . $e->getMessage();
}
25 changes: 1 addition & 24 deletions src/Exceptions/AuthException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,7 @@

namespace Ilovepdf\Exceptions;

class AuthException extends \Exception
class AuthException extends ExtendedException
{

private $params;
private $type;

public function __construct($message,
$code = 0,
Exception $previous = null,
$response)
{
parent::__construct($message, $code, $previous);

$this->type = $response->body->name;
$this->params = null;
}

public function getErrors()
{
return $this->params;
}

public function getType()
{
return $this->type;
}
}
7 changes: 7 additions & 0 deletions src/Exceptions/DownloadException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Ilovepdf\Exceptions;

class DownloadException extends ExtendedException {

}
45 changes: 45 additions & 0 deletions src/Exceptions/ExtendedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Ilovepdf\Exceptions;

class ExtendedException extends \Exception
{

private $params;
private $type;

/**
* ExtendedException constructor.
* @param string $message
* @param int $code
* @param Exception|null $previous
* @param $response
*/
public function __construct($message, $code = 0, Exception $previous = null, $response)
{
$this->type = $response->body->error->type;
$this->params = $response->body->error->param;

if (is_array($this->params) && isset($this->params[0]) && isset($this->params[0]->error)) {
parent::__construct($message . ' (' . $this->params[0]->error . ')', $code, $previous);
} else {
parent::__construct($message, $code, $previous);
}
}

/**
* @return mixed
*/
public function getErrors()
{
return $this->params;
}

/**
* @return mixed
*/
public function getType()
{
return $this->type;
}
}
34 changes: 1 addition & 33 deletions src/Exceptions/ProcessException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,6 @@

namespace Ilovepdf\Exceptions;

class ProcessException extends \Exception {
class ProcessException extends ExtendedException {

private $params;
private $type;

public function __construct($message,
$code = 0,
Exception $previous = null,
$response)
{
parent::__construct($message, $code, $previous);

$this->type = $response->body->error->type;
$this->params = $response->body->error->param;
}

public function getErrors() {

/*foreach($this->params as $key=>$error_param){
foreach($error_param as $single_param){
$errors[$key]=$single_param;
}
}*/
return $this->params;
}
public function getType() {

/*foreach($this->params as $key=>$error_param){
foreach($error_param as $single_param){
$errors[$key]=$single_param;
}
}*/
return $this->type;
}
}
34 changes: 1 addition & 33 deletions src/Exceptions/UploadException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,6 @@

namespace Ilovepdf\Exceptions;

class UploadException extends \Exception {
class UploadException extends ExtendedException {

private $params;
private $type;

public function __construct($message,
$code = 0,
Exception $previous = null,
$response)
{
parent::__construct($message, $code, $previous);

$this->type = $response->body->error->type;
$this->params = $response->body->error->param;
}

public function getErrors() {

/*foreach($this->params as $key=>$error_param){
foreach($error_param as $single_param){
$errors[$key]=$single_param;
}
}*/
return $this->params;
}
public function getType() {

/*foreach($this->params as $key=>$error_param){
foreach($error_param as $single_param){
$errors[$key]=$single_param;
}
}*/
return $this->type;
}
}
6 changes: 5 additions & 1 deletion src/Ilovepdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ilovepdf;

use Ilovepdf\Exceptions\DownloadException;
use Ilovepdf\Exceptions\ProcessException;
use Ilovepdf\Exceptions\UploadException;
use Ilovepdf\Exceptions\StartException;
Expand Down Expand Up @@ -29,7 +30,7 @@ class Ilovepdf
// @var string|null The version of the Ilovepdf API to use for requests.
public static $apiVersion = 'v1';

const VERSION = 'php.1.0.11';
const VERSION = 'php.1.0.14';

public $token = null;

Expand Down Expand Up @@ -182,6 +183,9 @@ public function sendRequest($method, $endpoint, $body=null)
elseif ($endpoint == 'process') {
throw new ProcessException($response->body->error->message, $response->code, null, $response);
}
elseif (strpos($endpoint, 'download')===0) {
throw new DownloadException($response->body->error->message, $response->code, null, $response);
}
else{
throw new \Exception($response->body->error->message);
}
Expand Down
22 changes: 22 additions & 0 deletions src/ImagepdfTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class ImagepdfTask extends Task
*/
public $margin;

/**
* @var boolean
*/
public $merge_after = true;

/**
* @var string
Expand Down Expand Up @@ -73,4 +77,22 @@ public function setPagesize($pagesize)
$this->pagesize = $pagesize;
return $this;
}

/**
* @param boolean $merge_after
*/
public function setMergeAfter($merge_after)
{
$this->merge_after = $merge_after;
return $this;
}

/**
* @param null $processData
* @return mixed
*/
public function execute($processData = null)
{
return parent::execute(get_object_vars($this));
}
}
35 changes: 35 additions & 0 deletions src/ProtectTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Ilovepdf;
/**
* Class LockTask
*
* @package Ilovepdf
*/
class ProtectTask extends Task
{

/**
* @var string
*/
public $password;

/**
* UnlockTask constructor.
* @param string $publicKey
* @param string $secretKey
*/
function __construct($publicKey, $secretKey)
{
$this->tool = 'protect';
parent::__construct($publicKey, $secretKey);
}

/**
* @param string $password
*/
public function setPassword($password)
{
$this->password = $password;
}
}
2 changes: 1 addition & 1 deletion src/WatermarkTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class WatermarkTask extends Task
*/
public $vertical_position;

private $verticalPositionValues = ['bottom', 'top'];
private $verticalPositionValues = ['bottom', 'center', 'top'];
/**
* @var string
*/
Expand Down

0 comments on commit cfaedc3

Please sign in to comment.