Skip to content
Draft
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
15 changes: 7 additions & 8 deletions src/UI/DataGrid/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ public function __construct(public string $name, public string $label, private D
}

/**
* @phpstan-param DataGrid::Order*|null $defaultOrder
* @return $this
*/
public function enableSort(string|null $defaultOrder = null): self
public function enableSort(ColumnOrder|null $defaultOrder = null): self
{
$this->sort = true;
if ($defaultOrder !== null) {
Expand All @@ -31,27 +30,27 @@ public function canSort(): bool
return $this->sort;
}

public function getNewState(): string
public function getNewState(): ColumnOrder
{
if ($this->isAsc()) {
return DataGrid::OrderDesc;
return ColumnOrder::Desc;
}

if ($this->isDesc()) {
return '';
return ColumnOrder::Undefined;
}

return DataGrid::OrderAsc;
return ColumnOrder::Asc;
}

public function isAsc(): bool
{
return $this->grid->orderColumn === $this->name && $this->grid->orderType === DataGrid::OrderAsc;
return $this->grid->orderColumn === $this->name && $this->grid->orderType === ColumnOrder::Asc;
}

public function isDesc(): bool
{
return $this->grid->orderColumn === $this->name && $this->grid->orderType === DataGrid::OrderDesc;
return $this->grid->orderColumn === $this->name && $this->grid->orderType === ColumnOrder::Desc;
}

}
14 changes: 14 additions & 0 deletions src/UI/DataGrid/ColumnOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types = 1);

namespace OriCMF\UI\DataGrid;

enum ColumnOrder: string
{

case Asc = 'asc';

case Desc = 'desc';

case Undefined = 'undefined';

}
6 changes: 1 addition & 5 deletions src/UI/DataGrid/DataGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
final class DataGrid extends BaseControl
{

public const OrderAsc = 'asc',
OrderDesc = 'desc';

public const TemplatePath = __DIR__ . '/DataGrid.latte';

/** @var array<string, mixed> */
Expand All @@ -39,9 +36,8 @@ final class DataGrid extends BaseControl
#[Persistent]
public string|null $orderColumn = null;

/** @var self::Order* */
#[Persistent]
public string $orderType = self::OrderAsc;
public ColumnOrder $orderType = ColumnOrder::Undefined;

/** @var int<1, max> */
#[Persistent]
Expand Down
23 changes: 8 additions & 15 deletions src/UI/DataGrid/OrderParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,31 @@
final class OrderParameter
{

/** @phpstan-var DataGrid::Order* */
private string $direction;

/**
* @phpstan-param DataGrid::Order* $direction
*/
public function __construct(private readonly string $column, string $direction)
public function __construct(
private readonly string $column,
private readonly ColumnOrder $order,
)
{
$this->direction = $direction;
}

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

/**
* @phpstan-return DataGrid::Order*
*/
public function getDirection(): string
public function getOrder(): ColumnOrder
{
return $this->direction;
return $this->order;
}

public function isAsc(): bool
{
return $this->direction === DataGrid::OrderAsc;
return $this->order === ColumnOrder::Asc;
}

public function isDesc(): bool
{
return $this->direction === DataGrid::OrderDesc;
return $this->order === ColumnOrder::Desc;
}

}