Skip to content
This repository has been archived by the owner on Feb 5, 2019. It is now read-only.

Commit

Permalink
Merge pull request #23 from CodeIncHQ/2.x
Browse files Browse the repository at this point in the history
v2.1.2
  • Loading branch information
Joan Fabrégat authored Oct 12, 2018
2 parents 37c2c74 + 2025df8 commit d4bb6bc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "codeinc/psr7-responses",
"version": "2.1.1",
"version": "2.1.2",
"description": "A collection of PSR-7 responses",
"homepage": "https://github.com/CodeIncHQ/Psr7Responses",
"type": "library",
"license": "MIT",
"require": {
"php": ">=7.1",
"php": ">=7.2",
"ext-json": "*",
"psr/http-message": "^1.0",
"guzzlehttp/psr7": "^1.4",
Expand Down
36 changes: 35 additions & 1 deletion src/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class JsonResponse extends Response
private $json;

/**
* TextResponse constructor.
* JsonResponse constructor.
*
* @param string $json
* @param int $code
Expand All @@ -60,6 +60,40 @@ public function __construct(string $json, int $code = 200, string $reasonPhrase
parent::__construct($code, $headers, $json, $version, $reasonPhrase);
}

/**
* @param array $array
* @param int $code
* @param string $reasonPhrase
* @param array $headers
* @param string $version
* @return JsonResponse
*/
public static function fromArray(array $array, int $code = 200, string $reasonPhrase = '',
array $headers = self::DEFAULT_HEADERS, string $version = '1.1'):self
{
if (($json = json_encode($array)) === false) {
throw new \RuntimeException("Error while encoding the array to JSON using json_encode().");
}
return new self($json, $code, $reasonPhrase, $headers, $version);
}

/**
* @param object $object
* @param int $code
* @param string $reasonPhrase
* @param array $headers
* @param string $version
* @return JsonResponse
*/
public static function fromObject(object $object, int $code = 200, string $reasonPhrase = '',
array $headers = self::DEFAULT_HEADERS, string $version = '1.1'):self
{
if (($json = json_encode($object)) === false) {
throw new \RuntimeException("Error while encoding the object to JSON using json_encode().");
}
return new self($json, $code, $reasonPhrase, $headers, $version);
}

/**
* Returns the raw JSON string
*
Expand Down
6 changes: 3 additions & 3 deletions src/LocalFileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class LocalFileResponse extends FileResponse
* @param string $version
* @throws \CodeInc\MediaTypes\Exceptions\MediaTypesException
*/
public function __construct(string $filePath, int $code = 200, string $reasonPhrase = '', ?string $fileName = null,
?string $contentType = null, ?int $contentLength = null, bool $asAttachment = true,
array $headers = [], string $version = '1.1')
public function __construct(string $filePath, int $code = 200, string $reasonPhrase = '',
?string $fileName = null, ?string $contentType = null, ?int $contentLength = null,
bool $asAttachment = true, array $headers = [], string $version = '1.1')
{
// opening the local file
if (!is_file($filePath)) {
Expand Down

0 comments on commit d4bb6bc

Please sign in to comment.