Skip to content

Commit

Permalink
refactor: new Group class with optional sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
mecha committed Jul 17, 2024
1 parent fd4d5a7 commit c2b93b8
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 7 deletions.
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

0 comments on commit c2b93b8

Please sign in to comment.