Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Added alias support
Browse files Browse the repository at this point in the history
  • Loading branch information
powturns committed Feb 29, 2016
1 parent 05d4923 commit ee9f2a1
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Collection of extensions to the doctrine2 ORM
- UTCTimestampableEntity - [Timestampable](https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/timestampable.md)
behaviour using UTCDateTime
- EntityManagerAwareTrait - Provides getters/setters for object to hold and instance of an EntityManager.
- Alias
- A smarter alias to use with the doctrine query builder. Allows building / chaining of multiple aliases for subquries.


## Skipper
Expand Down
128 changes: 128 additions & 0 deletions src/ORM/Alias.php
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);
}
}
55 changes: 55 additions & 0 deletions tests/DoctrineExtensions/Test/ORM/AliasTest.php
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 . '');
}
}

0 comments on commit ee9f2a1

Please sign in to comment.