Mercurial > hg > isophonics-drupal-site
diff vendor/symfony/serializer/Encoder/JsonEncode.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/symfony/serializer/Encoder/JsonEncode.php Wed Nov 29 16:09:58 2017 +0000 @@ -0,0 +1,68 @@ +<?php + +/* + * This file is part of the Symfony package. + * + * (c) Fabien Potencier <fabien@symfony.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Encoder; + +use Symfony\Component\Serializer\Exception\UnexpectedValueException; + +/** + * Encodes JSON data. + * + * @author Sander Coolen <sander@jibber.nl> + */ +class JsonEncode implements EncoderInterface +{ + private $options; + private $lastError = JSON_ERROR_NONE; + + public function __construct($bitmask = 0) + { + $this->options = $bitmask; + } + + /** + * Encodes PHP data to a JSON string. + * + * {@inheritdoc} + */ + public function encode($data, $format, array $context = array()) + { + $context = $this->resolveContext($context); + + $encodedJson = json_encode($data, $context['json_encode_options']); + + if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) { + throw new UnexpectedValueException(json_last_error_msg()); + } + + return $encodedJson; + } + + /** + * {@inheritdoc} + */ + public function supportsEncoding($format) + { + return JsonEncoder::FORMAT === $format; + } + + /** + * Merge default json encode options with context. + * + * @param array $context + * + * @return array + */ + private function resolveContext(array $context = array()) + { + return array_merge(array('json_encode_options' => $this->options), $context); + } +}