mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Echo
synced 2024-11-16 20:33:29 +00:00
0efef4faf3
I found candidates for this by looking for parameters names that end with a plural "s". Change-Id: I61c706eb4dfbdadceb0129afd724e6ce1eb4f4a8
49 lines
1.2 KiB
PHP
49 lines
1.2 KiB
PHP
<?php
|
|
|
|
class Bundler {
|
|
|
|
private function sort( &$array ) {
|
|
// We have to ignore the error here (use @usort)
|
|
// otherwise this code fails when executed by unit tests
|
|
// See: https://bugs.php.net/bug.php?id=50688
|
|
|
|
// @codingStandardsIgnoreStart
|
|
@usort( $array, function( Bundleable $a, Bundleable $b ) {
|
|
return strcmp( $b->getSortingKey(), $a->getSortingKey() );
|
|
} );
|
|
// @codingStandardsIgnoreEnd
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
}
|