nur-sery/nur_src/b/text/WordManager.php

56 lines
1.5 KiB
PHP
Raw Normal View History

2023-12-03 22:10:18 +04:00
<?php
namespace nur\b\text;
use nur\b\coll\IterableArray;
use nur\b\ValueException;
/**
* Class WordManager: un gestionnaire de mots
*
* Ce gestionnaire permet de stocker la configuration pour certains mots connus
*/
class WordManager extends IterableArray {
protected function WORDS(): array {
return static::WORDS;
} const WORDS = [
"service" => "le service#s",
"direction" => "la direction#s",
];
protected function ADJECTIVES(): array {
return static::ADJECTIVES;
} const ADJECTIVES = [
"ne" => "né#e"
];
function __construct() {
$words = [];
$i = 0;
foreach ($this->WORDS() as $key => $word) {
$word = new Word($word);
if ($key === $i) $words[] = $word;
else $words[$key] = $word;
$i++;
}
$i = 0;
foreach ($this->ADJECTIVES() as $key => $adjective) {
$word = new Word($adjective, true);
if ($key === $i) $words[] = $word;
else $words[$key] = $word;
$i++;
}
parent::__construct($words);
}
# ArrayAccess
function has(string $key): bool { return $this->_has($key); }
function get(string $key): Word {
return ValueException::check_nn($this->_get($key), "$key: unknown word");
}
function set(string $key, Word $value): self { return $this->_set($key, $value); }
function del(string $key): self { return $this->_del($key); }
# Iterator
function key(): string { return $this->_key(); }
function current(): Word { return $this->_current(); }
}