/** * Get render that returns JSON API response with JSON API Error objects and specified HTTP status code. * * @param int $statusCode * * @return Closure */ protected function getErrorsRender($statusCode) { /** * @param ErrorInterface[] $errors * @param EncoderOptions $encodeOptions * * @return mixed */ return function (array $errors, EncoderOptions $encodeOptions = null) use($statusCode) { $extensionsClosure = $this->extensionsClosure; /** @var SupportedExtensionsInterface $supportedExtensions */ $supportedExtensions = $extensionsClosure(); $content = Encoder::instance([], $encodeOptions)->errors($errors); $mediaType = $this->factory->createMediaType(MediaTypeInterface::JSON_API_TYPE, MediaTypeInterface::JSON_API_SUB_TYPE); return $this->responses->getResponse($statusCode, $mediaType, $content, $supportedExtensions); }; }
/** * Init codec container. * * @return void */ protected function initCodecMatcher() { $jsonApiEncoder = function () { $config = $this->getConfig(); $schemas = isset($config[C::SCHEMAS]) === true ? $config[C::SCHEMAS] : []; $container = $this->schemaContainer = (new SchemaFactory())->createContainer($schemas); $options = isset($config[C::JSON][C::JSON_OPTIONS]) === true ? $config[C::JSON][C::JSON_OPTIONS] : C::JSON_OPTIONS_DEFAULT; $depth = isset($config[C::JSON][C::JSON_DEPTH]) === true ? $config[C::JSON][C::JSON_DEPTH] : C::JSON_DEPTH_DEFAULT; $isShowVersion = isset($config[C::JSON][C::JSON_IS_SHOW_VERSION]) === true ? $config[C::JSON][C::JSON_IS_SHOW_VERSION] : C::JSON_IS_SHOW_VERSION_DEFAULT; $versionMeta = isset($config[C::JSON][C::JSON_VERSION_META]) === true ? $config[C::JSON][C::JSON_VERSION_META] : null; $urlPrefix = isset($config[C::JSON][C::JSON_URL_PREFIX]) === true ? $config[C::JSON][C::JSON_URL_PREFIX] : null; $encoderFactory = new EncoderFactory(); $parametersFactory = new ParametersFactory(); return new Encoder(new DocumentFactory(), $encoderFactory, $encoderFactory, $parametersFactory, $container, new EncoderOptions($options, $urlPrefix, $isShowVersion, $versionMeta, $depth)); }; $this->codecMatcher = new CodecMatcher(); $jsonApiType = $this->parametersFactory->createMediaType(MediaTypeInterface::JSON_API_TYPE, MediaTypeInterface::JSON_API_SUB_TYPE); $this->codecMatcher->registerEncoder($jsonApiType, $jsonApiEncoder); $this->codecMatcher->registerDecoder($jsonApiType, function () { return new ArrayDecoder(); }); }