This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
namespace DoctrineExtensions\ORM; | ||
|
||
/** | ||
* Provides an easy way to work with aliases for doctrine entities. | ||
* | ||
* Allows chaining of aliases to reduce the chance of collisions when | ||
* working with subqueries. | ||
*/ | ||
class Alias | ||
{ | ||
/** | ||
* Chain of aliases | ||
* | ||
* @var string|\string[] | ||
*/ | ||
protected $stack = []; | ||
|
||
/** | ||
* Global counter to allow generation of | ||
* unique aliases. | ||
* | ||
* @var int | ||
*/ | ||
protected static $counter=0; | ||
|
||
/** | ||
* Constructs a new alias instance. | ||
* If passed parameter is an array, it completely replaces | ||
* the internal stack. | ||
* | ||
* @param string|string[] $alias | ||
*/ | ||
public function __construct($alias = 'a') | ||
{ | ||
if (is_array($alias)) { | ||
$this->stack = $alias; | ||
return; | ||
} | ||
|
||
$this->stack[] = $alias; | ||
} | ||
|
||
/** | ||
* Adds another level. | ||
* The current alias will be copied | ||
* with the specified alias added on | ||
* | ||
* @param string $next alias to add to the stack | ||
* @return Alias | ||
*/ | ||
public function add($next) | ||
{ | ||
return new self(array_merge($this->stack, (array)$next)); | ||
} | ||
|
||
/** | ||
* Adds another level of alias. | ||
* | ||
* This method combines the prefix with a unique | ||
* counter to ensure that the alias is unique. | ||
* | ||
* Use this method with care as it will tend to create | ||
* misses in the doctrine query cache. | ||
* | ||
* @param string $prefix Prefix to use with alias | ||
* @return Alias | ||
*/ | ||
public function addUnique($prefix) | ||
{ | ||
return $this->add($prefix . (self::$counter++)); | ||
} | ||
|
||
/** | ||
* Writes out a field name | ||
* | ||
* @param string $name | ||
* @return string | ||
*/ | ||
public function field($name) | ||
{ | ||
return $this->alias() . '.' . $name; | ||
} | ||
|
||
/** | ||
* Writes out a parameter name | ||
* | ||
* @param string $name | ||
* @return string | ||
*/ | ||
public function param($name) | ||
{ | ||
return $this->alias() . '_' . $name; | ||
} | ||
|
||
/** | ||
* Returns the string version of the alias. | ||
* | ||
* @return string | ||
*/ | ||
public function alias() | ||
{ | ||
return join('_', $this->stack); | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return $this->alias(); | ||
} | ||
|
||
|
||
/** | ||
* Conditionally creates the alias. | ||
* If the passed object is already an alias, simply returns it. | ||
* | ||
* @param string|Alias $alias | ||
* @return Alias | ||
*/ | ||
public static function create($alias) | ||
{ | ||
if ($alias instanceof self) { | ||
return $alias; | ||
} | ||
|
||
return new self($alias); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace DoctrineExtensions\Test\ORM; | ||
|
||
use DoctrineExtensions\ORM\Alias; | ||
|
||
class AliasTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testField() | ||
{ | ||
$alias = new Alias(); | ||
|
||
$this->assertEquals('a.foo', $alias->field('foo')); | ||
} | ||
|
||
public function testParam() | ||
{ | ||
$alias = new Alias(); | ||
$this->assertEquals('a_foo', $alias->param('foo')); | ||
} | ||
|
||
public function testAlias() | ||
{ | ||
$alias = new Alias(['foo', 'bar']); | ||
$this->assertEquals('foo_bar', $alias->alias()); | ||
} | ||
|
||
public function testAdd() | ||
{ | ||
$alias = new Alias('a'); | ||
$alias2 = $alias->add('b'); | ||
$alias3 = $alias2->add('c'); | ||
|
||
$this->assertEquals('a', $alias->alias()); | ||
$this->assertEquals('a_b', $alias2->alias()); | ||
$this->assertEquals('a_b_c', $alias3->alias()); | ||
} | ||
|
||
public function testAddUnique() | ||
{ | ||
$alias = new Alias('a'); | ||
$alias2 = $alias->addUnique('b'); | ||
$alias3 = $alias2->addUnique('b'); | ||
$alias4 = $alias3->addUnique('b'); | ||
|
||
$this->assertEquals('a_b0_b1_b2', $alias4->alias()); | ||
$this->assertEquals('a_b3', $alias->addUnique('b')->alias()); | ||
} | ||
|
||
public function testToString() | ||
{ | ||
$alias = new Alias(['foo', 'bar']); | ||
$this->assertEquals('foo_bar', $alias . ''); | ||
} | ||
} |