mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 08:47:52 +00:00
Merge pull request #287 from skipperbent/v3-development
Response->json now accepts either array or \JsonSerializable (issue: #284)
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Pecee\Http;
|
namespace Pecee\Http;
|
||||||
|
|
||||||
class Response
|
class Response
|
||||||
@@ -82,14 +83,19 @@ class Response
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Json encode array
|
* Json encode
|
||||||
* @param array $value
|
* @param array|\JsonSerializable $value
|
||||||
|
* @throws \InvalidArgumentException;
|
||||||
*/
|
*/
|
||||||
public function json(array $value)
|
public function json($value)
|
||||||
{
|
{
|
||||||
$this->header('Content-Type: application/json');
|
if (($value instanceof \JsonSerializable) === false && is_array($value) === false) {
|
||||||
|
throw new \InvalidArgumentException('Invalid type for parameter "value". Must be of type array or object implementing the \JsonSerializable interface.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->header('Content-type: application/json');
|
||||||
echo json_encode($value);
|
echo json_encode($value);
|
||||||
die();
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user