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

New Group class with optional sorting #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
68 changes: 67 additions & 1 deletion src/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,70 @@

namespace RebelCode\Atlas;

class Group extends Order {}
use RebelCode\Atlas\Expression\ColumnTerm;

class Group
{
public const ASC = 'ASC';
public const DESC = 'DESC';

protected ColumnTerm $column;
protected ?string $sort;

/**
* Constructor.
*
* @param string|ColumnTerm $column The column to sort by.
* @param string|null $sort The sort order.
*/
public function __construct($column, ?string $sort = null)
{
$this->column = $column instanceof ColumnTerm ? $column : new ColumnTerm(null, $column);
$this->sort = $sort;
}

public function getColumn(): ColumnTerm
{
return $this->column;
}

public function getSort(): ?string
{
return $this->sort;
}

public function asc(): Group
{
return ($this->sort !== self::ASC)
? new self($this->column, self::ASC)
: $this;
}

public function desc(): Group
{
return ($this->sort !== self::DESC)
? new self($this->column, self::DESC)
: $this;
}

public function noSort(): Group
{
return ($this->sort !== null)
? new self($this->column, null)
: $this;
}

public function dir(?string $order, string $default = self::ASC): Group
{
if ($order !== null) {
$order = strtoupper($order);
$order = ($order === self::ASC || $order === self::DESC) ? $order : $default;
}
return new self($this->column, $order);
}

public static function by(string $column): Group
{
return new self($column);
}
}
9 changes: 7 additions & 2 deletions src/Query/Traits/HasGroupByTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace RebelCode\Atlas\Query\Traits;

use InvalidArgumentException;
use RebelCode\Atlas\Group;

trait HasGroupByTrait
Expand Down Expand Up @@ -36,7 +35,13 @@ protected function compileGroupBy(): string

$groupParts = [];
foreach ($this->groups as $group) {
$groupParts[] = $group->getColumn() . ' ' . $group->getSort();
$col = $group->getColumn();
$sort = $group->getSort();
if ($sort === null) {
$groupParts[] = $col;
} else {
$groupParts[] = "$col $sort";
}
}

return 'GROUP BY ' . implode(', ', $groupParts);
Expand Down
2 changes: 1 addition & 1 deletion tests/Query/SelectQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public function testCompileSelectGroupBy()
new Group('baz', Group::DESC),
]);

$expected = 'SELECT * FROM table GROUP BY `foo` ASC, `bar` ASC, `baz` DESC';
$expected = 'SELECT * FROM table GROUP BY `foo`, `bar` ASC, `baz` DESC';
$actual = $query->toSql();

$this->assertEquals($expected, $actual);
Expand Down
7 changes: 4 additions & 3 deletions tests/Query/Traits/HasGroupByTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ public function testSetGroups()
public function testCompile()
{
$mock = $this->getMockForTrait(HasGroupByTrait::class)->groupBy([
Group::by('foo')->asc(),
Group::by('bar')->desc(),
Group::by('foo'),
Group::by('bar')->asc(),
Group::by('baz')->desc(),
]);

$this->assertEquals('GROUP BY `foo` ASC, `bar` DESC', $this->expose($mock)->compileGroupBy());
$this->assertEquals('GROUP BY `foo`, `bar` ASC, `baz` DESC', $this->expose($mock)->compileGroupBy());
}

public function testCompileNoGroups()
Expand Down
Loading