registerDecoder() public method

Register decoder.
public registerDecoder ( Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface $mediaType, Closure $decoderClosure ) : void
$mediaType Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface
$decoderClosure Closure
return void
 /**
  * @param string $mediaType
  * @return $this
  */
 private function withMediaType($mediaType = MediaTypeInterface::JSON_API_MEDIA_TYPE)
 {
     $mediaType = MediaType::parse(0, $mediaType);
     $this->codecMatcher->registerEncoder($mediaType, function () {
         return Encoder::instance();
     });
     $this->codecMatcher->registerDecoder($mediaType, function () {
         return new DocumentDecoder();
     });
     return $this;
 }
Beispiel #2
0
 /**
  * Register specified decoders
  *
  * @param array $decoders
  * @param CodecMatcherInterface $matcher
  */
 private function registerDecoders(array $decoders, CodecMatcherInterface $matcher)
 {
     foreach ($decoders as $mediaType => $decoderType) {
         $matcher->registerDecoder($this->parseMediaTypeString($mediaType), $this->registry->getDecoder($decoderType));
     }
 }
Beispiel #3
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();
     });
 }