mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Echo
synced 2024-11-11 17:00:10 +00:00
e576cbdca0
Change-Id: If1405788a4adb550e8a7e8c58b0c2c55cf10ea67
45 lines
1 KiB
PHP
45 lines
1 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Extension\Notifications;
|
|
|
|
class Bundler {
|
|
|
|
private function sort( &$array ) {
|
|
usort( $array, static function ( Bundleable $a, Bundleable $b ) {
|
|
return strcmp( $b->getSortingKey(), $a->getSortingKey() );
|
|
} );
|
|
}
|
|
|
|
/**
|
|
* Bundle bundleable elements that can be bundled by their bundling keys
|
|
*
|
|
* @param Bundleable[] $bundleables
|
|
* @return Bundleable[] Grouped notifications sorted by timestamp DESC
|
|
*/
|
|
public function bundle( array $bundleables ) {
|
|
$groups = [];
|
|
$bundled = [];
|
|
|
|
/** @var Bundleable $element */
|
|
foreach ( $bundleables as $element ) {
|
|
if ( $element->canBeBundled() && $element->getBundlingKey() ) {
|
|
$groups[ $element->getBundlingKey() ][] = $element;
|
|
} else {
|
|
$bundled[] = $element;
|
|
}
|
|
}
|
|
|
|
foreach ( $groups as $bundlingKey => $group ) {
|
|
$this->sort( $group );
|
|
/** @var Bundleable $base */
|
|
$base = array_shift( $group );
|
|
$base->setBundledElements( $group );
|
|
$bundled[] = $base;
|
|
}
|
|
|
|
$this->sort( $bundled );
|
|
return $bundled;
|
|
}
|
|
|
|
}
|