vendor/json-mapper/json-mapper/src/JsonMapperBuilder.php line 48

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace JsonMapper;
  4. use JsonMapper\Cache\ArrayCache;
  5. use JsonMapper\Dto\NamedMiddleware;
  6. use JsonMapper\Enums\TextNotation;
  7. use JsonMapper\Exception\BuilderException;
  8. use JsonMapper\Handler\PropertyMapper;
  9. use JsonMapper\Middleware\Attributes\Attributes;
  10. use JsonMapper\Middleware\CaseConversion;
  11. use JsonMapper\Middleware\Debugger;
  12. use JsonMapper\Middleware\DocBlockAnnotations;
  13. use JsonMapper\Middleware\FinalCallback;
  14. use JsonMapper\Middleware\NamespaceResolver;
  15. use JsonMapper\Middleware\Rename\Mapping;
  16. use JsonMapper\Middleware\Rename\Rename;
  17. use JsonMapper\Middleware\TypedProperties;
  18. use Psr\Log\LoggerInterface;
  19. use Psr\SimpleCache\CacheInterface;
  20. /**
  21.  * @template T of \JsonMapper\JsonMapperInterface
  22.  */
  23. class JsonMapperBuilder
  24. {
  25.     /**
  26.      * @psalm-var class-string<T>
  27.      */
  28.     protected $jsonMapperClassName JsonMapper::class;
  29.     /** @var PropertyMapper */
  30.     protected $propertyMapper;
  31.     /** @var CacheInterface */
  32.     protected $defaultCache;
  33.     /** @var NamedMiddleware[] */
  34.     protected $namedMiddleware = [];
  35.     public static function new(): JsonMapperBuilder
  36.     {
  37.         return new JsonMapperBuilder();
  38.     }
  39.     public function __construct()
  40.     {
  41.         $this->withPropertyMapper(new PropertyMapper())
  42.             ->withDefaultCache(new ArrayCache());
  43.     }
  44.     public function build(): JsonMapperInterface
  45.     {
  46.         if (empty($this->namedMiddleware)) {
  47.             throw BuilderException::forBuildingWithoutMiddleware();
  48.         }
  49.         /** @var JsonMapperInterface $mapper */
  50.         $mapper = new $this->jsonMapperClassName();
  51.         $mapper->setPropertyMapper($this->propertyMapper);
  52.         foreach ($this->namedMiddleware as $namedMiddleware) {
  53.             $mapper->push($namedMiddleware->getMiddleware(), $namedMiddleware->getName());
  54.         }
  55.         return $mapper;
  56.     }
  57.     /** @psalm-param class-string<T> $jsonMapperClassName */
  58.     public function withJsonMapperClassName(string $jsonMapperClassName): JsonMapperBuilder
  59.     {
  60.         $reflectedClass = new \ReflectionClass($jsonMapperClassName);
  61.         if (!$reflectedClass->implementsInterface(JsonMapperInterface::class)) {
  62.             throw BuilderException::invalidJsonMapperClassName($jsonMapperClassName);
  63.         }
  64.         $this->jsonMapperClassName $jsonMapperClassName;
  65.         return $this;
  66.     }
  67.     public function withPropertyMapper(PropertyMapper $propertyMapper): JsonMapperBuilder
  68.     {
  69.         $this->propertyMapper $propertyMapper;
  70.         return $this;
  71.     }
  72.     public function withDefaultCache(CacheInterface $defaultCache): JsonMapperBuilder
  73.     {
  74.         $this->defaultCache $defaultCache;
  75.         return $this;
  76.     }
  77.     public function withDocBlockAnnotationsMiddleware(?CacheInterface $cache null): JsonMapperBuilder
  78.     {
  79.         return $this->withMiddleware(
  80.             new DocBlockAnnotations($cache ?: $this->defaultCache),
  81.             DocBlockAnnotations::class
  82.         );
  83.     }
  84.     public function withNamespaceResolverMiddleware(?CacheInterface $cache null): JsonMapperBuilder
  85.     {
  86.         return $this->withMiddleware(
  87.             new NamespaceResolver($cache ?: $this->defaultCache),
  88.             NamespaceResolver::class
  89.         );
  90.     }
  91.     public function withTypedPropertiesMiddleware(?CacheInterface $cache null): JsonMapperBuilder
  92.     {
  93.         return $this->withMiddleware(
  94.             new TypedProperties($cache ?: $this->defaultCache),
  95.             TypedProperties::class
  96.         );
  97.     }
  98.     public function withAttributesMiddleware(): JsonMapperBuilder
  99.     {
  100.         return $this->withMiddleware(new Attributes(), Attributes::class);
  101.     }
  102.     public function withRenameMiddleware(Mapping ...$mapping): JsonMapperBuilder
  103.     {
  104.         return $this->withMiddleware(new Rename(...$mapping), Rename::class);
  105.     }
  106.     public function withCaseConversionMiddleware(
  107.         TextNotation $searchSeparator,
  108.         TextNotation $replacementSeparator
  109.     ): JsonMapperBuilder {
  110.         return $this->withMiddleware(
  111.             new CaseConversion($searchSeparator$replacementSeparator),
  112.             CaseConversion::class
  113.         );
  114.     }
  115.     public function withDebuggerMiddleware(LoggerInterface $logger): JsonMapperBuilder
  116.     {
  117.         return $this->withMiddleware(new Debugger($logger), Debugger::class);
  118.     }
  119.     public function withFinalCallbackMiddleware(
  120.         callable $callback,
  121.         bool $onlyApplyCallBackOnTopLevel true
  122.     ): JsonMapperBuilder {
  123.         return $this->withMiddleware(new FinalCallback($callback$onlyApplyCallBackOnTopLevel), FinalCallback::class);
  124.     }
  125.     public function withMiddleware(callable $middleware, ?string $name null): JsonMapperBuilder
  126.     {
  127.         $fallbackName is_object($middleware) ? get_class($middleware) : '<anonymous>';
  128.         $this->namedMiddleware[] = new NamedMiddleware($middleware$name ?: $fallbackName);
  129.         return $this;
  130.     }
  131. }