4use Shellbox\RPC\RpcClient;
12 private RpcClient $rpcClient;
13 private string $locale;
17 [
'service' =>
'icu-collation' ] );
18 $this->locale = $locale;
31 private static function encode( $strings ) {
33 foreach ( $strings as $s ) {
34 $ret .= sprintf(
"%08x", strlen( $s ) ) . $s;
45 private static function decode( $blob ) {
48 while ( $p < strlen( $blob ) ) {
49 $len = intval( substr( $blob, $p, 8 ), 16 );
51 $ret[] = substr( $blob, $p, $len );
58 if ( !count( $strings ) ) {
61 $blob = $this->rpcClient->call(
63 self::class .
'::' .
'doGetSortKeys',
66 self::encode( array_values( $strings ) )
69 'classes' => [ parent::class, self::class ],
74 array_keys( $strings ),
81 throw new RuntimeException( __METHOD__ .
': not implemented' );
92 $mainCollator = Collator::create( $locale );
93 if ( !$mainCollator ) {
94 throw new RuntimeException(
"Invalid ICU locale specified for collation: $locale" );
98 if ( str_ends_with( $locale,
'-u-kn' ) ) {
99 $mainCollator->setAttribute( Collator::NUMERIC_COLLATION, Collator::ON );
102 foreach ( self::decode( $blob ) as $string ) {
103 $ret[] = $mainCollator->getSortKey( $string );
105 return self::encode( $ret );
An ICU collation that uses a remote server to compute sort keys.
static doGetSortKeys( $locale, $blob)
The remote entry point.
__construct(ShellboxClientFactory $shellboxClientFactory, string $locale)
getSortKey( $string)
Given a string, convert it to a (hopefully short) key that can be used for efficient sorting.
getSortKeys( $strings)
Get multiple sort keys.
getFirstLetter( $string)
Given a string, return the logical "first letter" to be used for grouping on category pages and so on...