Skip to content

Commit

Permalink
Support ORM 3
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored and Hikariii committed Mar 8, 2024
1 parent 638bcc1 commit afcb6af
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 50 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@
"require": {
"php": "^7.4 || ^8.0",
"ext-pdo": "*",
"doctrine/dbal": "^3.0",
"doctrine/dbal": "^3.2",
"doctrine/lexer": "^2.0 || ^3.0",
"doctrine/orm": "^2.14"
"doctrine/orm": "^2.19 || ^3"
},
"require-dev": {
"doctrine/annotations": "^1.13",
"doctrine/annotations": "^2",
"doctrine/coding-standard": "^8.0 || ^9.0",
"phpunit/phpunit": "^8.0 || ^9.0",
"psalm/plugin-phpunit": "^0.18",
"slevomat/coding-standard": "^8.4",
"symfony/cache": "^5.4 || ^6.4 || ^7",
"vimeo/psalm": "^5.2",
"webmozart/assert": "^1.11"
},
Expand Down
25 changes: 13 additions & 12 deletions src/Query/AST/Functions/AbstractJsonFunctionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\QueryException;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\TokenType;

abstract class AbstractJsonFunctionNode extends FunctionNode
{
Expand Down Expand Up @@ -40,16 +41,16 @@ abstract class AbstractJsonFunctionNode extends FunctionNode
*/
public function parse(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$parser->match(TokenType::T_IDENTIFIER);
$parser->match(TokenType::T_OPEN_PARENTHESIS);

$argumentParsed = $this->parseArguments($parser, $this->requiredArgumentTypes);

if (!empty($this->optionalArgumentTypes)) {
$this->parseOptionalArguments($parser, $argumentParsed);
}

$parser->match(Lexer::T_CLOSE_PARENTHESIS);
$parser->match(TokenType::T_CLOSE_PARENTHESIS);
}

/**
Expand All @@ -59,10 +60,10 @@ public function parse(Parser $parser): void
*/
protected function parseOptionalArguments(Parser $parser, bool $argumentParsed): void
{
$continueParsing = !$parser->getLexer()->isNextToken(Lexer::T_CLOSE_PARENTHESIS);
$continueParsing = !$parser->getLexer()->isNextToken(TokenType::T_CLOSE_PARENTHESIS);
while ($continueParsing) {
$argumentParsed = $this->parseArguments($parser, $this->optionalArgumentTypes, $argumentParsed);
$continueParsing = $this->allowOptionalArgumentRepeat && $parser->getLexer()->isNextToken(Lexer::T_COMMA);
$continueParsing = $this->allowOptionalArgumentRepeat && $parser->getLexer()->isNextToken(TokenType::T_COMMA);
}
}

Expand All @@ -77,7 +78,7 @@ protected function parseArguments(Parser $parser, array $argumentTypes, bool $ar
{
foreach ($argumentTypes as $argType) {
if ($argumentParsed) {
$parser->match(Lexer::T_COMMA);
$parser->match(TokenType::T_COMMA);
} else {
$argumentParsed = true;
}
Expand Down Expand Up @@ -113,7 +114,7 @@ protected function parseStringLiteral(Parser $parser): Literal
$lexer = $parser->getLexer();
$lookaheadType = $lexer->lookahead->type;

if ($lookaheadType !== Lexer::T_STRING) {
if ($lookaheadType !== TokenType::T_STRING) {
$parser->syntaxError('string');
}

Expand All @@ -131,12 +132,12 @@ protected function parseAlphaNumericLiteral(Parser $parser): Literal
$lookaheadType = $lexer->lookahead->type;

switch ($lookaheadType) {
case Lexer::T_STRING:
case TokenType::T_STRING:
return $this->matchStringLiteral($parser, $lexer);
case Lexer::T_INTEGER:
case Lexer::T_FLOAT:
case TokenType::T_INTEGER:
case TokenType::T_FLOAT:
$parser->match(
$lexer->isNextToken(Lexer::T_INTEGER) ? Lexer::T_INTEGER : Lexer::T_FLOAT
$lexer->isNextToken(TokenType::T_INTEGER) ? TokenType::T_INTEGER : TokenType::T_FLOAT
);

return new Literal(Literal::NUMERIC, $lexer->token->value);
Expand All @@ -147,7 +148,7 @@ protected function parseAlphaNumericLiteral(Parser $parser): Literal

private function matchStringLiteral(Parser $parser, Lexer $lexer): Literal
{
$parser->match(Lexer::T_STRING);
$parser->match(TokenType::T_STRING);
return new Literal(Literal::STRING, $lexer->token->value);
}

Expand Down
10 changes: 5 additions & 5 deletions src/Query/AST/Functions/Mysql/JsonContainsPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql;

use Doctrine\DBAL\Exception;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\TokenType;

/**
* "JSON_CONTAINS_PATH" "(" StringPrimary "," ["one" | "all"] {"," StringPrimary }* ")"
Expand All @@ -22,17 +22,17 @@ class JsonContainsPath extends JsonSearch
*/
public function parse(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$parser->match(TokenType::T_IDENTIFIER);
$parser->match(TokenType::T_OPEN_PARENTHESIS);

$this->parsedArguments[] = $parser->StringPrimary();

$parser->match(Lexer::T_COMMA);
$parser->match(TokenType::T_COMMA);

$this->parsedArguments[] = $this->parsePathMode($parser);

$this->parseOptionalArguments($parser, true);

$parser->match(Lexer::T_CLOSE_PARENTHESIS);
$parser->match(TokenType::T_CLOSE_PARENTHESIS);
}
}
14 changes: 7 additions & 7 deletions src/Query/AST/Functions/Mysql/JsonSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use Doctrine\DBAL\Exception;
use Doctrine\ORM\Query\AST\Node;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\TokenType;

/**
* "JSON_SEARCH" "(" StringPrimary "," ["one" | "all"] "," StringPrimary {"," NewValue { "," StringPrimary }* } ")"
Expand Down Expand Up @@ -38,27 +38,27 @@ class JsonSearch extends MysqlJsonFunctionNode
*/
public function parse(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$parser->match(TokenType::T_IDENTIFIER);
$parser->match(TokenType::T_OPEN_PARENTHESIS);

$this->parsedArguments[] = $parser->StringPrimary();

$parser->match(Lexer::T_COMMA);
$parser->match(TokenType::T_COMMA);

$this->parsedArguments[] = $this->parsePathMode($parser);

$parser->match(Lexer::T_COMMA);
$parser->match(TokenType::T_COMMA);

$this->parsedArguments[] = $parser->StringPrimary();

$continueParsing = !$parser->getLexer()->isNextToken(Lexer::T_CLOSE_PARENTHESIS);
$continueParsing = !$parser->getLexer()->isNextToken(TokenType::T_CLOSE_PARENTHESIS);
if ($continueParsing) {
$this->parseArguments($parser, [self::VALUE_ARG, self::STRING_PRIMARY_ARG], true);
}

$this->parseOptionalArguments($parser, true);

$parser->match(Lexer::T_CLOSE_PARENTHESIS);
$parser->match(TokenType::T_CLOSE_PARENTHESIS);
}

/**
Expand Down
12 changes: 9 additions & 3 deletions tests/Entities/Blank.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

namespace Scienta\DoctrineJsonFunctions\Tests\Entities;

/**
* @Entity
*/
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;

/** @Entity */
#[Entity]
class Blank
{
/** @Id @Column(type="string") @GeneratedValue */
#[Id, Column(type: Types::STRING), GeneratedValue]
public $id;
}
22 changes: 13 additions & 9 deletions tests/Entities/JsonData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@

namespace Scienta\DoctrineJsonFunctions\Tests\Entities;

/**
* @Entity
*/
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;

/** @Entity */
#[Entity]
class JsonData
{
/** @Id @Column(type="string") @GeneratedValue */
#[Id, Column(type: Types::STRING), GeneratedValue]
public $id;

/**
* @Column(type="json_array")
*/
/** @Column(type="json") */
#[Column(type: Types::JSON)]
public $jsonCol;

/**
* @Column(type="json_array")
*/
/** @Column(type="json") */
#[Column(type: Types::JSON)]
public $jsonData;
}
19 changes: 8 additions & 11 deletions tests/Query/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Scienta\DoctrineJsonFunctions\Tests\Query;

use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMSetup;
use Scienta\DoctrineJsonFunctions\Tests\DoctrineJsonTestcase;
use Scienta\DoctrineJsonFunctions\Tests\Mocks;
use Webmozart\Assert\Assert;
Expand All @@ -18,25 +20,20 @@ abstract class DbTestCase extends DoctrineJsonTestcase
/** @var Configuration */
protected $configuration;

/**
* @throws \Doctrine\ORM\ORMException
*/
public function setUp(): void
{
$this->configuration = new Configuration();
$this->configuration->setProxyDir(__DIR__ . '/Proxies');
$this->configuration->setProxyNamespace('DoctrineExtensions\Tests\Proxies');
$this->configuration->setAutoGenerateProxyClasses(true);
$this->configuration->setMetadataDriverImpl($this->configuration->newDefaultAnnotationDriver([]));
$this->configuration = PHP_VERSION_ID >= 80000
? ORMSetup::createAttributeMetadataConfiguration([], true, __DIR__ . '/Proxies')
: ORMSetup::createAnnotationMetadataConfiguration([], true, __DIR__ . '/Proxies');

$conn = [
$conn = DriverManager::getConnection([
'driverClass' => Mocks\DriverMock::class,
'wrapperClass' => Mocks\ConnectionMock::class,
'user' => 'john',
'password' => 'wayne'
];
], $this->configuration);

$this->entityManager = EntityManager::create($conn, $this->configuration);
$this->entityManager = new EntityManager($conn, $this->configuration);
}

/**
Expand Down

0 comments on commit afcb6af

Please sign in to comment.