|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TypeLang\Mapper\Type\Builder; |
| 6 | + |
| 7 | +use TypeLang\Mapper\Exception\Definition\Template\Hint\TemplateArgumentHintsNotSupportedException; |
| 8 | +use TypeLang\Mapper\Exception\Definition\Template\MissingTemplateArgumentsException; |
| 9 | +use TypeLang\Mapper\Exception\Definition\Template\MissingTemplateArgumentsInRangeException; |
| 10 | +use TypeLang\Mapper\Exception\Definition\Template\TemplateArgumentsNotSupportedException; |
| 11 | +use TypeLang\Mapper\Exception\Definition\Template\TooManyTemplateArgumentsException; |
| 12 | +use TypeLang\Mapper\Exception\Definition\Template\TooManyTemplateArgumentsInRangeException; |
| 13 | +use TypeLang\Parser\Node\Stmt\NamedTypeNode; |
| 14 | +use TypeLang\Parser\Node\Stmt\Template\TemplateArgumentNode; |
| 15 | + |
| 16 | +final class Assert |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @throws TemplateArgumentsNotSupportedException |
| 20 | + */ |
| 21 | + public static function expectNoTemplateArguments(NamedTypeNode $stmt): void |
| 22 | + { |
| 23 | + if ($stmt->arguments === null) { |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + throw TemplateArgumentsNotSupportedException::becauseTooManyArguments($stmt); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @throws TooManyTemplateArgumentsException |
| 32 | + * @throws MissingTemplateArgumentsException |
| 33 | + * @throws TemplateArgumentsNotSupportedException |
| 34 | + * @throws TooManyTemplateArgumentsInRangeException |
| 35 | + * @throws MissingTemplateArgumentsInRangeException |
| 36 | + */ |
| 37 | + public static function expectTemplateArgumentsCountInRange(NamedTypeNode $stmt, int $from, int $to): void |
| 38 | + { |
| 39 | + $from = \max(0, $from); |
| 40 | + $to = \max(0, $to); |
| 41 | + |
| 42 | + if ($from === $to) { |
| 43 | + self::expectTemplateArgumentsCount($stmt, $from); |
| 44 | + |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + if ($from > $to) { |
| 49 | + [$from, $to] = [$to, $from]; |
| 50 | + } |
| 51 | + |
| 52 | + if ($from < 1) { |
| 53 | + self::expectTemplateArgumentsCountLessOrEqualThan($stmt, $to); |
| 54 | + |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + $actualArgumentsCount = $stmt->arguments?->count() ?? 0; |
| 59 | + |
| 60 | + /** @var int<1, max> $to */ |
| 61 | + if ($actualArgumentsCount > $to) { |
| 62 | + throw TooManyTemplateArgumentsInRangeException::becauseArgumentsCountRequired($from, $to, $stmt); |
| 63 | + } |
| 64 | + |
| 65 | + if ($actualArgumentsCount < $from) { |
| 66 | + throw MissingTemplateArgumentsInRangeException::becauseArgumentsCountRequired($from, $to, $stmt); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @throws TooManyTemplateArgumentsException |
| 72 | + * @throws MissingTemplateArgumentsException |
| 73 | + * @throws TemplateArgumentsNotSupportedException |
| 74 | + */ |
| 75 | + public static function expectTemplateArgumentsCount(NamedTypeNode $stmt, int $count): void |
| 76 | + { |
| 77 | + if ($count < 1) { |
| 78 | + self::expectNoTemplateArguments($stmt); |
| 79 | + |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + if (($stmt->arguments?->count() ?? 0) > $count) { |
| 84 | + throw TooManyTemplateArgumentsException::becauseArgumentsCountRequired($count, $stmt); |
| 85 | + } |
| 86 | + |
| 87 | + throw MissingTemplateArgumentsException::becauseArgumentsCountRequired($count, $stmt); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @throws TooManyTemplateArgumentsException |
| 92 | + */ |
| 93 | + public static function expectTemplateArgumentsCountGreaterThan(NamedTypeNode $stmt, int $count): void |
| 94 | + { |
| 95 | + self::expectTemplateArgumentsCountGreaterOrEqualThan($stmt, $count + 1); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @throws TooManyTemplateArgumentsException |
| 100 | + */ |
| 101 | + public static function expectTemplateArgumentsCountGreaterOrEqualThan(NamedTypeNode $stmt, int $count): void |
| 102 | + { |
| 103 | + $actualArgumentsCount = $stmt->arguments?->count() ?? 0; |
| 104 | + |
| 105 | + if ($count <= $actualArgumentsCount) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + throw TooManyTemplateArgumentsException::becauseArgumentsCountLessThan($count, $stmt); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @throws MissingTemplateArgumentsException |
| 114 | + */ |
| 115 | + public static function expectTemplateArgumentsCountLessThan(NamedTypeNode $stmt, int $count): void |
| 116 | + { |
| 117 | + self::expectTemplateArgumentsCountLessOrEqualThan($stmt, $count - 1); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @throws MissingTemplateArgumentsException |
| 122 | + * @throws TemplateArgumentsNotSupportedException |
| 123 | + */ |
| 124 | + public static function expectTemplateArgumentsCountLessOrEqualThan(NamedTypeNode $stmt, int $count): void |
| 125 | + { |
| 126 | + if ($count < 1) { |
| 127 | + self::expectNoTemplateArguments($stmt); |
| 128 | + |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + $actualArgumentsCount = $stmt->arguments?->count() ?? 0; |
| 133 | + |
| 134 | + if ($count >= $actualArgumentsCount) { |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + throw MissingTemplateArgumentsException::becauseArgumentsCountLessThan($count, $stmt); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @throws TemplateArgumentHintsNotSupportedException |
| 143 | + */ |
| 144 | + public static function expectNoTemplateArgumentHints(NamedTypeNode $stmt, TemplateArgumentNode $argument): void |
| 145 | + { |
| 146 | + // Skip in case of argument is not a part of type |
| 147 | + if (!\in_array($argument, $stmt->arguments->items ?? [], true)) { |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + if ($argument->hint === null) { |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + throw TemplateArgumentHintsNotSupportedException::becauseTooManyHints( |
| 156 | + argument: $argument, |
| 157 | + type: $stmt, |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * @throws TemplateArgumentHintsNotSupportedException |
| 163 | + */ |
| 164 | + public static function expectNoAnyTemplateArgumentsHints(NamedTypeNode $stmt): void |
| 165 | + { |
| 166 | + foreach ($stmt->arguments->items ?? [] as $argument) { |
| 167 | + self::expectNoTemplateArgumentHints($stmt, $argument); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments