|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleAsFuck\ApiToolkit\Service\Common; |
| 6 | + |
| 7 | +use Psr\Http\Message\StreamInterface; |
| 8 | +use SimpleAsFuck\Validator\Factory\Exception; |
| 9 | +use SimpleAsFuck\Validator\Factory\UnexpectedValueException; |
| 10 | +use SimpleAsFuck\Validator\Model\Validated; |
| 11 | +use SimpleAsFuck\Validator\Rule\General\Rules; |
| 12 | + |
| 13 | +class JsonService |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @param non-empty-string $stringName |
| 17 | + * @param int $jsonDecodeFlags bitmask https://www.php.net/manual/en/function.json-decode.php |
| 18 | + */ |
| 19 | + public static function jsonDecode( |
| 20 | + string $string, |
| 21 | + string $stringName = 'String', |
| 22 | + Exception $exceptionFactory = new UnexpectedValueException(), |
| 23 | + bool $allowInvalidJson = false, |
| 24 | + int $jsonDecodeFlags = 0, |
| 25 | + ): Rules { |
| 26 | + $content = \json_decode($string, flags: $jsonDecodeFlags); |
| 27 | + if (\json_last_error() !== JSON_ERROR_NONE) { |
| 28 | + if ($allowInvalidJson) { |
| 29 | + $content = null; |
| 30 | + } else { |
| 31 | + $truncated = strlen($string) > 200; |
| 32 | + $logString = $truncated ? substr($string, 0, 200) : $string; |
| 33 | + throw $exceptionFactory->create($stringName.' must be valid json, invalid content: \''.$logString.'\''.($truncated ? ' (truncated)' : '')); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return new Rules($exceptionFactory, $stringName.' json', new Validated($content)); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @param non-empty-string $streamName |
| 42 | + * @param int $jsonDecodeFlags bitmask https://www.php.net/manual/en/function.json-decode.php |
| 43 | + * @return \Iterator<int, Rules> |
| 44 | + */ |
| 45 | + public static function jsonlDecode( |
| 46 | + StreamInterface $stream, |
| 47 | + string $streamName = 'Stream content', |
| 48 | + Exception $exceptionFactory = new UnexpectedValueException(), |
| 49 | + bool $allowInvalidJson = false, |
| 50 | + int $jsonDecodeFlags = 0, |
| 51 | + ): \Iterator { |
| 52 | + return new class ($stream, $streamName, $exceptionFactory, $allowInvalidJson, $jsonDecodeFlags) implements \Iterator { |
| 53 | + private int $lineNumber = 0; |
| 54 | + private string $buffer = ''; |
| 55 | + private ?Rules $current = null; |
| 56 | + |
| 57 | + public function __construct( |
| 58 | + private readonly StreamInterface $stream, |
| 59 | + private readonly string $streamName, |
| 60 | + private readonly Exception $exceptionFactory, |
| 61 | + private readonly bool $allowInvalidJson, |
| 62 | + private readonly int $jsonDecodeFlags, |
| 63 | + ) { |
| 64 | + $this->fetch(); |
| 65 | + } |
| 66 | + |
| 67 | + public function key(): ?int |
| 68 | + { |
| 69 | + if ($this->valid()) { |
| 70 | + return $this->lineNumber ?? null; |
| 71 | + } |
| 72 | + |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + public function current(): Rules |
| 77 | + { |
| 78 | + if ($this->valid()) { |
| 79 | + return $this->current ?? throw $this->exceptionFactory->create($this->streamName . ' value ' . $this->lineNumber . ' is not valid stream'); |
| 80 | + } |
| 81 | + |
| 82 | + throw $this->exceptionFactory->create($this->streamName . ' value ' . $this->lineNumber . ' is not valid stream'); |
| 83 | + } |
| 84 | + |
| 85 | + public function next(): void |
| 86 | + { |
| 87 | + $this->fetch(); |
| 88 | + } |
| 89 | + |
| 90 | + public function valid(): bool |
| 91 | + { |
| 92 | + return $this->current !== null || ! $this->stream->eof() || $this->buffer !== ''; |
| 93 | + } |
| 94 | + |
| 95 | + public function rewind(): void |
| 96 | + { |
| 97 | + if ($this->lineNumber <= 1) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + $this->stream->rewind(); |
| 102 | + $this->lineNumber = 0; |
| 103 | + $this->buffer = ''; |
| 104 | + $this->fetch(); |
| 105 | + } |
| 106 | + |
| 107 | + private function fetch(): void |
| 108 | + { |
| 109 | + if (!$this->valid()) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + $this->lineNumber++; |
| 114 | + |
| 115 | + while (!$this->stream->eof()) { |
| 116 | + $this->buffer .= $this->stream->read(1024); |
| 117 | + $endLine = strpos($this->buffer, "\n"); |
| 118 | + if ($endLine !== false) { |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if ($this->buffer === '') { |
| 124 | + $this->current = null; |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + $endLine = strpos($this->buffer, "\n"); |
| 129 | + if ($endLine === false) { |
| 130 | + $line = $this->buffer; |
| 131 | + $this->buffer = ''; |
| 132 | + } else { |
| 133 | + $line = substr($this->buffer, 0, $endLine); |
| 134 | + $this->buffer = substr($this->buffer, $endLine + 1); |
| 135 | + } |
| 136 | + |
| 137 | + $this->current = JsonService::jsonDecode( |
| 138 | + $line, |
| 139 | + $this->streamName . ' value ' . $this->lineNumber, |
| 140 | + $this->exceptionFactory, |
| 141 | + $this->allowInvalidJson, |
| 142 | + $this->jsonDecodeFlags, |
| 143 | + ); |
| 144 | + } |
| 145 | + }; |
| 146 | + } |
| 147 | +} |
0 commit comments