Skip to content

Commit

Permalink
feat: special handling for '*' in F::COUNT()
Browse files Browse the repository at this point in the history
  • Loading branch information
mecha committed Dec 15, 2023
1 parent 4441179 commit 0d94c9e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion inc/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace RebelCode\Atlas;

// For use in select queries, for readability purposes.
const ALL = ['*'];
const ALL = '*';

// Aliases for order constants
const ASC = Order::ASC;
Expand Down
4 changes: 3 additions & 1 deletion src/Expression/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace RebelCode\Atlas\Expression;

use InvalidArgumentException;
use Iterator;
use Traversable;

/** @psalm-immutable */
Expand All @@ -14,6 +13,7 @@ class Term extends BaseExpr
public const BOOLEAN = 2;
public const LIST = 3;
public const NULL = 4;
public const SPECIAL = 5;

/** @var mixed */
protected $value;
Expand Down Expand Up @@ -67,6 +67,8 @@ protected function toBaseString(): string
}, $elements);

return '(' . implode(', ', $elementsStr) . ')';
case self::SPECIAL:
return $this->value;
default:
throw new InvalidArgumentException("Term has unknown type: \"$this->type\"");
}
Expand Down
8 changes: 6 additions & 2 deletions src/F.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,12 @@ abstract class F
* @param list<mixed|ExprInterface> $arguments The call arguments.
* @return FnExpr The created function expression.
*/
public static function __callStatic(string $operator, array $arguments): FnExpr
public static function __callStatic(string $fnName, array $args): FnExpr
{
return new FnExpr($operator, array_map([Term::class, 'create'], $arguments));
if ($fnName === 'COUNT' && (count($args) === 0 || $args[0] === '*')) {
$args = [new Term(Term::SPECIAL, '*')];
}

return new FnExpr($fnName, array_map([Term::class, 'create'], $args));
}
}
18 changes: 18 additions & 0 deletions tests/FTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,22 @@ public function testMagicStaticCall()
$this->assertEquals('foo', $this->expose($expr)->name);
$this->assertSame([$term1, $term2], $this->expose($expr)->args);
}

public function testCountNoArgs()
{
$count = F::COUNT();
$star = new Term(Term::SPECIAL, '*');

$this->assertEquals('COUNT', $this->expose($count)->name);
$this->assertEquals([$star], $this->expose($count)->args);
}

public function testCountWithStar()
{
$count = F::COUNT('*');
$star = new Term(Term::SPECIAL, '*');

$this->assertEquals('COUNT', $this->expose($count)->name);
$this->assertEquals([$star], $this->expose($count)->args);
}
}

0 comments on commit 0d94c9e

Please sign in to comment.