예제 #1
0
 /**
  * @param array|object|null                $data
  * @param EncodingParametersInterface|null $parameters
  *
  * @return EncodingParametersInterface
  */
 private function getEncodingParameters($data, EncodingParametersInterface $parameters = null)
 {
     if (empty($data) === true && $parameters === null) {
         return $this->parametersFactory->createEncodingParameters();
     } elseif ($parameters !== null && $parameters->getIncludePaths() !== null) {
         return $parameters;
     } else {
         $schema = $this->container->getSchema(is_array($data) ? $data[0] : $data);
         $includePaths = $schema->getIncludePaths();
         $fieldSets = $parameters === null ? null : $parameters->getFieldSets();
         return $this->parametersFactory->createEncodingParameters($includePaths, $fieldSets);
     }
 }
예제 #2
0
 /**
  * @param array $parameters
  *
  * @return SortParameterInterface[]|null
  */
 protected function getSortParameters(array $parameters)
 {
     $sortParams = null;
     $sortParam = $this->getParamOrNull($parameters, self::PARAM_SORT);
     if ($sortParam !== null) {
         foreach (explode(',', $sortParam) as $param) {
             $isDesc = false;
             empty($param) === false ? $isDesc = $param[0] === '-' : $this->exceptionThrower->throwBadRequest();
             $sortField = ltrim($param, '+-');
             empty($sortField) === false ?: $this->exceptionThrower->throwBadRequest();
             $sortParams[] = $this->factory->createSortParam($sortField, $isDesc === false);
         }
     }
     return $sortParams;
 }
예제 #3
0
 /**
  * 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);
     };
 }
예제 #4
0
 /**
  * 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();
     });
 }