This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/61' into develop
Close #61
- Loading branch information
Showing
10 changed files
with
403 additions
and
162 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
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,73 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @see http://github.com/zendframework/zend-diactoros for the canonical source repository | ||
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace Zend\Diactoros\Response; | ||
|
||
use InvalidArgumentException; | ||
use Psr\Http\Message\StreamInterface; | ||
use Zend\Diactoros\Response; | ||
use Zend\Diactoros\Stream; | ||
|
||
/** | ||
* HTML response. | ||
* | ||
* Allows creating a response by passing an HTML string to the constructor; | ||
* by default, sets a status code of 200 and sets the Content-Type header to | ||
* text/html. | ||
*/ | ||
class HtmlResponse extends Response | ||
{ | ||
use InjectContentTypeTrait; | ||
|
||
/** | ||
* Create an HTML response. | ||
* | ||
* Produces an HTML response with a Content-Type of text/html and a default | ||
* status of 200. | ||
* | ||
* @param string|StreamInterface $html HTML or stream for the message body. | ||
* @param int $status Integer status code for the response; 200 by default. | ||
* @param array $headers Array of headers to use at initialization. | ||
* @throws InvalidArgumentException if $html is neither a string or stream. | ||
*/ | ||
public function __construct($html, $status = 200, array $headers = []) | ||
{ | ||
parent::__construct( | ||
$this->createBody($html), | ||
$status, | ||
$this->injectContentType('text/html', $headers) | ||
); | ||
} | ||
|
||
/** | ||
* Create the message body. | ||
* | ||
* @param string|StreamInterface $html | ||
* @return StreamInterface | ||
* @throws InvalidArgumentException if $html is neither a string or stream. | ||
*/ | ||
private function createBody($html) | ||
{ | ||
if ($html instanceof StreamInterface) { | ||
return $html; | ||
} | ||
|
||
if (! is_string($html)) { | ||
throw new InvalidArgumentException(sprintf( | ||
'Invalid content (%s) provided to %s', | ||
(is_object($html) ? get_class($html) : gettype($html)), | ||
__CLASS__ | ||
)); | ||
} | ||
|
||
$body = new Stream('php://temp', 'wb+'); | ||
$body->write($html); | ||
return $body; | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @see http://github.com/zendframework/zend-diactoros for the canonical source repository | ||
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace Zend\Diactoros\Response; | ||
|
||
trait InjectContentTypeTrait | ||
{ | ||
/** | ||
* Inject the provided Content-Type, if none is already present. | ||
* | ||
* @param string $contentType | ||
* @param array $headers | ||
* @return array Headers with injected Content-Type | ||
*/ | ||
private function injectContentType($contentType, array $headers) | ||
{ | ||
$hasContentType = array_reduce(array_keys($headers), function ($carry, $item) { | ||
return $carry ?: (strtolower($item) === 'content-type'); | ||
}, false); | ||
|
||
if (! $hasContentType) { | ||
$headers['content-type'] = [$contentType]; | ||
} | ||
|
||
return $headers; | ||
} | ||
} |
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,96 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @see http://github.com/zendframework/zend-diactoros for the canonical source repository | ||
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace Zend\Diactoros\Response; | ||
|
||
use ArrayObject; | ||
use InvalidArgumentException; | ||
use Zend\Diactoros\Response; | ||
use Zend\Diactoros\Stream; | ||
|
||
/** | ||
* HTML response. | ||
* | ||
* Allows creating a response by passing an HTML string to the constructor; | ||
* by default, sets a status code of 200 and sets the Content-Type header to | ||
* text/html. | ||
*/ | ||
class JsonResponse extends Response | ||
{ | ||
use InjectContentTypeTrait; | ||
|
||
/** | ||
* Create a JSON response with the given array of data. | ||
* | ||
* If the data provided is null, an empty ArrayObject is used; if the data | ||
* is scalar, it is cast to an array prior to serialization. | ||
* | ||
* Default JSON encoding is performed with the following options, which | ||
* produces RFC4627-compliant JSON, capable of embedding into HTML. | ||
* | ||
* - JSON_HEX_TAG | ||
* - JSON_HEX_APOS | ||
* - JSON_HEX_AMP | ||
* - JSON_HEX_QUOT | ||
* | ||
* @param string $data Data to convert to JSON. | ||
* @param int $status Integer status code for the response; 200 by default. | ||
* @param array $headers Array of headers to use at initialization. | ||
* @param int $encodingOptions JSON encoding options to use. | ||
* @throws InvalidArgumentException if unable to encode the $data to JSON. | ||
*/ | ||
public function __construct($data, $status = 200, array $headers = [], $encodingOptions = 15) | ||
{ | ||
$body = new Stream('php://temp', 'wb+'); | ||
$body->write($this->jsonEncode($data, $encodingOptions)); | ||
|
||
$headers = $this->injectContentType('application/json', $headers); | ||
|
||
parent::__construct($body, $status, $headers); | ||
} | ||
|
||
/** | ||
* Encode the provided data to JSON. | ||
* | ||
* @param mixed $data | ||
* @param int $encodingOptions | ||
* @return string | ||
* @throws InvalidArgumentException if unable to encode the $data to JSON. | ||
*/ | ||
private function jsonEncode($data, $encodingOptions) | ||
{ | ||
if (is_resource($data)) { | ||
throw new InvalidArgumentException('Cannot JSON encode resources'); | ||
} | ||
|
||
if ($data === null) { | ||
// Use an ArrayObject to force an empty JSON object. | ||
$data = new ArrayObject(); | ||
} | ||
|
||
if (is_scalar($data)) { | ||
$data = (array) $data; | ||
} | ||
|
||
// Clear json_last_error() | ||
json_encode(null); | ||
|
||
$json = json_encode($data, $encodingOptions); | ||
|
||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
throw new InvalidArgumentException(sprintf( | ||
'Unable to encode data to JSON in %s: %s', | ||
__CLASS__, | ||
json_last_error_msg() | ||
)); | ||
} | ||
|
||
return $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
Oops, something went wrong.