Compare commits

...

4 Commits

Author SHA1 Message Date
Jephté Clain 5353fdbec5 ajout split_tokens 2025-01-24 13:19:49 +04:00
Jephté Clain 17c3c7fe53 ajout merge 2025-01-24 13:19:34 +04:00
Jephté Clain 213ace1cbf ajout any, all 2025-01-24 13:19:21 +04:00
Jephté Clain 741c8ab9b2 maj words 2025-01-24 13:18:54 +04:00
8 changed files with 166 additions and 23 deletions

View File

@ -6,6 +6,7 @@
<cache>
<versions>
<info id="Local/home/jclain/wop/php/nulib/vendor/autoload.php" version="9.6.13" />
<info id="Local/vendor/autoload.php" version="9.6.21" />
</versions>
</cache>
</tool>

View File

@ -103,6 +103,29 @@ abstract class _base {
$condkey++;
}
break;
case "any":
case "all":
case "not any":
case "not all":
# ["list", $values]
if ($op === "any" || $op === "all") {
$condprefix = $op;
$op = "=";
} elseif ($op === "not any" || $op === "not all") {
$condprefix = substr($op, strlen("not "));
$op = "<>";
}
$condprefix .= "(array[";
$condsep = ", ";
$condsuffix = "])";
$condvalues = null;
if (array_key_exists("values", $cond)) {
$condvalues = cl::with($cond["values"]);
} elseif (array_key_exists($condkey, $condkeys)) {
$condvalues = cl::with($cond[$condkeys[$condkey]]);
$condkey++;
}
break;
case "in":
# ["in", $values]
$condprefix = "(";

View File

