Groups.php
48 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\IAWP\Tables\Groups; |
| 4 | |
| 5 | class Groups |
| 6 | { |
| 7 | private $groups = []; |
| 8 | public function add(Group $group) : void |
| 9 | { |
| 10 | $this->groups[] = $group; |
| 11 | } |
| 12 | public function find_by_id(?string $id = null) : Group |
| 13 | { |
| 14 | if (\is_null($id)) { |
| 15 | return $this->default_group(); |
| 16 | } |
| 17 | $array = \array_filter($this->groups(), function ($group) use($id) { |
| 18 | return $group->has_id($id); |
| 19 | }); |
| 20 | $match = \reset($array); |
| 21 | return $match === \false ? $this->default_group() : $match; |
| 22 | } |
| 23 | /** |
| 24 | * Return an array of groups representing buttons one can select |
| 25 | * |
| 26 | * @return Group[] |
| 27 | */ |
| 28 | public function buttons() : array |
| 29 | { |
| 30 | if (\count($this->groups()) === 1) { |
| 31 | return []; |
| 32 | } else { |
| 33 | return $this->groups(); |
| 34 | } |
| 35 | } |
| 36 | private function default_group() : Group |
| 37 | { |
| 38 | return $this->groups()[0]; |
| 39 | } |
| 40 | /** |
| 41 | * @return Group[] |
| 42 | */ |
| 43 | private function groups() : array |
| 44 | { |
| 45 | return $this->groups; |
| 46 | } |
| 47 | } |
| 48 |