| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications; |
| 4 |
|
| 5 |
class Bundler { |
| 6 |
|
| 7 |
private function sort( &$array ) { |
| 8 |
usort( $array, static function ( Bundleable $a, Bundleable $b ) { |
| 9 |
return strcmp( $b->getSortingKey(), $a->getSortingKey() ); |
| 10 |
} ); |
| 11 |
} |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public function bundle( array $bundleables ) { |
| 20 |
$groups = []; |
| 21 |
$bundled = []; |
| 22 |
|
| 23 |
|
| 24 |
foreach ( $bundleables as $element ) { |
| 25 |
if ( $element->canBeBundled() && $element->getBundlingKey() ) { |
| 26 |
$groups[ $element->getBundlingKey() ][] = $element; |
| 27 |
} else { |
| 28 |
$bundled[] = $element; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
foreach ( $groups as $bundlingKey => $group ) { |
| 33 |
$this->sort( $group ); |
| 34 |
|
| 35 |
$base = array_shift( $group ); |
| 36 |
$base->setBundledElements( $group ); |
| 37 |
$bundled[] = $base; |
| 38 |
} |
| 39 |
|
| 40 |
$this->sort( $bundled ); |
| 41 |
return $bundled; |
| 42 |
} |
| 43 |
|
| 44 |
} |