eee
This commit is contained in:
+65
-46
@@ -18,61 +18,80 @@ use JetBrains\PhpStorm\Pure;
|
||||
class When
|
||||
{
|
||||
|
||||
public ActiveQuery|ISqlBuilder $query;
|
||||
public ActiveQuery|ISqlBuilder $query;
|
||||
|
||||
|
||||
private array $_condition = [];
|
||||
|
||||
private string $else = '';
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $_condition = [] {
|
||||
&get {
|
||||
return $this->_condition;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* CaseWhen constructor.
|
||||
* @param string $column
|
||||
* @param ActiveQuery|ISqlBuilder $activeQuery
|
||||
*/
|
||||
public function __construct(public string $column, public ActiveQuery|ISqlBuilder $activeQuery)
|
||||
{
|
||||
$this->_condition[] = 'CASE ' . $column;
|
||||
}
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private string $else = '' {
|
||||
get {
|
||||
return $this->else;
|
||||
}
|
||||
set(string $value) {
|
||||
$this->else = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string|int $condition
|
||||
* @param string $then
|
||||
* @return $this
|
||||
* @throws
|
||||
*/
|
||||
public function when(string|int $condition, string $then): static
|
||||
{
|
||||
$this->_condition[] = sprintf('WHEN %s THEN %s', $condition, $then);
|
||||
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* CaseWhen constructor.
|
||||
* @param string $column
|
||||
* @param ActiveQuery|ISqlBuilder $activeQuery
|
||||
*/
|
||||
public function __construct(public string $column, public ActiveQuery|ISqlBuilder $activeQuery)
|
||||
{
|
||||
$this->_condition[] = 'CASE ' . $column;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $alias
|
||||
*/
|
||||
public function else(string $alias): void
|
||||
{
|
||||
$this->else = $alias;
|
||||
}
|
||||
/**
|
||||
* @param string|int $condition
|
||||
* @param string $then
|
||||
* @return $this
|
||||
* @throws
|
||||
*/
|
||||
public function when(string|int $condition, string $then): static
|
||||
{
|
||||
$this->_condition[] = sprintf('WHEN %s THEN %s', $condition, $then);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] public function end(): string
|
||||
{
|
||||
if (empty($this->_condition)) {
|
||||
return '';
|
||||
}
|
||||
$prefix = implode(' ', $this->_condition);
|
||||
if (!empty($this->else)) {
|
||||
$prefix .= ' ELSE ' . $this->else;
|
||||
}
|
||||
return '(' . $prefix . ' END)';
|
||||
}
|
||||
/**
|
||||
* @param string $alias
|
||||
*/
|
||||
public function else(string $alias): void
|
||||
{
|
||||
$this->else = $alias;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
#[Pure]
|
||||
public function end(): string
|
||||
{
|
||||
if (empty($this->_condition)) {
|
||||
return '';
|
||||
}
|
||||
$prefix = implode(' ', $this->_condition);
|
||||
if (!empty($this->else)) {
|
||||
$prefix .= ' ELSE ' . $this->else;
|
||||
}
|
||||
return '(' . $prefix . ' END)';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user