|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Respect/Stringifier. |
| 5 | + * Copyright (c) Henrique Moody <henriquemoody@gmail.com> |
| 6 | + * SPDX-License-Identifier: MIT |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace Respect\Stringifier\Handlers; |
| 12 | + |
| 13 | +use DateTimeInterface; |
| 14 | +use Respect\Stringifier\Handler; |
| 15 | +use Respect\Stringifier\Quoters\StandardQuoter; |
| 16 | + |
| 17 | +use function array_unshift; |
| 18 | + |
| 19 | +final class CompositeHandler implements Handler |
| 20 | +{ |
| 21 | + private const int MAXIMUM_DEPTH = 3; |
| 22 | + private const int MAXIMUM_NUMBER_OF_ITEMS = 5; |
| 23 | + private const int MAXIMUM_NUMBER_OF_PROPERTIES = self::MAXIMUM_NUMBER_OF_ITEMS; |
| 24 | + private const int MAXIMUM_LENGTH = 120; |
| 25 | + |
| 26 | + /** @var array<Handler> */ |
| 27 | + private array $handlers = []; |
| 28 | + |
| 29 | + public function __construct(Handler ...$handlers) |
| 30 | + { |
| 31 | + $this->handlers = $handlers; |
| 32 | + } |
| 33 | + |
| 34 | + public static function create(): self |
| 35 | + { |
| 36 | + $quoter = new StandardQuoter(self::MAXIMUM_LENGTH); |
| 37 | + |
| 38 | + $handler = new self( |
| 39 | + new InfiniteNumberHandler($quoter), |
| 40 | + new NotANumberHandler($quoter), |
| 41 | + new ResourceHandler($quoter), |
| 42 | + new BoolHandler($quoter), |
| 43 | + new NullHandler($quoter), |
| 44 | + new DeclaredHandler($quoter), |
| 45 | + $jsonEncodableHandler = new JsonEncodableHandler(), |
| 46 | + ); |
| 47 | + $handler->prependHandler( |
| 48 | + $arrayHandler = new ArrayHandler( |
| 49 | + $handler, |
| 50 | + $quoter, |
| 51 | + self::MAXIMUM_DEPTH, |
| 52 | + self::MAXIMUM_NUMBER_OF_ITEMS, |
| 53 | + ), |
| 54 | + ); |
| 55 | + $handler->prependHandler( |
| 56 | + new ObjectHandler( |
| 57 | + $handler, |
| 58 | + $quoter, |
| 59 | + self::MAXIMUM_DEPTH, |
| 60 | + self::MAXIMUM_NUMBER_OF_PROPERTIES, |
| 61 | + ), |
| 62 | + ); |
| 63 | + $handler->prependHandler(new CallableHandler($handler, $quoter)); |
| 64 | + $handler->prependHandler( |
| 65 | + new FiberObjectHandler(new CallableHandler($handler, $quoter, closureOnly: false), $quoter), |
| 66 | + ); |
| 67 | + $handler->prependHandler(new EnumerationHandler($quoter)); |
| 68 | + $handler->prependHandler(new ObjectWithDebugInfoHandler($arrayHandler, $quoter)); |
| 69 | + $handler->prependHandler(new ArrayObjectHandler($arrayHandler, $quoter)); |
| 70 | + $handler->prependHandler(new JsonSerializableObjectHandler($jsonEncodableHandler, $quoter)); |
| 71 | + $handler->prependHandler(new StringableObjectHandler($jsonEncodableHandler, $quoter)); |
| 72 | + $handler->prependHandler(new ThrowableObjectHandler($jsonEncodableHandler, $quoter)); |
| 73 | + $handler->prependHandler(new DateTimeHandler($quoter, DateTimeInterface::ATOM)); |
| 74 | + $handler->prependHandler(new IteratorObjectHandler($handler, $quoter)); |
| 75 | + |
| 76 | + return $handler; |
| 77 | + } |
| 78 | + |
| 79 | + public function prependHandler(Handler $handler): void |
| 80 | + { |
| 81 | + array_unshift($this->handlers, $handler); |
| 82 | + } |
| 83 | + |
| 84 | + public function handle(mixed $raw, int $depth): string|null |
| 85 | + { |
| 86 | + foreach ($this->handlers as $handler) { |
| 87 | + $string = $handler->handle($raw, $depth); |
| 88 | + if ($string === null) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + return $string; |
| 93 | + } |
| 94 | + |
| 95 | + return null; |
| 96 | + } |
| 97 | +} |
0 commit comments