JsonMockResponse.php 921 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpClient\Response;
  11. use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
  12. class JsonMockResponse extends MockResponse
  13. {
  14. /**
  15. * @param mixed $body
  16. * @param mixed[] $info
  17. */
  18. public function __construct($body = [], $info = [])
  19. {
  20. try {
  21. $json = json_encode($body, 0);
  22. } catch (\JsonException $e) {
  23. throw new InvalidArgumentException('JSON encoding failed: '.$e->getMessage(), $e->getCode(), $e);
  24. }
  25. $info['response_headers']['content-type'] = $info['response_headers']['content-type'] ?? 'application/json';
  26. parent::__construct($json, $info);
  27. }
  28. }