/** * @return EncoderOptions */ private function getEncoderOptions() { // Load JSON formatting options from config $options = array_get($this->integration->getConfig(), C::JSON . '.' . C::JSON_OPTIONS, C::JSON_OPTIONS_DEFAULT); $encodeOptions = new EncoderOptions($options); return $encodeOptions; }
/** * @return CodecMatcherInterface */ protected function getCodecMatcher() { if ($this->codecMatcher === null) { $this->codecMatcher = $this->integration->getFromContainer(CodecMatcherInterface::class); } return $this->codecMatcher; }
/** * Here you can add 'exception -> HTTP code' mapping or custom exception renders. */ private function registerCustomExceptions() { $this->renderContainer->registerHttpCodeMapping([MassAssignmentException::class => Response::HTTP_FORBIDDEN]); // // That's an example of how to create custom response with JSON API Error. // $custom404render = function () { // This render can convert JSON API Error to Response $jsonApiErrorRender = $this->renderContainer->getErrorsRender(Response::HTTP_NOT_FOUND); // Prepare Error object (e.g. take info from the exception) $title = 'Requested item not found'; $error = new Error(null, null, null, null, $title); // Load JSON formatting options from config $opts = array_get($this->integration->getConfig(), C::JSON . '.' . C::JSON_OPTIONS, C::JSON_OPTIONS_DEFAULT); $encodeOptions = new EncoderOptions($opts); // Convert error (note it accepts array of errors) to HTTP response return $jsonApiErrorRender([$error], $encodeOptions); }; $this->renderContainer->registerRender(ModelNotFoundException::class, $custom404render); }
/** * @param IntegrationInterface $integration */ public function registerCodecMatcher(IntegrationInterface $integration) { // register factory $factory = $this->createFactory(); $integration->setInContainer(FactoryInterface::class, $factory); // register config $config = $integration->getConfig(); $integration->setInContainer(C::class, $config); // register schemas $schemaContainer = $this->createSchemaContainer($config, $factory); $integration->setInContainer(ContainerInterface::class, $schemaContainer); // register codec matcher $codecMatcher = $this->createCodecMatcher($config, $factory, $schemaContainer); $integration->setInContainer(CodecMatcherInterface::class, $codecMatcher); }
/** * Get response for invalid authentication credentials. * * @return Response */ protected function getUnauthorizedResponse() { $authHeaderValue = $this->realm === null ? static::AUTHENTICATION_SCHEME : static::AUTHENTICATION_SCHEME . ' realm="' . $this->realm . '"'; return $this->integration->createResponse(null, Response::HTTP_UNAUTHORIZED, [self::HEADER_WWW_AUTHENTICATE => $authHeaderValue]); }
/** * @return Response */ protected function createForbiddenResponse() { $response = $this->integration->createResponse(null, Response::HTTP_FORBIDDEN, []); return $response; }
/** * Init integrations with JSON API implementation. * * @param IntegrationInterface $integration * * @return void */ private function initJsonApiSupport(IntegrationInterface $integration) { $this->integration = $integration; /** @var FactoryInterface $factory */ $factory = $this->getIntegration()->getFromContainer(FactoryInterface::class); $this->codecMatcher = $integration->getFromContainer(CodecMatcherInterface::class); $this->exceptionThrower = $integration->getFromContainer(ExceptionThrowerInterface::class); $this->parametersParser = $factory->createParametersParser(); $this->supportedExtensions = $factory->createSupportedExtensions($this->extensions); $this->parametersChecker = $factory->createParametersChecker($this->exceptionThrower, $this->codecMatcher, $this->allowUnrecognizedParams, $this->allowedIncludePaths, $this->allowedFieldSetTypes, $this->allowedSortFields, $this->allowedPagingParameters, $this->allowedFilteringParameters); // information about extensions supported by the current controller might be used in exception handler $integration->setInContainer(SupportedExtensionsInterface::class, $this->supportedExtensions); }