From f4e252a6e0a716e1743a7bab861336008937d560 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Wed, 19 Mar 2025 19:32:27 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/php/access/AbstractAccess.php | 4 +- src/php/access/FormAccess.php | 2 + src/php/access/KeyAccess.php | 88 ++++++++++++++++-------- src/php/access/ValueAccess.php | 62 ----------------- src/schema/input/Input.php | 44 ++++-------- tests/wip/php/access/KeyAccessTest.php | 64 ++++++++++++++++- tests/wip/php/access/ValueAccessTest.php | 69 ------------------- 7 files changed, 143 insertions(+), 190 deletions(-) delete mode 100644 src/php/access/ValueAccess.php delete mode 100644 tests/wip/php/access/ValueAccessTest.php diff --git a/src/php/access/AbstractAccess.php b/src/php/access/AbstractAccess.php index f162ca5..417d2cb 100644 --- a/src/php/access/AbstractAccess.php +++ b/src/php/access/AbstractAccess.php @@ -8,8 +8,10 @@ use nulib\cl; * de {@link IAccess} */ abstract class AbstractAccess implements IAccess { + const ALLOW_EMPTY = true; + function __construct(?array $params=null) { - $this->allowEmpty = $params["allow_empty"] ?? true; + $this->allowEmpty = $params["allow_empty"] ?? static::ALLOW_EMPTY; } protected bool $allowEmpty; diff --git a/src/php/access/FormAccess.php b/src/php/access/FormAccess.php index db8d09a..92c2a8c 100644 --- a/src/php/access/FormAccess.php +++ b/src/php/access/FormAccess.php @@ -7,6 +7,8 @@ use nulib\cl; * Class FormAccess: accès à une valeur de $_POST puis $_GET, dans cet ordre */ class FormAccess extends AbstractAccess { + const ALLOW_EMPTY = false; + function __construct($key, ?array $params=null) { parent::__construct($params); $this->key = $key; diff --git a/src/php/access/KeyAccess.php b/src/php/access/KeyAccess.php index 2f7b4c4..b6e5979 100644 --- a/src/php/access/KeyAccess.php +++ b/src/php/access/KeyAccess.php @@ -3,70 +3,102 @@ namespace nur\sery\wip\php\access; use ArrayAccess; use nulib\cl; +use stdClass; /** - * Class KeyAccess: accès à une valeur d'une clé dans un tableau + * Class KeyAccess: accès + * - soit à une valeur d'un chemin de clé dans un tableau (si $key !== null) + * - soit à une valeur scalaire (si $key === null) */ class KeyAccess extends AbstractAccess { - function __construct(&$array, $key, ?array $params=null) { + const ALLOW_NULL = null; + const ALLOW_FALSE = null; + + function __construct(&$dest, $key=null, ?array $params=null) { parent::__construct($params); - $this->array =& $array; + $this->dest =& $dest; $this->key = $key; - $this->allowNull = $params["allow_null"] ?? true; - $this->allowFalse = $params["allow_false"] ?? false; + $this->allowNull = $params["allow_null"] ?? static::ALLOW_NULL; + $this->allowFalse = $params["allow_false"] ?? static::ALLOW_FALSE; } - /** @var array|ArrayAccess */ - protected $array; + /** @var mixed|array|ArrayAccess */ + protected $dest; - /** @var int|string|array */ + /** @var null|int|string|array */ protected $key; - protected bool $allowNull; - - protected bool $allowFalse; - - function reset(&$array): self { - $this->array =& $array; + function reset(&$dest, $key=null): self { + $this->dest =& $dest; + $this->key = $key; return $this; } + function resetKey($key=null): self { + $this->key = $key; + return $this; + } + + protected ?bool $allowNull; + + protected function isAllowNull(): bool { + $allowNull = $this->allowNull; + if ($allowNull !== null) return $allowNull; + return $this->key !== null; + } + + protected ?bool $allowFalse; + + protected function isAllowFalse(): bool { + $allowFalse = $this->allowFalse; + if ($allowFalse !== null) return $allowFalse; + return $this->key === null; + } + function exists(): bool { $key = $this->key; - if ($key === null) return false; - return cl::phas($this->array, $key); + if ($key === null) { + return $this->isAllowNull() || $this->dest !== null; + } else { + return cl::phas($this->dest, $key); + } } function available(): bool { if (!$this->exists()) return false; - $value = cl::pget($this->array, $this->key); + $key = $this->key; + if ($key === null) $value = $this->dest; + else $value = cl::pget($this->dest, $key); if ($value === "") return $this->allowEmpty; - if ($value === null) return $this->allowNull; - if ($value === false) return $this->allowFalse; + if ($value === null) return $this->isAllowNull(); + if ($value === false) return $this->isAllowFalse(); return true; } function get($default=null) { - if ($this->key === null) return $default; - $value = cl::pget($this->array, $this->key, $default); + $key = $this->key; + if ($key === null) $value = $this->dest; + else $value = cl::pget($this->dest, $key, $default); if ($value === "" && !$this->allowEmpty) return $default; - if ($value === null && !$this->allowNull) return $default; - if ($value === false && !$this->allowFalse) return $default; + if ($value === null && !$this->isAllowNull()) return $default; + if ($value === false && !$this->isAllowFalse()) return $default; return $value; } function set($value): void { - if ($this->key === null) return; - cl::pset($this->array, $this->key, $value); + $key = $this->key; + if ($key === null) $this->dest = $value; + else cl::pset($this->dest, $key, $value); } function del(): void { - if ($this->key === null) return; - cl::pdel($this->array, $this->key); + $key = $this->key; + if ($key === null) $this->dest = null; + else cl::pdel($this->dest, $key); } function addKey($key): self { - return new KeyAccess($this->array, cl::merge($this->key, $key), [ + return new KeyAccess($this->dest, cl::merge($this->key, $key), [ "allow_empty" => $this->allowEmpty, "allow_null" => $this->allowNull, "allow_false" => $this->allowFalse, diff --git a/src/php/access/ValueAccess.php b/src/php/access/ValueAccess.php deleted file mode 100644 index 3b95431..0000000 --- a/src/php/access/ValueAccess.php +++ /dev/null @@ -1,62 +0,0 @@ -value =& $value; - $this->allowNull = $params["allow_null"] ?? false; - $this->allowFalse = $params["allow_false"] ?? true; - } - - /** @var mixed */ - protected $value; - - protected bool $allowNull; - - protected bool $allowFalse; - - function reset(&$value): self { - $this->value =& $value; - return $this; - } - - function exists(): bool { - return $this->allowNull || $this->value !== null; - } - - function available(): bool { - if (!$this->exists()) return false; - $value = $this->value; - if ($value === false) return $this->allowFalse; - if ($value === "") return $this->allowEmpty; - return true; - } - - function get($default=null) { - $value = $this->value; - if ($value === null && !$this->allowNull) return $default; - if ($value === false && !$this->allowFalse) return $default; - if ($value === "" && !$this->allowEmpty) return $default; - return $value; - } - - function set($value): void { - $this->value = $value; - } - - function del(): void { - $this->value = null; - } - - function addKey($key): KeyAccess { - return new KeyAccess($this->value, $key, [ - "allow_empty" => $this->allowEmpty, - "allow_null" => $this->allowNull, - "allow_false" => $this->allowFalse, - ]); - } -} diff --git a/src/schema/input/Input.php b/src/schema/input/Input.php index 1ee3c7e..6c63ed9 100644 --- a/src/schema/input/Input.php +++ b/src/schema/input/Input.php @@ -1,9 +1,8 @@ value =& $value; - $this->allowEmpty = $params["allow_empty"] ?? static::ALLOW_EMPTY; - } - - /** @var mixed */ - protected $value; - - /** - * @var bool comment considérer une chaine vide: "" si allowEmpty, null sinon - */ - protected $allowEmpty; - - protected ?ValueAccess $valueAccess = null; - protected ?array $keyAccess = null; - - protected function access($key): IAccess { - if ($key === null) { - return $this->valueAccess ??= new ValueAccess($this->value, [ + function __construct(&$dest=null, ?array $params=null) { + if (is_object($dest)) { + $this->access = new PropertyAccess($dest, null, [ + "allow_empty" => $params["allow_empty"] ?? static::ALLOW_EMPTY, "allow_null" => true, - "allow_empty" => $this->allowEmpty, ]); } else { - return $this->keyAccess[$key] ??= new KeyAccess($this->value, $key, [ - "allow_empty" => $this->allowEmpty, + $this->access = new KeyAccess($dest, null, [ + "allow_empty" => $params["allow_empty"] ?? static::ALLOW_EMPTY, + "allow_null" => true, ]); } } + protected KeyAccess $access; + /** tester si la valeur existe sans tenir compte de $allowEmpty */ function isPresent($key=null): bool { - return $this->access($key)->exists(); + return $this->access->resetKey($key)->exists(); } /** tester si la valeur est disponible en tenant compte de $allowEmpty */ function isAvailable($key=null): bool { - return $this->access($key)->available(); + return $this->access->resetKey($key)->available(); } function get($key=null) { - return $this->access($key)->get(); + return $this->access->resetKey($key)->get(); } function set($value, $key=null): void { - $this->access($key)->set($value); + $this->access->resetKey($key)->set($value); } function unset($key=null): void { - $this->access($key)->del(); + $this->access->resetKey($key)->del(); } function addKey($key): IInput { diff --git a/tests/wip/php/access/KeyAccessTest.php b/tests/wip/php/access/KeyAccessTest.php index 2b1c8ff..d71eb8e 100644 --- a/tests/wip/php/access/KeyAccessTest.php +++ b/tests/wip/php/access/KeyAccessTest.php @@ -5,7 +5,69 @@ use nulib\tests\TestCase; use stdClass; class KeyAccessTest extends TestCase { - function testAccess() { + function testValueAccess() { + $default = new stdClass(); + + # + $i = null; + $a = new KeyAccess($i); + self::assertFalse($a->exists()); + self::assertFalse($a->available()); + self::assertSame($default, $a->get($default)); + + $i = false; + $a = new KeyAccess($i); + self::assertTrue($a->exists()); + self::assertTrue($a->available()); + self::assertSame(false, $a->get($default)); + + $i = ""; + $a = new KeyAccess($i); + self::assertTrue($a->exists()); + self::assertTrue($a->available()); + self::assertSame("", $a->get($default)); + + # + $i = null; + $a = new KeyAccess($i, null, ["allow_null" => false]); + self::assertFalse($a->exists()); + self::assertFalse($a->available()); + self::assertSame($default, $a->get($default)); + + $i = null; + $a = new KeyAccess($i, null, ["allow_null" => true]); + self::assertTrue($a->exists()); + self::assertTrue($a->available()); + self::assertSame(null, $a->get($default)); + + # + $i = false; + $a = new KeyAccess($i, null, ["allow_false" => false]); + self::assertTrue($a->exists()); + self::assertFalse($a->available()); + self::assertSame($default, $a->get($default)); + + $i = false; + $a = new KeyAccess($i, null, ["allow_false" => true]); + self::assertTrue($a->exists()); + self::assertTrue($a->available()); + self::assertSame(false, $a->get($default)); + + # + $i = ""; + $a = new KeyAccess($i, null, ["allow_empty" => false]); + self::assertTrue($a->exists()); + self::assertFalse($a->available()); + self::assertSame($default, $a->get($default)); + + $i = ""; + $a = new KeyAccess($i, null, ["allow_empty" => true]); + self::assertTrue($a->exists()); + self::assertTrue($a->available()); + self::assertSame("", $a->get($default)); + } + + function testArrayAccess() { $default = new stdClass(); $array = ["null" => null, "false" => false, "empty" => ""]; diff --git a/tests/wip/php/access/ValueAccessTest.php b/tests/wip/php/access/ValueAccessTest.php deleted file mode 100644 index 3bd9333..0000000 --- a/tests/wip/php/access/ValueAccessTest.php +++ /dev/null @@ -1,69 +0,0 @@ -exists()); - self::assertFalse($a->available()); - self::assertSame($default, $a->get($default)); - - $i = false; - $a = new ValueAccess($i); - self::assertTrue($a->exists()); - self::assertTrue($a->available()); - self::assertSame(false, $a->get($default)); - - $i = ""; - $a = new ValueAccess($i); - self::assertTrue($a->exists()); - self::assertTrue($a->available()); - self::assertSame("", $a->get($default)); - - # - $i = null; - $a = new ValueAccess($i, ["allow_null" => false]); - self::assertFalse($a->exists()); - self::assertFalse($a->available()); - self::assertSame($default, $a->get($default)); - - $i = null; - $a = new ValueAccess($i, ["allow_null" => true]); - self::assertTrue($a->exists()); - self::assertTrue($a->available()); - self::assertSame(null, $a->get($default)); - - # - $i = false; - $a = new ValueAccess($i, ["allow_false" => false]); - self::assertTrue($a->exists()); - self::assertFalse($a->available()); - self::assertSame($default, $a->get($default)); - - $i = false; - $a = new ValueAccess($i, ["allow_false" => true]); - self::assertTrue($a->exists()); - self::assertTrue($a->available()); - self::assertSame(false, $a->get($default)); - - # - $i = ""; - $a = new ValueAccess($i, ["allow_empty" => false]); - self::assertTrue($a->exists()); - self::assertFalse($a->available()); - self::assertSame($default, $a->get($default)); - - $i = ""; - $a = new ValueAccess($i, ["allow_empty" => true]); - self::assertTrue($a->exists()); - self::assertTrue($a->available()); - self::assertSame("", $a->get($default)); - } -}