Merge pull request #991 from mnapoli/custom-functions-callback
Ability to define custom functions with callback instead of class name
This commit is contained in:
commit
47ca10076b
@ -3406,10 +3406,13 @@ class Parser
|
|||||||
public function CustomFunctionsReturningNumerics()
|
public function CustomFunctionsReturningNumerics()
|
||||||
{
|
{
|
||||||
// getCustomNumericFunction is case-insensitive
|
// getCustomNumericFunction is case-insensitive
|
||||||
$funcName = strtolower($this->lexer->lookahead['value']);
|
$functionName = strtolower($this->lexer->lookahead['value']);
|
||||||
$funcClass = $this->em->getConfiguration()->getCustomNumericFunction($funcName);
|
$functionClass = $this->em->getConfiguration()->getCustomNumericFunction($functionName);
|
||||||
|
|
||||||
|
$function = is_string($functionClass)
|
||||||
|
? new $functionClass($functionName)
|
||||||
|
: $functionClass($functionName);
|
||||||
|
|
||||||
$function = new $funcClass($funcName);
|
|
||||||
$function->parse($this);
|
$function->parse($this);
|
||||||
|
|
||||||
return $function;
|
return $function;
|
||||||
@ -3442,10 +3445,13 @@ class Parser
|
|||||||
public function CustomFunctionsReturningDatetime()
|
public function CustomFunctionsReturningDatetime()
|
||||||
{
|
{
|
||||||
// getCustomDatetimeFunction is case-insensitive
|
// getCustomDatetimeFunction is case-insensitive
|
||||||
$funcName = $this->lexer->lookahead['value'];
|
$functionName = $this->lexer->lookahead['value'];
|
||||||
$funcClass = $this->em->getConfiguration()->getCustomDatetimeFunction($funcName);
|
$functionClass = $this->em->getConfiguration()->getCustomDatetimeFunction($functionName);
|
||||||
|
|
||||||
|
$function = is_string($functionClass)
|
||||||
|
? new $functionClass($functionName)
|
||||||
|
: $functionClass($functionName);
|
||||||
|
|
||||||
$function = new $funcClass($funcName);
|
|
||||||
$function->parse($this);
|
$function->parse($this);
|
||||||
|
|
||||||
return $function;
|
return $function;
|
||||||
@ -3479,10 +3485,13 @@ class Parser
|
|||||||
public function CustomFunctionsReturningStrings()
|
public function CustomFunctionsReturningStrings()
|
||||||
{
|
{
|
||||||
// getCustomStringFunction is case-insensitive
|
// getCustomStringFunction is case-insensitive
|
||||||
$funcName = $this->lexer->lookahead['value'];
|
$functionName = $this->lexer->lookahead['value'];
|
||||||
$funcClass = $this->em->getConfiguration()->getCustomStringFunction($funcName);
|
$functionClass = $this->em->getConfiguration()->getCustomStringFunction($functionName);
|
||||||
|
|
||||||
|
$function = is_string($functionClass)
|
||||||
|
? new $functionClass($functionName)
|
||||||
|
: $functionClass($functionName);
|
||||||
|
|
||||||
$function = new $funcClass($funcName);
|
|
||||||
$function->parse($this);
|
$function->parse($this);
|
||||||
|
|
||||||
return $function;
|
return $function;
|
||||||
|
72
tests/Doctrine/Tests/ORM/Functional/CustomFunctionsTest.php
Normal file
72
tests/Doctrine/Tests/ORM/Functional/CustomFunctionsTest.php
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Tests\ORM\Functional;
|
||||||
|
|
||||||
|
use Doctrine\ORM\Query;
|
||||||
|
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
|
||||||
|
use Doctrine\ORM\Query\AST\PathExpression;
|
||||||
|
use Doctrine\ORM\Query\Lexer;
|
||||||
|
use Doctrine\ORM\Query\Parser;
|
||||||
|
use Doctrine\ORM\Query\SqlWalker;
|
||||||
|
use Doctrine\Tests\Models\CMS\CmsUser;
|
||||||
|
use Doctrine\Tests\OrmFunctionalTestCase;
|
||||||
|
|
||||||
|
require_once __DIR__ . '/../../TestInit.php';
|
||||||
|
|
||||||
|
class CustomFunctionsTest extends OrmFunctionalTestCase
|
||||||
|
{
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->useModelSet('cms');
|
||||||
|
|
||||||
|
parent::setUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCustomFunctionDefinedWithCallback()
|
||||||
|
{
|
||||||
|
$user = new CmsUser();
|
||||||
|
$user->name = 'Bob';
|
||||||
|
$user->username = 'Dylan';
|
||||||
|
$this->_em->persist($user);
|
||||||
|
$this->_em->flush();
|
||||||
|
|
||||||
|
// Instead of defining the function with the class name, we use a callback
|
||||||
|
$this->_em->getConfiguration()->addCustomStringFunction('FOO', function($funcName) {
|
||||||
|
return new NoOp($funcName);
|
||||||
|
});
|
||||||
|
$this->_em->getConfiguration()->addCustomNumericFunction('BAR', function($funcName) {
|
||||||
|
return new NoOp($funcName);
|
||||||
|
});
|
||||||
|
|
||||||
|
$query = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u'
|
||||||
|
. ' WHERE FOO(u.name) = \'Bob\''
|
||||||
|
. ' AND BAR(1) = 1');
|
||||||
|
|
||||||
|
$users = $query->getResult();
|
||||||
|
|
||||||
|
$this->assertEquals(1, count($users));
|
||||||
|
$this->assertSame($user, $users[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class NoOp extends FunctionNode
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var PathExpression
|
||||||
|
*/
|
||||||
|
private $field;
|
||||||
|
|
||||||
|
public function parse(Parser $parser)
|
||||||
|
{
|
||||||
|
$parser->match(Lexer::T_IDENTIFIER);
|
||||||
|
$parser->match(Lexer::T_OPEN_PARENTHESIS);
|
||||||
|
$this->field = $parser->ArithmeticPrimary();
|
||||||
|
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSql(SqlWalker $sqlWalker)
|
||||||
|
{
|
||||||
|
return $this->field->dispatch($sqlWalker);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user