| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Minerva\Menu; |
| 22 |
|
| 23 |
use DomainException; |
| 24 |
use MediaWiki\Minerva\Menu\Entries\IMenuEntry; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
final class Group { |
| 30 |
|
| 31 |
private array $entries = []; |
| 32 |
private string $id; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public function __construct( $id ) { |
| 38 |
$this->id = $id; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function getId(): string { |
| 47 |
return $this->id; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function hasEntries(): bool { |
| 56 |
return count( $this->entries ) > 0; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function getEntries(): array { |
| 65 |
$entryPresenter = static function ( IMenuEntry $entry ) { |
| 66 |
$result = [ |
| 67 |
'name' => $entry->getName(), |
| 68 |
'components' => $entry->getComponents(), |
| 69 |
]; |
| 70 |
$classes = $entry->getCSSClasses(); |
| 71 |
if ( $classes ) { |
| 72 |
$result[ 'class' ] = implode( ' ', $classes ); |
| 73 |
} |
| 74 |
|
| 75 |
return $result; |
| 76 |
}; |
| 77 |
|
| 78 |
return array_map( $entryPresenter, $this->entries ); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
private function throwIfNotUnique( string $name ): void { |
| 88 |
try { |
| 89 |
$this->search( $name ); |
| 90 |
} catch ( DomainException $exception ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
throw new DomainException( "The \"{$name}\" entry already exists." ); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function prependEntry( IMenuEntry $entry ): void { |
| 102 |
$this->throwIfNotUnique( $entry->getName() ); |
| 103 |
array_unshift( $this->entries, $entry ); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function insertEntry( IMenuEntry $entry ): void { |
| 112 |
$this->throwIfNotUnique( $entry->getName() ); |
| 113 |
$this->entries[] = $entry; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
private function search( string $name ): int { |
| 124 |
$count = count( $this->entries ); |
| 125 |
|
| 126 |
for ( $i = 0; $i < $count; ++$i ) { |
| 127 |
if ( $this->entries[$i]->getName() === $name ) { |
| 128 |
return $i; |
| 129 |
} |
| 130 |
} |
| 131 |
throw new DomainException( "The \"{$name}\" entry doesn't exist." ); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function getEntryByName( string $targetName ): IMenuEntry { |
| 140 |
$index = $this->search( $targetName ); |
| 141 |
return $this->entries[$index]; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public function serialize(): array { |
| 149 |
return [ |
| 150 |
'entries' => $this->getEntries(), |
| 151 |
'id' => $this->getId(), |
| 152 |
]; |
| 153 |
} |
| 154 |
} |