4use Shellbox\RPC\RpcClient;
12 private RpcClient $rpcClient;
13 private string $locale;
17 [
'service' =>
'icu-collation' ] );
18 $this->locale = $locale;
32 private static function encode( $strings ) {
34 foreach ( $strings as $s ) {
35 $ret .= sprintf(
"%08x", strlen( $s ) ) . $s;
46 private static function decode( $blob ) {
49 while ( $p < strlen( $blob ) ) {
50 $len = intval( substr( $blob, $p, 8 ), 16 );
52 $ret[] = substr( $blob, $p, $len );
60 if ( !count( $strings ) ) {
63 $blob = $this->rpcClient->call(
65 [ self::class,
'doGetSortKeys' ],
68 self::encode( array_values( $strings ) )
71 'classes' => [ parent::class, self::class ],
76 array_keys( $strings ),
84 throw new RuntimeException( __METHOD__ .
': not implemented' );
95 $mainCollator = Collator::create( $locale );
96 if ( !$mainCollator ) {
97 throw new RuntimeException(
"Invalid ICU locale specified for collation: $locale" );
101 if ( str_ends_with( $locale,
'-u-kn' ) ) {
102 $mainCollator->setAttribute( Collator::NUMERIC_COLLATION, Collator::ON );
105 foreach ( self::decode( $blob ) as $string ) {
106 $ret[] = $mainCollator->getSortKey( $string );
108 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.string[]
getFirstLetter( $string)
Given a string, return the logical "first letter" to be used for grouping on category pages and so on...