|
5 | 5 | use ArrayIterator; |
6 | 6 | use Iterator; |
7 | 7 | use Nextras\MultiQueryParser\Exception\RuntimeException; |
| 8 | +use Nextras\MultiQueryParser\Fragment\Comment; |
| 9 | +use Nextras\MultiQueryParser\Fragment\Fragment; |
| 10 | +use Nextras\MultiQueryParser\Fragment\Query; |
| 11 | +use Nextras\MultiQueryParser\Strategy\DropComments; |
8 | 12 | use function feof; |
9 | 13 | use function fopen; |
10 | 14 | use function fread; |
11 | 15 |
|
12 | 16 |
|
13 | 17 | abstract class BaseMultiQueryParser implements IMultiQueryParser |
14 | 18 | { |
| 19 | + private CommentStrategy $commentStrategy; |
| 20 | + |
| 21 | + |
| 22 | + public function __construct(?CommentStrategy $commentStrategy = null) |
| 23 | + { |
| 24 | + $this->commentStrategy = $commentStrategy ?? new DropComments(); |
| 25 | + } |
| 26 | + |
| 27 | + |
15 | 28 | /** |
16 | 29 | * @param positive-int $chunkSize |
17 | 30 | * @return Iterator<string> |
@@ -52,7 +65,32 @@ public function parseString(string $s): Iterator |
52 | 65 | * @param Iterator<string> $stream |
53 | 66 | * @return Iterator<string> |
54 | 67 | */ |
55 | | - abstract public function parseStringStream(Iterator $stream): Iterator; |
| 68 | + public function parseStringStream(Iterator $stream): Iterator |
| 69 | + { |
| 70 | + return $this->commentStrategy->apply($this->parseStringStreamToFragments($stream)); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + /** |
| 75 | + * @param Iterator<string> $stream |
| 76 | + * @return Iterator<Fragment> |
| 77 | + */ |
| 78 | + abstract protected function parseStringStreamToFragments(Iterator $stream): Iterator; |
| 79 | + |
| 80 | + |
| 81 | + /** |
| 82 | + * @return Iterator<Fragment> |
| 83 | + */ |
| 84 | + protected function buildFragments(?string $leadingComments, ?string $query): Iterator |
| 85 | + { |
| 86 | + if ($leadingComments !== null && $leadingComments !== '') { |
| 87 | + yield new Comment($leadingComments); |
| 88 | + } |
| 89 | + |
| 90 | + if ($query !== null && $query !== '') { |
| 91 | + yield new Query($query); |
| 92 | + } |
| 93 | + } |
56 | 94 |
|
57 | 95 |
|
58 | 96 | /** |
|
0 commit comments