26 private const FORMALITY_INDEXES_PATH = __DIR__ .
'/../data/formal-indexes.json';
44 public function __construct(
45 private readonly
string $langCode,
46 private readonly LoggerInterface $logger,
60 public function getFormalityIndex():
int {
61 self::$indexesCache ??= $this->loadIndexes();
68 if ( str_contains( $this->langCode,
'-' ) ) {
69 $parts = explode(
'-', $this->langCode );
70 $suffix = array_pop( $parts );
71 if ( $suffix ===
'formal' ) {
73 } elseif ( $suffix ===
'informal' ) {
89 private function loadIndexes(): array {
90 $rawIndexes = $this->jsonLoader->load( self::FORMALITY_INDEXES_PATH,
'formality indexes' );
93 foreach ( $rawIndexes as $key => $value ) {
94 if ( is_string( $key ) && is_int( $value ) ) {
95 $indexes[$key] = $value;
98 'Invalid formality index entry. Expected string key and int value. ' .
99 'Got key type {keyType}, value type {valueType}.',
101 'keyType' => gettype( $key ),
102 'valueType' => gettype( $value ),