2017-07-12 15:12:40 +00:00
|
|
|
<?php
|
2018-04-15 23:21:12 +00:00
|
|
|
/**
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2019-04-08 17:08:57 +00:00
|
|
|
namespace MediaWiki\Minerva\Menu;
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
use DomainException;
|
2019-07-01 18:06:01 +00:00
|
|
|
use MediaWiki\Minerva\Menu\Entries\IMenuEntry;
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Model for a menu that can be presented in a skin.
|
|
|
|
*/
|
2019-07-12 03:06:13 +00:00
|
|
|
final class Group {
|
2024-03-28 19:15:41 +00:00
|
|
|
/** @var IMenuEntry[] */
|
|
|
|
private array $entries = [];
|
|
|
|
private string $id;
|
2019-10-23 18:30:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $id of the menu defaults to null (optional)
|
|
|
|
*/
|
|
|
|
public function __construct( $id ) {
|
|
|
|
$this->id = $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the identifier for the group
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function getId(): string {
|
2019-10-23 18:30:55 +00:00
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:08:57 +00:00
|
|
|
/**
|
|
|
|
* Return entries count
|
|
|
|
*
|
2019-05-21 17:01:00 +00:00
|
|
|
* @return bool
|
2019-04-08 17:08:57 +00:00
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function hasEntries(): bool {
|
2019-04-08 17:08:57 +00:00
|
|
|
return count( $this->entries ) > 0;
|
|
|
|
}
|
|
|
|
|
2017-07-12 15:12:40 +00:00
|
|
|
/**
|
|
|
|
* Get all entries represented as plain old PHP arrays.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function getEntries(): array {
|
2021-05-03 14:12:42 +00:00
|
|
|
$entryPresenter = static function ( IMenuEntry $entry ) {
|
2017-07-12 15:12:40 +00:00
|
|
|
$result = [
|
|
|
|
'name' => $entry->getName(),
|
|
|
|
'components' => $entry->getComponents(),
|
|
|
|
];
|
2019-05-14 10:31:54 +00:00
|
|
|
$classes = $entry->getCSSClasses();
|
|
|
|
if ( $classes ) {
|
|
|
|
$result[ 'class' ] = implode( ' ', $classes );
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
};
|
|
|
|
|
|
|
|
return array_map( $entryPresenter, $this->entries );
|
|
|
|
}
|
|
|
|
|
2019-05-14 09:38:39 +00:00
|
|
|
/**
|
|
|
|
* Helper method to verify that the $name of entry is unique (do not exists
|
|
|
|
* in current Group )
|
|
|
|
* @param string $name
|
|
|
|
* @throws DomainException When the entry already exists
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
private function throwIfNotUnique( string $name ): void {
|
2019-05-29 17:00:22 +00:00
|
|
|
try {
|
|
|
|
$this->search( $name );
|
|
|
|
} catch ( DomainException $exception ) {
|
|
|
|
return;
|
2019-05-14 09:38:39 +00:00
|
|
|
}
|
2022-11-04 20:45:21 +00:00
|
|
|
throw new DomainException( "The \"{$name}\" entry already exists." );
|
2019-05-14 09:38:39 +00:00
|
|
|
}
|
|
|
|
|
2023-03-24 21:56:18 +00:00
|
|
|
/**
|
|
|
|
* Prepend new menu entry
|
|
|
|
* @param IMenuEntry $entry
|
|
|
|
* @throws DomainException When the entry already exists
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function prependEntry( IMenuEntry $entry ): void {
|
2023-03-24 21:56:18 +00:00
|
|
|
$this->throwIfNotUnique( $entry->getName() );
|
|
|
|
array_unshift( $this->entries, $entry );
|
|
|
|
}
|
|
|
|
|
2019-05-14 10:31:54 +00:00
|
|
|
/**
|
|
|
|
* Insert new menu entry
|
|
|
|
* @param IMenuEntry $entry
|
|
|
|
* @throws DomainException When the entry already exists
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function insertEntry( IMenuEntry $entry ): void {
|
2019-05-14 10:31:54 +00:00
|
|
|
$this->throwIfNotUnique( $entry->getName() );
|
|
|
|
$this->entries[] = $entry;
|
|
|
|
}
|
|
|
|
|
2017-07-12 15:12:40 +00:00
|
|
|
/**
|
|
|
|
* Searches for a menu entry by name.
|
|
|
|
*
|
|
|
|
* @param string $name
|
2020-01-26 19:26:31 +00:00
|
|
|
* @return int If the menu entry exists, then the 0-based index of the entry; otherwise, -1
|
2019-05-29 17:00:22 +00:00
|
|
|
* @throws DomainException
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
private function search( string $name ): int {
|
2017-07-12 15:12:40 +00:00
|
|
|
$count = count( $this->entries );
|
|
|
|
|
|
|
|
for ( $i = 0; $i < $count; ++$i ) {
|
|
|
|
if ( $this->entries[$i]->getName() === $name ) {
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 17:00:22 +00:00
|
|
|
throw new DomainException( "The \"{$name}\" entry doesn't exist." );
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 17:00:22 +00:00
|
|
|
/**
|
|
|
|
* @param string $targetName
|
|
|
|
* @return IMenuEntry
|
|
|
|
* @throws DomainException
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function getEntryByName( string $targetName ): IMenuEntry {
|
2019-05-29 17:00:22 +00:00
|
|
|
$index = $this->search( $targetName );
|
|
|
|
return $this->entries[$index];
|
|
|
|
}
|
2019-10-23 18:30:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize the group for use in a template
|
|
|
|
* @return array{entries:array,id:string}
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function serialize(): array {
|
2019-10-23 18:30:55 +00:00
|
|
|
return [
|
|
|
|
'entries' => $this->getEntries(),
|
|
|
|
'id' => $this->getId(),
|
|
|
|
];
|
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|