-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "Mutators" directory, deprecate "Transformer" directory (#7)
- Loading branch information
Showing
11 changed files
with
242 additions
and
121 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
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
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,20 @@ | ||
<?php | ||
|
||
namespace Rougin\Weasley\Contract; | ||
|
||
/** | ||
* Mutator Interface | ||
* | ||
* @package Weasley | ||
* @author Rougin Gutib <rougingutib@gmail.com> | ||
*/ | ||
interface Mutator | ||
{ | ||
/** | ||
* Mutates the contents of the result. | ||
* | ||
* @param mixed $data | ||
* @return mixed | ||
*/ | ||
public function mutate($data); | ||
} |
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,92 @@ | ||
<?php | ||
|
||
namespace Rougin\Weasley\Mutators; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Rougin\Weasley\Contract\Mutator; | ||
|
||
/** | ||
* JSON Transformer | ||
* | ||
* @package Weasley | ||
* @author Rougin Gutib <rougingutib@gmail.com> | ||
*/ | ||
class JsonMutator implements Mutator | ||
{ | ||
/** | ||
* @var array<integer, string> | ||
*/ | ||
protected $errors = array(); | ||
|
||
/** | ||
* @var integer | ||
*/ | ||
protected $options = 0; | ||
|
||
/** | ||
* @var \Psr\Http\Message\ResponseInterface | ||
*/ | ||
protected $response; | ||
|
||
/** | ||
* @param \Psr\Http\Message\ResponseInterface $response | ||
* @param integer $options | ||
*/ | ||
public function __construct(ResponseInterface $response, $options = 0) | ||
{ | ||
$this->errors[] = 'No errors'; | ||
$this->errors[] = 'Maximum stack depth exceeded'; | ||
$this->errors[] = 'Underflow or the modes mismatch'; | ||
$this->errors[] = 'Unexpected control character found'; | ||
$this->errors[] = 'Syntax error, malformed JSON'; | ||
$this->errors[] = 'Malformed UTF-8 characters, possibly incorrectly encoded'; | ||
$this->errors[] = 'One or more recursive references in the value to be encoded'; | ||
$this->errors[] = 'One or more NAN or INF values in the value to be encoded'; | ||
$this->errors[] = 'A value of a type that cannot be encoded was given'; | ||
$this->errors[] = 'A property name that cannot be encoded was given'; | ||
$this->errors[] = 'Malformed UTF-16 characters, possibly incorrectly encoded'; | ||
|
||
$this->options = $options; | ||
|
||
$this->response = $response; | ||
} | ||
|
||
/** | ||
* @deprecated since ~0.7, use "mutate" instead. | ||
* @codeCoverageIgnore | ||
* | ||
* Transforms the contents of the result. | ||
* | ||
* @param mixed $data | ||
* @return mixed | ||
*/ | ||
public function transform($data) | ||
{ | ||
return $this->mutate($data); | ||
} | ||
|
||
/** | ||
* Mutates the contents of the result. | ||
* | ||
* @param mixed $data | ||
* @return mixed | ||
*/ | ||
public function mutate($data) | ||
{ | ||
$response = $this->response; | ||
|
||
/** @var string */ | ||
$stream = @json_encode($data, $this->options); | ||
|
||
if (json_last_error() !== JSON_ERROR_NONE) | ||
{ | ||
$stream = $this->errors[json_last_error()]; | ||
|
||
$response = $response->withStatus(400); | ||
} | ||
|
||
$response->getBody()->write($stream); | ||
|
||
return $response->withHeader('Content-Type', 'application/json'); | ||
} | ||
} |
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,77 @@ | ||
<?php | ||
|
||
namespace Rougin\Weasley\Mutators; | ||
|
||
use Illuminate\Contracts\Support\Arrayable; | ||
use Rougin\Weasley\Contract\Mutator; | ||
|
||
/** | ||
* API Transformer | ||
* | ||
* @package Weasley | ||
* @author Rougin Gutib <rougingutib@gmail.com> | ||
*/ | ||
class RestMutator implements Mutator | ||
{ | ||
const PAGINATOR = 'Illuminate\Pagination\LengthAwarePaginator'; | ||
|
||
/** | ||
* @deprecated since ~0.7, use "mutate" instead. | ||
* @codeCoverageIgnore | ||
* | ||
* Transforms the contents of the result. | ||
* | ||
* @param \Illuminate\Contracts\Support\Arrayable $data | ||
* @return mixed | ||
*/ | ||
public function transform($data) | ||
{ | ||
return $this->mutate($data); | ||
} | ||
|
||
/** | ||
* Mutates the contents of the result. | ||
* | ||
* @param \Illuminate\Contracts\Support\Arrayable $data | ||
* @return mixed | ||
*/ | ||
public function mutate($data) | ||
{ | ||
$result = $data->toArray(); | ||
|
||
if (is_object($data) && is_a($data, self::PAGINATOR)) | ||
{ | ||
/** @var \Illuminate\Pagination\LengthAwarePaginator $data */ | ||
$result = $this->paginator($data); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Converts the paginator into Paypal API standards. | ||
* | ||
* @param \Illuminate\Contracts\Support\Arrayable $data | ||
* @return array<string, mixed> | ||
*/ | ||
protected function paginator(Arrayable $data) | ||
{ | ||
$object = $data->toArray(); | ||
|
||
$perPage = (int) $object['per_page']; | ||
|
||
$total = (int) $object['total']; | ||
|
||
$rounded = round($total / $perPage); | ||
|
||
$result = array(); | ||
|
||
$result['total_items'] = $total; | ||
|
||
$result['total_pages'] = $rounded ?: 1; | ||
|
||
$result['items'] = $object['data']; | ||
|
||
return $result; | ||
} | ||
} |
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
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
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
Oops, something went wrong.