= 4096 ) { throw new UnexpectedValueException( "Alphabet must be < 4096 items" ); } $digitTransformLang = $digitTransformLang instanceof Language ? $digitTransformLang : $languageFactory->getLanguage( $digitTransformLang ); $this->puaSubset = []; $this->alphabet = []; $len = count( $alphabet ); for ( $i = 0; $i < $len; $i++ ) { // We allow alphabet to contain array members if multiple characters should be sorted as equivalent. for ( $j = 0; $j < count( (array)( $alphabet[$i] ) ); $j++ ) { $this->puaSubset[] = "\xF3\xB3" . chr( (int)floor( $i / 64 ) + 128 ) . chr( ( $i % 64 ) + 128 ); // For digraphs, we uppercase it all during sorting but not when displaying first letter. $this->alphabet[] = $digitTransformLang->uc( ( (array)( $alphabet[$i] ) )[$j] ); // Note: first letters is always first of group $this->firstLetters[] = ( (array)( $alphabet[$i] ) )[0]; } } // Sort these arrays so that any trigraphs, digraphs etc. are first // (and they get replaced first in convertToPua()). $lengths = array_map( 'mb_strlen', $this->alphabet ); array_multisort( $lengths, SORT_DESC, $this->firstLetters, $this->alphabet, $this->puaSubset ); parent::__construct( $languageFactory, $digitTransformLang ); } private function convertToPua( string $string ): string { return str_replace( $this->alphabet, $this->puaSubset, $string ); } public function getSortKey( $string ) { return $this->convertToPua( parent::getSortKey( $string ) ); } public function getFirstLetter( $string ) { $sortkey = $this->getSortKey( $string ); // In case a title begins with a character from our alphabet, return the corresponding // first-letter. (This also happens if the title has a corresponding PUA code in it, to avoid // inconsistent behaviour. This class mostly assumes that people will not use PUA codes.) $index = array_search( substr( $sortkey, 0, 4 ), $this->puaSubset ); if ( $index !== false ) { return $this->firstLetters[ $index ]; } // String begins with a character outside of our alphabet, fall back return parent::getFirstLetter( $string ); } }