@ -290,6 +290,14 @@ class str {
return $s;
}
/** découper la chaine sur tout ensemble de caractères espaces */
static final function split_tokens(?string $s): ?array {
$s = self::trim(self::norm_nl($s));
if ($s === null) return null;
elseif ($s === "") return [];
else return preg_split('/\s+/', $s);
}
/**
* joindre les éléments de $parts comme avec implode(), mais en ignorant les
* valeurs fausses (cela n'inclue pas la chaine "0")

View File

@ -31,6 +31,8 @@ class Word {
private $fem;
/** @var string article "le", "la", "l'" */
private $le;
/** @var string article "ce", "cet", "cette" */
private $ce;
/** @var string article "du", "de la", "de l'" */
private $du;
/** @var string article "au", "à la", "à l'" */
@ -56,23 +58,27 @@ class Word {
}
if (preg_match('/^l\'\s*/i', $spec, $ms) && $fem !== null) {
$le = "l'";
$ce = "cet ";
$du = "de l'";
$au = "à l'";
$spec = substr($spec, strlen($ms[0]));
} elseif (preg_match('/^la\s+/i', $spec, $ms)) {
$fem = true;
$le = "la ";
$ce = "cette ";
$du = "de la ";
$au = "à la ";
$spec = substr($spec, strlen($ms[0]));
} elseif (preg_match('/^le\s+/i', $spec, $ms)) {
$fem = false;
$le = "le ";
$ce = "ce ";
$du = "du ";
$au = "au ";
$spec = substr($spec, strlen($ms[0]));
} else {
$le = null;
$ce = null;
$du = null;
$au = null;
}
@ -86,6 +92,7 @@ class Word {
}
$this->fem = $fem;
$this->le = $le;
$this->ce = $ce;
$this->du = $du;
$this->au = $au;
$this->w = $spec;
@ -99,10 +106,11 @@ class Word {
* @param bool|string $fem genre du nom avec lequel accorder les adjectifs,
* avec l'équivalence false==="M" et true==="F"
*/
function w($amount=1, bool $upper1=false, $fem=false): string {
function w($amount=1, bool $upper1=false, $fem=null): string {
if ($amount === true) $amount = 2;
elseif ($amount === false) $amount = 0;
$amount = abs($amount);
$fem ??= $this->fem;
$w = $this->w;
# marque du nombre
if ($amount <= 1) {
@ -136,8 +144,8 @@ class Word {
*
* @param bool|int $amount
*/
function u($amount=1, $fem=false): string {
return $this->w($amount, true, $fem);
function u($amount=1): string {
return $this->w($amount, true);
}
/**
@ -151,62 +159,121 @@ class Word {
}
/** retourner le mot sans article et avec la quantité */
function q(int $amount=1, $fem=false): string {
return $amount." ".$this->w($amount, $fem);
function q(int $amount=1): string {
return $amount." ".$this->w($amount);
}
/** retourner le mot sans article et avec la quantité $amount/$max */
function r(int $amount, int $max, $fem=false): string {
return "$amount/$max ".$this->w($amount, $fem);
function r(int $amount, int $max): string {
return "$amount/$max ".$this->w($amount);
}
/** retourner le mot avec l'article indéfini et la quantité */
function un(int $amount=1, $fem=false): string {
function un(int $amount=1): string {
$abs_amount = abs($amount);
if ($abs_amount == 0) {
$aucun = $this->fem? "aucune ": "aucun ";
return $aucun.$this->w($amount, $fem);
return $aucun.$this->w($amount);
} elseif ($abs_amount == 1) {
$un = $this->fem? "une ": "un ";
return $un.$this->w($amount, $fem);
return $un.$this->w($amount);
} else {
return "les $amount ".$this->w($amount, $fem);
return "les $amount ".$this->w($amount);
}
}
function le(int $amount=1, $fem=false): string {
/** retourner le mot avec l'article indéfini mais sans la quantité */
function _un(int $amount=1): string {
$abs_amount = abs($amount);
if ($abs_amount <= 1) {
$un = $this->fem? "une ": "un ";
return $un.$this->w($amount);
} else {
return "les ".$this->w($amount);
}
}
function le(int $amount=1): string {
$abs_amount = abs($amount);
if ($abs_amount == 0) {
$le = $this->fem? "la 0 ": "le 0 ";
return $le.$this->w($amount, $fem);
return $le.$this->w($amount);
} elseif ($abs_amount == 1) {
return $this->le.$this->w($amount, $fem);
return $this->le.$this->w($amount);
} else {
return "les $amount ".$this->w($amount, $fem);
return "les $amount ".$this->w($amount);
}
}
function du(int $amount=1, $fem=false): string {
function _le(int $amount=1): string {
$abs_amount = abs($amount);
if ($abs_amount <= 1) {
return $this->le.$this->w($amount);
} else {
return "les ".$this->w($amount);
}
}
function ce(int $amount=1): string {
$abs_amount = abs($amount);
if ($abs_amount == 0) {
$ce = $this->fem? "cette 0 ": "ce 0 ";
return $ce.$this->w($amount);
} elseif ($abs_amount == 1) {
return $this->ce.$this->w($amount);
} else {
return "ces $amount ".$this->w($amount);
}
}
function _ce(int $amount=1): string {
$abs_amount = abs($amount);
if ($abs_amount <= 1) {
return $this->ce.$this->w($amount);
} else {
return "ces ".$this->w($amount);
}
}
function du(int $amount=1): string {
$abs_amount = abs($amount);
if ($abs_amount == 0) {
$du = $this->fem? "de la 0 ": "du 0 ";
return $du.$this->w($amount, $fem);
return $du.$this->w($amount);
} elseif ($abs_amount == 1) {
return $this->du.$this->w($amount, $fem);
return $this->du.$this->w($amount);
} else {
return "des $amount ".$this->w($amount, $fem);
return "des $amount ".$this->w($amount);
}
}
function au(int $amount=1, $fem=false): string {
function _du(int $amount=1): string {
$abs_amount = abs($amount);
if ($abs_amount <= 1) {
return $this->du.$this->w($amount);
} else {
return "des ".$this->w($amount);
}
}
function au(int $amount=1): string {
$abs_amount = abs($amount);
if ($abs_amount == 0) {
$au = $this->fem? "à la 0 ": "au 0 ";
return $au.$this->w($amount, $fem);
return $au.$this->w($amount);
} elseif ($abs_amount == 1) {
return $this->au.$this->w($amount, $fem);
return $this->au.$this->w($amount);
} else {
return "aux $amount ".$this->w($amount, $fem);
return "aux $amount ".$this->w($amount);
}
}
function _au(int $amount=1): string {
$abs_amount = abs($amount);
if ($abs_amount <= 1) {
return $this->au.$this->w($amount);
} else {
return "aux ".$this->w($amount);
}
}
}

View File

@ -2,6 +2,11 @@
namespace nulib\text;
class words {
static function with($spec): Word {
if ($spec instanceof Word) return $spec;
else return new Word(strval($spec));
}
static function q(int $count, string $spec, bool $adjective=true): string {
$word = new Word($spec, $adjective);
return $word->q($count);

View File

@ -1,6 +1,7 @@
<?php
namespace nulib\web\params;
use nulib\cl;
use nulib\str;
/**
@ -46,6 +47,14 @@ class F {
));
}
static final function merge(?array $merge=null): array {
$params = [];
foreach (self::get_names() as $name) {
$params[$name] = self::get($name);
}
return cl::merge($params, $merge);
}
/**
* retourner une liste des paramètres qui ont été passés, en les sélectionnant
* selon le contenu de $includes et $excludes. ensuite, fusionner le tableau

View File

@ -0,0 +1,22 @@
<?php
namespace nulib\db\_private;
use nulib\tests\TestCase;
class _baseTest extends TestCase {
function testParse_conds() {
$values = [1, "string"];
$sql = null;
$bindings = null;
_base::parse_conds([
"value" => ["any", $values],
], $sql, $bindings);
self::assertSame([
"value = any(array[:value, :value2])",
], $sql);
self::assertSame([
"value" => 1,
"value2" => "string",
], $bindings);
}
}

View File

@ -5,6 +5,14 @@ namespace nulib;
use nulib\tests\TestCase;
class strTest extends TestCase {
function testSplit_tokens() {
self::assertNull(str::split_tokens(null));
self::assertSame([], str::split_tokens(""));
self::assertSame(["token"], str::split_tokens("token"));
self::assertSame(["t", "u", "v"], str::split_tokens(" t u v "));
self::assertSame(["t", "u", "v", "w"], str::split_tokens("\nt\n\nu\r\nv\rw"));
}
function testCamel2us() {
self::assertSame("a", str::camel2us("a"));
self::assertSame("aa", str::camel2us("aa"));