The open cards are kept in the user's board preferences (expanded_cards, at most 500), saved without redrawing the board and restored on return. Co-Authored-By: Claude Opus 5.5 <[email protected]>
175 lines
5.6 KiB
PHP
175 lines
5.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Jobs\Domain\ValueObjects;
|
|
|
|
use App\Modules\Jobs\Domain\Enums\BoardColumn;
|
|
use App\Modules\Jobs\Domain\Enums\BoardDensity;
|
|
use App\Modules\Jobs\Domain\Enums\ColumnSort;
|
|
use App\Modules\Jobs\Domain\Enums\UrgentThreshold;
|
|
use App\Modules\Shared\Domain\ValueObjects\HexColor;
|
|
|
|
/**
|
|
* One user's Kanban board settings: card density, the urgent threshold,
|
|
* their own colour and order for each column, and which cards they keep
|
|
* open (task list shown). A column without a colour of its own uses the
|
|
* design's default colour.
|
|
*/
|
|
final readonly class BoardPreferences
|
|
{
|
|
/** At most this many open cards are remembered, the latest kept. */
|
|
public const int MAX_EXPANDED_CARDS = 500;
|
|
|
|
/**
|
|
* @param array<string, HexColor> $columnColors by BoardColumn value
|
|
* @param array<string, ColumnSort> $columnSorts by BoardColumn value
|
|
* @param list<string> $expandedCards card keys ("job-12", "task-34"), oldest first
|
|
*/
|
|
public function __construct(
|
|
public BoardDensity $density = BoardDensity::Normal,
|
|
public UrgentThreshold $urgentThreshold = UrgentThreshold::Urgent,
|
|
public array $columnColors = [],
|
|
public array $columnSorts = [],
|
|
public array $expandedCards = [],
|
|
) {}
|
|
|
|
public static function defaults(): self
|
|
{
|
|
return new self;
|
|
}
|
|
|
|
public static function isCardKey(string $key): bool
|
|
{
|
|
return preg_match('/^(job|task)-\d+$/', $key) === 1;
|
|
}
|
|
|
|
/**
|
|
* Rebuilds stored settings, dropping unknown columns and invalid values
|
|
* so a bad stored value never breaks the board.
|
|
*
|
|
* @param array<array-key, mixed> $colors
|
|
* @param array<array-key, mixed> $sorts
|
|
* @param array<array-key, mixed> $expanded
|
|
*/
|
|
public static function fromStored(?string $density, ?string $threshold, array $colors, array $sorts, array $expanded = []): self
|
|
{
|
|
$columnColors = [];
|
|
|
|
foreach ($colors as $column => $color) {
|
|
if (BoardColumn::tryFrom((string) $column) !== null && is_string($color) && preg_match('/^#[0-9a-f]{6}$/i', $color) === 1) {
|
|
$columnColors[(string) $column] = new HexColor($color);
|
|
}
|
|
}
|
|
|
|
$columnSorts = [];
|
|
|
|
foreach ($sorts as $column => $sort) {
|
|
$parsed = is_string($sort) ? ColumnSort::tryFrom($sort) : null;
|
|
|
|
if (BoardColumn::tryFrom((string) $column) !== null && $parsed !== null) {
|
|
$columnSorts[(string) $column] = $parsed;
|
|
}
|
|
}
|
|
|
|
$expandedCards = array_values(array_unique(array_filter($expanded, fn ($key): bool => is_string($key) && self::isCardKey($key))));
|
|
|
|
return new self(
|
|
BoardDensity::tryFrom((string) $density) ?? BoardDensity::Normal,
|
|
UrgentThreshold::tryFrom((string) $threshold) ?? UrgentThreshold::Urgent,
|
|
$columnColors,
|
|
$columnSorts,
|
|
array_slice($expandedCards, -self::MAX_EXPANDED_CARDS),
|
|
);
|
|
}
|
|
|
|
public function colorOf(BoardColumn $column): HexColor
|
|
{
|
|
return $this->columnColors[$column->value] ?? new HexColor($column->defaultColor());
|
|
}
|
|
|
|
public function hasOwnColor(BoardColumn $column): bool
|
|
{
|
|
return isset($this->columnColors[$column->value]);
|
|
}
|
|
|
|
public function sortOf(BoardColumn $column): ColumnSort
|
|
{
|
|
return $this->columnSorts[$column->value] ?? ColumnSort::Manual;
|
|
}
|
|
|
|
public function isCardExpanded(string $key): bool
|
|
{
|
|
return in_array($key, $this->expandedCards, true);
|
|
}
|
|
|
|
public function withDensity(BoardDensity $density): self
|
|
{
|
|
return new self($density, $this->urgentThreshold, $this->columnColors, $this->columnSorts, $this->expandedCards);
|
|
}
|
|
|
|
public function withUrgentThreshold(UrgentThreshold $threshold): self
|
|
{
|
|
return new self($this->density, $threshold, $this->columnColors, $this->columnSorts, $this->expandedCards);
|
|
}
|
|
|
|
/**
|
|
* Null returns the column to its default colour.
|
|
*/
|
|
public function withColumnColor(BoardColumn $column, ?HexColor $color): self
|
|
{
|
|
$colors = $this->columnColors;
|
|
|
|
if ($color === null) {
|
|
unset($colors[$column->value]);
|
|
} else {
|
|
$colors[$column->value] = $color;
|
|
}
|
|
|
|
return new self($this->density, $this->urgentThreshold, $colors, $this->columnSorts, $this->expandedCards);
|
|
}
|
|
|
|
public function withColumnSort(BoardColumn $column, ColumnSort $sort): self
|
|
{
|
|
$sorts = $this->columnSorts;
|
|
$sorts[$column->value] = $sort;
|
|
|
|
return new self($this->density, $this->urgentThreshold, $this->columnColors, $sorts, $this->expandedCards);
|
|
}
|
|
|
|
/**
|
|
* Opens or closes a card's task list for this user. A card key that is
|
|
* not one ("job-12" / "task-34") changes nothing.
|
|
*/
|
|
public function withCardExpanded(string $key, bool $expanded): self
|
|
{
|
|
if (! self::isCardKey($key)) {
|
|
return $this;
|
|
}
|
|
|
|
$cards = array_values(array_diff($this->expandedCards, [$key]));
|
|
|
|
if ($expanded) {
|
|
$cards[] = $key;
|
|
}
|
|
|
|
return new self($this->density, $this->urgentThreshold, $this->columnColors, $this->columnSorts, array_slice($cards, -self::MAX_EXPANDED_CARDS));
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
public function colorValues(): array
|
|
{
|
|
return array_map(fn (HexColor $color): string => $color->value, $this->columnColors);
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
public function sortValues(): array
|
|
{
|
|
return array_map(fn (ColumnSort $sort): string => $sort->value, $this->columnSorts);
|
|
}
|
|
}
|