registerEncoder() public method

Register encoder.
public registerEncoder ( Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface $mediaType, Closure $encoderClosure ) : void
$mediaType Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface
$encoderClosure 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;
 }
Esempio n. 2
0
 /**
  * Register specified encoders
  *
  * @param array $encoders
  * @param CodecMatcherInterface $matcher
  */
 private function registerEncoders(array $encoders, CodecMatcherInterface $matcher)
 {
     foreach ($encoders as $mediaType => $encoderType) {
         $matcher->registerEncoder($this->parseMediaTypeString($mediaType), $this->registry->getEncoder($encoderType));
     }
 }
Esempio n. 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();
     });
 }