$columnColors by BoardColumn value * @param array $columnSorts by BoardColumn value * @param list $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 $colors * @param array $sorts * @param array $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 */ public function colorValues(): array { return array_map(fn (HexColor $color): string => $color->value, $this->columnColors); } /** * @return array */ public function sortValues(): array { return array_map(fn (ColumnSort $sort): string => $sort->value, $this->columnSorts); } }