|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MariaStan\Database\FunctionInfo; |
| 6 | + |
| 7 | +use MariaStan\Analyser\AnalyserConditionTypeEnum; |
| 8 | +use MariaStan\Analyser\ExprTypeResult; |
| 9 | +use MariaStan\Ast\Expr\FunctionCall\FunctionCall; |
| 10 | +use MariaStan\Parser\Exception\ParserException; |
| 11 | +use MariaStan\Schema\DbType\DbTypeEnum; |
| 12 | +use MariaStan\Schema\DbType\IntType; |
| 13 | +use MariaStan\Schema\DbType\NullType; |
| 14 | + |
| 15 | +use function count; |
| 16 | +use function in_array; |
| 17 | + |
| 18 | +final class Year implements FunctionInfo |
| 19 | +{ |
| 20 | + /** @inheritDoc */ |
| 21 | + public function getSupportedFunctionNames(): array |
| 22 | + { |
| 23 | + return ['YEAR']; |
| 24 | + } |
| 25 | + |
| 26 | + public function getFunctionType(): FunctionTypeEnum |
| 27 | + { |
| 28 | + return FunctionTypeEnum::SIMPLE; |
| 29 | + } |
| 30 | + |
| 31 | + public function checkSyntaxErrors(FunctionCall $functionCall): void |
| 32 | + { |
| 33 | + $args = $functionCall->getArguments(); |
| 34 | + $argCount = count($args); |
| 35 | + |
| 36 | + if ($argCount !== 1) { |
| 37 | + throw new ParserException( |
| 38 | + FunctionInfoHelper::createArgumentCountErrorMessageFixed( |
| 39 | + $functionCall->getFunctionName(), |
| 40 | + 1, |
| 41 | + $argCount, |
| 42 | + ), |
| 43 | + ); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** @inheritDoc */ |
| 48 | + public function getInnerConditions(?AnalyserConditionTypeEnum $condition, array $arguments): array |
| 49 | + { |
| 50 | + // TRUTHY(YEAR(A)) => NOT_NULL(A) |
| 51 | + // FALSY(YEAR(A)) => NOT_NULL(A) |
| 52 | + // NOT_NULL(YEAR(A)) => NOT_NULL(A) |
| 53 | + // NULL(YEAR(A)) => [] |
| 54 | + $innerCondition = match ($condition) { |
| 55 | + null, AnalyserConditionTypeEnum::NULL => null, |
| 56 | + default => AnalyserConditionTypeEnum::NOT_NULL, |
| 57 | + }; |
| 58 | + |
| 59 | + return [$innerCondition]; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @inheritDoc |
| 64 | + * @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter |
| 65 | + */ |
| 66 | + public function getReturnType( |
| 67 | + FunctionCall $functionCall, |
| 68 | + array $argumentTypes, |
| 69 | + ?AnalyserConditionTypeEnum $condition, |
| 70 | + bool $isNonEmptyAggResultSet, |
| 71 | + ): ExprTypeResult { |
| 72 | + $date = $argumentTypes[0]; |
| 73 | + $isNullable = $date->isNullable || $date->type::getTypeEnum() !== DbTypeEnum::DATETIME; |
| 74 | + $type = in_array($date->type::getTypeEnum(), [DbTypeEnum::DATETIME, DbTypeEnum::VARCHAR], true) |
| 75 | + ? new IntType() |
| 76 | + : new NullType(); |
| 77 | + |
| 78 | + return new ExprTypeResult($type, $isNullable, null, $date->knowledgeBase); |
| 79 | + } |
| 80 | +} |
0 commit comments