23 private const string FORMALITY_INDEXES_PATH = __DIR__ .
'/../data/formal-indexes.json';
33 public function __construct(
34 private readonly
string $langCode,
35 private readonly LoggerInterface $logger,
49 public function getFormalityIndex():
int {
50 self::$indexesCache ??= $this->loadIndexes();
57 if ( str_contains( $this->langCode,
'-' ) ) {
58 $parts = explode(
'-', $this->langCode );
59 $suffix = array_pop( $parts );
60 if ( $suffix ===
'formal' ) {
62 } elseif ( $suffix ===
'informal' ) {
78 private function loadIndexes(): array {
79 $rawIndexes = $this->jsonLoader->load( self::FORMALITY_INDEXES_PATH,
'formality indexes' );
82 foreach ( $rawIndexes as $key => $value ) {
83 if ( is_string( $key ) && is_int( $value ) ) {
84 $indexes[$key] = $value;
87 'Invalid formality index entry. Expected string key and int value. ' .
88 'Got key type {keyType}, value type {valueType}.',
90 'keyType' => gettype( $key ),
91 'valueType' => gettype( $value ),