ajouter le support des articles

This commit is contained in:
Jephté Clain 2025-10-08 09:04:31 +04:00
parent ee058e00cd
commit 75c06e7038

View File

@ -28,17 +28,23 @@ use nulib\ValueException;
*/ */
class Word { class Word {
/** @var bool le mot est-il féminin? */ /** @var bool le mot est-il féminin? */
private $fem; private bool $fem;
/** @var string article "aucun", "aucune" */
private ?string $aucun;
/** @var string article "un", "une" */
private ?string $un;
/** @var string article "le", "la", "l'" */ /** @var string article "le", "la", "l'" */
private $le; private ?string $le;
/** @var string article "ce", "cet", "cette" */ /** @var string article "ce", "cet", "cette" */
private $ce; private ?string $ce;
/** @var string article "de", "d'" */
private ?string $de;
/** @var string article "du", "de la", "de l'" */ /** @var string article "du", "de la", "de l'" */
private $du; private ?string $du;
/** @var string article "au", "à la", "à l'" */ /** @var string article "au", "à la", "à l'" */
private $au; private ?string $au;
/** @var string le mot sans article */ /** @var string le mot sans article */
private $w; private string $w;
function __construct(string $spec, bool $adjective=false) { function __construct(string $spec, bool $adjective=false) {
if (preg_match('/^f([eé]m(inin)?)?\s*:\s*/iu', $spec, $ms)) { if (preg_match('/^f([eé]m(inin)?)?\s*:\s*/iu', $spec, $ms)) {
@ -57,28 +63,40 @@ class Word {
$fem = null; $fem = null;
} }
if (preg_match('/^l\'\s*/i', $spec, $ms) && $fem !== null) { if (preg_match('/^l\'\s*/i', $spec, $ms) && $fem !== null) {
$aucun = $fem? "aucune ": "aucun ";
$un = $fem? "une ": "un ";
$le = "l'"; $le = "l'";
$ce = "cet "; $ce = "cet ";
$de = "d'";
$du = "de l'"; $du = "de l'";
$au = "à l'"; $au = "à l'";
$spec = substr($spec, strlen($ms[0])); $spec = substr($spec, strlen($ms[0]));
} elseif (preg_match('/^la\s+/i', $spec, $ms)) { } elseif (preg_match('/^la\s+/i', $spec, $ms)) {
$fem = true; $fem = true;
$aucun = "aucune ";
$un = "une ";
$le = "la "; $le = "la ";
$ce = "cette "; $ce = "cette ";
$de = "de ";
$du = "de la "; $du = "de la ";
$au = "à la "; $au = "à la ";
$spec = substr($spec, strlen($ms[0])); $spec = substr($spec, strlen($ms[0]));
} elseif (preg_match('/^le\s+/i', $spec, $ms)) { } elseif (preg_match('/^le\s+/i', $spec, $ms)) {
$fem = false; $fem = false;
$aucun = "aucun ";
$un = "un ";
$le = "le "; $le = "le ";
$ce = "ce "; $ce = "ce ";
$de = "de ";
$du = "du "; $du = "du ";
$au = "au "; $au = "au ";
$spec = substr($spec, strlen($ms[0])); $spec = substr($spec, strlen($ms[0]));
} else { } else {
$aucun = null;
$un = null;
$le = null; $le = null;
$ce = null; $ce = null;
$de = null;
$du = null; $du = null;
$au = null; $au = null;
} }
@ -86,13 +104,16 @@ class Word {
# si c'est un nom, il faut l'article et le genre # si c'est un nom, il faut l'article et le genre
if ($fem === null) { if ($fem === null) {
throw new ValueException("Vous devez spécifier le genre du nom"); throw new ValueException("Vous devez spécifier le genre du nom");
} elseif ($le === null || $du === null || $au === null) { } elseif ($le === null) {
throw new ValueException("Vous devez spécifier l'article du nom"); throw new ValueException("Vous devez spécifier l'article du nom");
} }
} }
$this->fem = $fem; $this->fem = $fem;
$this->aucun = $aucun;
$this->un = $un;
$this->le = $le; $this->le = $le;
$this->ce = $ce; $this->ce = $ce;
$this->de = $de;
$this->du = $du; $this->du = $du;
$this->au = $au; $this->au = $au;
$this->w = $spec; $this->w = $spec;
@ -168,15 +189,25 @@ class Word {
return "$amount/$max ".$this->w($amount); return "$amount/$max ".$this->w($amount);
} }
function pronom(): string {
return $this->fem? "elle": "il";
}
function articleAucun(): ?string {
return $this->un;
}
function articleUn(): ?string {
return $this->un;
}
/** retourner le mot avec l'article indéfini et la quantité */ /** retourner le mot avec l'article indéfini et la quantité */
function un(int $amount=1): string { function un(int $amount=1): string {
$abs_amount = abs($amount); $abs_amount = abs($amount);
if ($abs_amount == 0) { if ($abs_amount == 0) {
$aucun = $this->fem? "aucune ": "aucun "; return $this->aucun.$this->w($amount);
return $aucun.$this->w($amount);
} elseif ($abs_amount == 1) { } elseif ($abs_amount == 1) {
$un = $this->fem? "une ": "un "; return $this->un.$this->w($amount);
return $un.$this->w($amount);
} else { } else {
return "les $amount ".$this->w($amount); return "les $amount ".$this->w($amount);
} }
@ -193,6 +224,10 @@ class Word {
} }
} }
function articleLe(): ?string {
return $this->le;
}
function le(int $amount=1): string { function le(int $amount=1): string {
$abs_amount = abs($amount); $abs_amount = abs($amount);
if ($abs_amount == 0) { if ($abs_amount == 0) {
@ -214,6 +249,10 @@ class Word {
} }
} }
function articleCe(): string {
return $this->ce;
}
function ce(int $amount=1): string { function ce(int $amount=1): string {
$abs_amount = abs($amount); $abs_amount = abs($amount);
if ($abs_amount == 0) { if ($abs_amount == 0) {
@ -235,6 +274,18 @@ class Word {
} }
} }
function articleDe(): string {
return $this->de;
}
function _de(int $amount=1): string {
return $this->de.$this->w($amount);
}
function articleDu(): string {
return $this->du;
}
function du(int $amount=1): string { function du(int $amount=1): string {
$abs_amount = abs($amount); $abs_amount = abs($amount);
if ($abs_amount == 0) { if ($abs_amount == 0) {
@ -256,6 +307,10 @@ class Word {
} }
} }
function articleAu(): string {
return $this->au;
}
function au(int $amount=1): string { function au(int $amount=1): string {
$abs_amount = abs($amount); $abs_amount = abs($amount);
if ($abs_amount == 0) { if ($abs_amount == 0) {