= 4096 ) { throw new UnexpectedValueException( "Alphabet must be < 4096 items" ); } $this->firstLetters = $alphabet; // For digraphs, only the first letter is capitalized in input $this->alphabet = array_map( [ $lang, 'uc' ], $alphabet ); $this->puaSubset = []; $len = count( $alphabet ); for ( $i = 0; $i < $len; $i++ ) { $this->puaSubset[] = "\xF3\xB3" . chr( floor( $i / 64 ) + 128 ) . chr( ( $i % 64 ) + 128 ); } // 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( $lang ); } private function convertToPua( $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 ); } }