mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Echo
synced 2024-12-21 12:12:57 +00:00
4b4954ac40
https://bugs.php.net/bug.php?id=50688 was closed as fixed in 2017 when PHP 7.0 was only supported upstream. We already require PHP 7.2. Change-Id: Ie9801e38915da634e31c91ebdcb61226e0ae5712
43 lines
989 B
PHP
43 lines
989 B
PHP
<?php
|
|
|
|
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;
|
|
}
|
|
|
|
}
|