<?php
 namespace App\Reports\Generation; class SortingManager { public ?bool $mainCriterionVisible; public ?bool $secCriterionVisible; public ?bool $thirdCriterionVisible; public ?bool $groupByCriterionVisible; public ?string $mainCriterionColumn; public ?string $secCriterionColumn; public ?string $thirdCriterionColumn; public ?string $mainCriterionOrderDirection; public ?string $secCriterionOrderDirection; public ?string $thirdCriterionOrderDirection; public ?string $groupBy; public function check() :void { if ($this->mainCriterionColumn and !in_array($this->mainCriterionOrderDirection, ['ASC', 'DESC'])) { throw new \App\Reports\Generation\Exception('The main criterion order direction is not valid. It must be either `ASC` or `DESC`.'); } if ($this->secCriterionColumn and !in_array($this->secCriterionOrderDirection, ['ASC', 'DESC'])) { throw new \App\Reports\Generation\Exception('The secondary criterion order direction is not valid. It must be either `ASC` or `DESC`.'); } if ($this->thirdCriterionColumn and !in_array($this->thirdCriterionOrderDirection, ['ASC', 'DESC'])) { throw new \App\Reports\Generation\Exception('The third criterion order direction is not valid. It must be either `ASC` or `DESC`.'); } if ($this->secCriterionColumn and !$this->mainCriterionColumn) { throw new \App\Reports\Generation\Exception('You must set the main criterion column before setting the secondary one.'); } if ($this->thirdCriterionColumn and (!$this->mainCriterionColumn or !$this->secCriterionColumn)) { throw new \App\Reports\Generation\Exception('You must set the main and secondary criterion columns before setting the third one.'); } $cleanCriteriaList = array_filter( [ $this->mainCriterionColumn, $this->secCriterionColumn, $this->thirdCriterionColumn ] ); if (count($cleanCriteriaList)!=count(array_unique($cleanCriteriaList))) { throw new \App\Reports\Generation\Exception('You cannot reuse the same column for the main, secondary and third criterion.'); } switch ($this->groupBy) { case null: break; case 'main': if (!$this->mainCriterionColumn) { throw new \App\Reports\Generation\Exception('The main criterion column must be set when grouping by it.'); } break; case 'sec': if (!$this->secCriterionColumn) { throw new \App\Reports\Generation\Exception('The secondary criterion column must be set when grouping by it.'); } break; case 'third': if (!$this->thirdCriterionColumn) { throw new \App\Reports\Generation\Exception('The third criterion column must be set when grouping by it.'); } break; default: throw new \App\Reports\Generation\Exception('The grouping value is not valid. It must be either null, `main`, `sec` or `third`.'); } } }