| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\User\TempUser; |
| 4 |
|
| 5 |
use ArrayUtils; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class FilteredRadixSerialMapping implements SerialMapping { |
| 18 |
|
| 19 |
private $radix; |
| 20 |
|
| 21 |
|
| 22 |
private $badIndexes; |
| 23 |
|
| 24 |
|
| 25 |
private $uppercase; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public function __construct( $config ) { |
| 31 |
$this->radix = $config['radix'] ?? 10; |
| 32 |
$this->badIndexes = $config['badIndexes'] ?? []; |
| 33 |
$this->uppercase = $config['uppercase'] ?? false; |
| 34 |
} |
| 35 |
|
| 36 |
public function getSerialIdForIndex( int $index ): string { |
| 37 |
$index = $this->adjustID( $index ); |
| 38 |
return \Wikimedia\base_convert( (string)$index, 10, $this->radix, 1, !$this->uppercase ); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
private function adjustID( int $id ): int { |
| 49 |
$pos = ArrayUtils::findLowerBound( |
| 50 |
function ( $i ) { |
| 51 |
return $this->badIndexes[$i]; |
| 52 |
}, |
| 53 |
count( $this->badIndexes ), |
| 54 |
static function ( $a, $b ) { |
| 55 |
return $a <=> $b; |
| 56 |
}, |
| 57 |
$id |
| 58 |
); |
| 59 |
return $pos === false ? $id : $id + $pos + 1; |
| 60 |
} |
| 61 |
} |