|
20 | 20 |
|
21 | 21 | use danog\MadelineProto\FileRefExtractor\FieldExtractorOp; |
22 | 22 | use danog\MadelineProto\FileRefExtractor\TLContext; |
| 23 | +use danog\MadelineProto\FileRefExtractor\TypedOp; |
23 | 24 | use Webmozart\Assert\Assert; |
24 | 25 |
|
25 | | -final readonly class ExtractFromHereOp implements FieldExtractorOp |
| 26 | +final readonly class ExtractFromHereOp extends FieldExtractorOp |
26 | 27 | { |
27 | | - public function __construct( |
28 | | - /** @var string[] */ |
29 | | - public readonly array $path, |
30 | | - public readonly bool $isFlag = false, |
31 | | - public readonly ?FieldExtractorOp $ifEmptyFlag = null, |
32 | | - ) { |
33 | | - if ($ifEmptyFlag !== null) { |
34 | | - Assert::true($isFlag); |
35 | | - } |
36 | | - } |
37 | | - |
38 | | - public function normalize(array $stack, string $current): ?\danog\MadelineProto\FileRefExtractor\TypedOp |
| 28 | + public function normalize(array $stack, string $current, bool $ignoreFlag): ?\danog\MadelineProto\FileRefExtractor\TypedOp |
39 | 29 | { |
40 | | - $if = $this->ifEmptyFlag?->normalize($stack, $current); |
41 | | - if ($if === null && $this->ifEmptyFlag !== null) { |
42 | | - return null; |
| 30 | + $new = []; |
| 31 | + foreach ($this->path as $i => $part) { |
| 32 | + if ($ignoreFlag && \array_key_exists(2, $part) && $part[2] === null) { |
| 33 | + return null; |
| 34 | + } |
| 35 | + if (isset($part[2]) && $part[2] instanceof TypedOp) { |
| 36 | + $n = $part[2]->normalize($stack, $current, $ignoreFlag); |
| 37 | + if ($n === null) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + $part[2] = $n; |
| 41 | + } |
| 42 | + $new[$i] = $part; |
43 | 43 | } |
44 | | - Assert::eq($current, $this->path[0]); |
| 44 | + Assert::eq($current, $this->path[0][0]); |
45 | 45 | return new self( |
46 | | - [...$stack, ...$this->path], |
47 | | - $this->isFlag, |
48 | | - $if |
| 46 | + [...$stack, ...$new], |
49 | 47 | ); |
50 | 48 | } |
51 | 49 |
|
52 | | - public function getType(TLContext $tl): string |
53 | | - { |
54 | | - $t = $tl->getTypeAtPosition($this); |
55 | | - if ($this->ifEmptyFlag !== null) { |
56 | | - Assert::eq($this->ifEmptyFlag->getType($tl), $t); |
57 | | - } |
58 | | - return $t; |
59 | | - } |
60 | | - |
61 | | - public function extend(string ...$path): self |
62 | | - { |
63 | | - return new self(...$this->path, ...$path); |
64 | | - } |
65 | | - |
66 | 50 | public function build(TLContext $tl): array |
67 | 51 | { |
68 | 52 | // Validate |
69 | 53 | $this->getType($tl); |
70 | 54 |
|
| 55 | + $new = []; |
| 56 | + foreach ($this->path as $part) { |
| 57 | + if (isset($part[2]) && $part[2] !== true) { |
| 58 | + $part[2] = $part[2]->build($tl); |
| 59 | + } |
| 60 | + $new[] = $part; |
| 61 | + } |
71 | 62 | return [ |
72 | 63 | 'op' => 'extractFromHere', |
73 | | - 'isFlag' => $this->isFlag, |
74 | | - 'ifFlagEmptyUse' => $this->ifEmptyFlag?->build($tl), |
75 | | - 'path' => $this->path, |
| 64 | + 'path' => $new, |
76 | 65 | ]; |
77 | 66 | } |
78 | 67 | } |
0 commit comments