Remove the scienta/doctrine-json-functions dependency

This commit is contained in:
Jacob Tobiasz 2024-03-18 10:00:38 +01:00
parent ddd48306c6
commit 3780629ab8
No known key found for this signature in database
GPG key ID: 3F84290201B006E0
4 changed files with 218 additions and 5 deletions

View file

@ -71,7 +71,6 @@
"psr/http-message": "^1.0",
"psr/log": "^2.0",
"ramsey/uuid": "^4.0",
"scienta/doctrine-json-functions": "*",
"sonata-project/block-bundle": "^4.2 || ^5.0",
"stof/doctrine-extensions-bundle": "^1.4",
"sylius-labs/association-hydrator": "^1.1 || ^1.2",

View file

@ -0,0 +1,176 @@
<?php
namespace Sylius\Bundle\ProductBundle\Doctrine\Query\Function;
use Doctrine\DBAL\Exception;
use Doctrine\ORM\Query\AST\ASTException;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\AST\Literal;
use Doctrine\ORM\Query\AST\Node;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\QueryException;
use Doctrine\ORM\Query\SqlWalker;
abstract class AbstractJsonFunctionNode extends FunctionNode
{
/** @var string|null */
public const FUNCTION_NAME = null;
protected const ALPHA_NUMERIC = 'alphaNumeric';
protected const STRING_PRIMARY_ARG = 'stringPrimary';
protected const STRING_ARG = 'string';
protected const VALUE_ARG = 'newValue';
/** @var string[] */
protected array $requiredArgumentTypes = [];
/** @var string[] */
protected array $optionalArgumentTypes = [];
protected bool $allowOptionalArgumentRepeat = false;
/** @var array<Node|null> */
protected array $parsedArguments = [];
/**
* @throws QueryException
*/
public function parse(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$argumentParsed = $this->parseArguments($parser, $this->requiredArgumentTypes);
if (!empty($this->optionalArgumentTypes)) {
$this->parseOptionalArguments($parser, $argumentParsed);
}
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
/**
* @param Parser $parser
* @param bool $argumentParsed
* @throws QueryException
*/
protected function parseOptionalArguments(Parser $parser, bool $argumentParsed): void
{
$continueParsing = !$parser->getLexer()->isNextToken(Lexer::T_CLOSE_PARENTHESIS);
while ($continueParsing) {
$argumentParsed = $this->parseArguments($parser, $this->optionalArgumentTypes, $argumentParsed);
$continueParsing = $this->allowOptionalArgumentRepeat && $parser->getLexer()->isNextToken(Lexer::T_COMMA);
}
}
/**
* @param string[] $argumentTypes
* @throws QueryException
*/
protected function parseArguments(Parser $parser, array $argumentTypes, bool $argumentParsed = false): bool
{
foreach ($argumentTypes as $argType) {
if ($argumentParsed) {
$parser->match(Lexer::T_COMMA);
} else {
$argumentParsed = true;
}
$this->parsedArguments[] = match ($argType) {
self::STRING_PRIMARY_ARG => $parser->StringPrimary(),
self::STRING_ARG => $this->parseStringLiteral($parser),
self::ALPHA_NUMERIC => $this->parseAlphaNumericLiteral($parser),
self::VALUE_ARG => $parser->NewValue(),
default => throw QueryException::semanticalError(sprintf('Unknown function argument type %s for %s()', $argType, static::FUNCTION_NAME)),
};
}
return $argumentParsed;
}
/**
* @throws QueryException
*/
protected function parseStringLiteral(Parser $parser): Literal
{
$lexer = $parser->getLexer();
$lookaheadType = $lexer->lookahead->type;
if ($lookaheadType != Lexer::T_STRING) {
$parser->syntaxError('string');
}
return $this->matchStringLiteral($parser, $lexer);
}
/**
* @param Parser $parser
* @return Literal
* @throws QueryException
*/
protected function parseAlphaNumericLiteral(Parser $parser): Literal
{
$lexer = $parser->getLexer();
$lookaheadType = $lexer->lookahead->type;
switch ($lookaheadType) {
case Lexer::T_STRING:
return $this->matchStringLiteral($parser, $lexer);
case Lexer::T_INTEGER:
case Lexer::T_FLOAT:
$parser->match(
$lexer->isNextToken(Lexer::T_INTEGER) ? Lexer::T_INTEGER : Lexer::T_FLOAT
);
return new Literal(Literal::NUMERIC, $lexer->token->value);
default:
$parser->syntaxError('numeric');
}
}
private function matchStringLiteral(Parser $parser, Lexer $lexer): Literal
{
$parser->match(Lexer::T_STRING);
return new Literal(Literal::STRING, $lexer->token->value);
}
/**
* @throws ASTException
* @throws Exception
*/
public function getSql(SqlWalker $sqlWalker): string
{
$this->validatePlatform($sqlWalker);
$args = [];
foreach ($this->parsedArguments as $parsedArgument) {
if ($parsedArgument === null) {
$args[] = 'NULL';
} else {
$args[] = $parsedArgument->dispatch($sqlWalker);
}
}
return $this->getSqlForArgs($args);
}
/**
* @param string[] $arguments
*/
protected function getSqlForArgs(array $arguments): string
{
return sprintf('%s(%s)', $this->getSQLFunction(), implode(', ', $arguments));
}
protected function getSQLFunction(): string
{
return static::FUNCTION_NAME;
}
/**
* @param SqlWalker $sqlWalker
* @throws Exception
*/
abstract protected function validatePlatform(SqlWalker $sqlWalker): void;
}

View file

@ -0,0 +1,40 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Bundle\ProductBundle\Doctrine\Query\Function;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\ORM\Query\SqlWalker;
abstract class AbstractPostgresqlJsonFunctionNode extends AbstractJsonFunctionNode
{
/**
* @param SqlWalker $sqlWalker
* @throws Exception
*/
protected function validatePlatform(SqlWalker$sqlWalker): void
{
if (!$sqlWalker->getConnection()->getDatabasePlatform() instanceof PostgreSQLPlatform) {
throw Exception::notSupported(static::FUNCTION_NAME);
}
}
/**
* @return string
*/
protected function getSQLFunction(): string
{
return strtolower(static::FUNCTION_NAME);
}
}

View file

@ -13,17 +13,15 @@ declare(strict_types=1);
namespace Sylius\Bundle\ProductBundle\Doctrine\Query\Function;
use Scienta\DoctrineJsonFunctions\Query\AST\Functions\Postgresql\PostgresqlJsonFunctionNode;
/**
* "JSONB_ARRAY_ELEMENTS_TEXT" "(" StringPrimary ")"
*/
final class JsonbArrayElementsText extends PostgresqlJsonFunctionNode
final class JsonbArrayElementsText extends AbstractPostgresqlJsonFunctionNode
{
public const FUNCTION_NAME = 'JSONB_ARRAY_ELEMENTS_TEXT';
/** @var string[] */
protected $requiredArgumentTypes = [self::STRING_PRIMARY_ARG];
protected array $requiredArgumentTypes = [self::STRING_PRIMARY_ARG];
protected function getSqlForArgs(array $arguments): string
{