Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to define custom functions with callback instead of class name #991

Merged
merged 1 commit into from
May 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions lib/Doctrine/ORM/Query/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3350,10 +3350,13 @@ public function FunctionsReturningNumerics()
public function CustomFunctionsReturningNumerics()
{
// getCustomNumericFunction is case-insensitive
$funcName = strtolower($this->lexer->lookahead['value']);
$funcClass = $this->em->getConfiguration()->getCustomNumericFunction($funcName);
$functionName = strtolower($this->lexer->lookahead['value']);
$functionClass = $this->em->getConfiguration()->getCustomNumericFunction($functionName);

$function = is_string($functionClass)
? new $functionClass($functionName)
: $functionClass($functionName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mnapoli could you change it to call_user_func($functionClass, $functionName) to support non-closure callables on PHP 5.3 too ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(fixed in #1031)


$function = new $funcClass($funcName);
$function->parse($this);

return $function;
Expand Down Expand Up @@ -3381,10 +3384,13 @@ public function FunctionsReturningDatetime()
public function CustomFunctionsReturningDatetime()
{
// getCustomDatetimeFunction is case-insensitive
$funcName = $this->lexer->lookahead['value'];
$funcClass = $this->em->getConfiguration()->getCustomDatetimeFunction($funcName);
$functionName = $this->lexer->lookahead['value'];
$functionClass = $this->em->getConfiguration()->getCustomDatetimeFunction($functionName);

$function = is_string($functionClass)
? new $functionClass($functionName)
: $functionClass($functionName);

$function = new $funcClass($funcName);
$function->parse($this);

return $function;
Expand Down Expand Up @@ -3417,10 +3423,13 @@ public function FunctionsReturningStrings()
public function CustomFunctionsReturningStrings()
{
// getCustomStringFunction is case-insensitive
$funcName = $this->lexer->lookahead['value'];
$funcClass = $this->em->getConfiguration()->getCustomStringFunction($funcName);
$functionName = $this->lexer->lookahead['value'];
$functionClass = $this->em->getConfiguration()->getCustomStringFunction($functionName);

$function = is_string($functionClass)
? new $functionClass($functionName)
: $functionClass($functionName);

$function = new $funcClass($funcName);
$function->parse($this);

return $function;
Expand Down
72 changes: 72 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/CustomFunctionsTest.php
Original file line number Diff line number Diff line change
@@ -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);
}
}