diff --git a/.idea/php-test-framework.xml b/.idea/php-test-framework.xml new file mode 100644 index 0000000..d766127 --- /dev/null +++ b/.idea/php-test-framework.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/wip/php/access/KeyAccessTest.php b/tests/wip/php/access/KeyAccessTest.php new file mode 100644 index 0000000..2b1c8ff --- /dev/null +++ b/tests/wip/php/access/KeyAccessTest.php @@ -0,0 +1,66 @@ + null, "false" => false, "empty" => ""]; + + # + $a = new KeyAccess($array, "inexistant"); + self::assertFalse($a->exists()); + self::assertFalse($a->available()); + self::assertSame($default, $a->get($default)); + + $a = new KeyAccess($array, "null"); + self::assertTrue($a->exists()); + self::assertTrue($a->available()); + self::assertSame(null, $a->get($default)); + + $a = new KeyAccess($array, "false"); + self::assertTrue($a->exists()); + self::assertFalse($a->available()); + self::assertSame($default, $a->get($default)); + + $a = new KeyAccess($array, "empty"); + self::assertTrue($a->exists()); + self::assertTrue($a->available()); + self::assertSame("", $a->get($default)); + + # + $a = new KeyAccess($array, "null", ["allow_null" => false]); + self::assertTrue($a->exists()); + self::assertFalse($a->available()); + self::assertSame($default, $a->get($default)); + + $a = new KeyAccess($array, "null", ["allow_null" => true]); + self::assertTrue($a->exists()); + self::assertTrue($a->available()); + self::assertSame(null, $a->get($default)); + + # + $a = new KeyAccess($array, "false", ["allow_false" => false]); + self::assertTrue($a->exists()); + self::assertFalse($a->available()); + self::assertSame($default, $a->get($default)); + + $a = new KeyAccess($array, "false", ["allow_false" => true]); + self::assertTrue($a->exists()); + self::assertTrue($a->available()); + self::assertSame(false, $a->get($default)); + + # + $a = new KeyAccess($array, "empty", ["allow_empty" => false]); + self::assertTrue($a->exists()); + self::assertFalse($a->available()); + self::assertSame($default, $a->get($default)); + + $a = new KeyAccess($array, "empty", ["allow_empty" => true]); + self::assertTrue($a->exists()); + self::assertTrue($a->available()); + self::assertSame("", $a->get($default)); + } +} diff --git a/tests/wip/php/access/ValueAccessTest.php b/tests/wip/php/access/ValueAccessTest.php new file mode 100644 index 0000000..3bd9333 --- /dev/null +++ b/tests/wip/php/access/ValueAccessTest.php @@ -0,0 +1,69 @@ +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)); + } +} diff --git a/wip/php/access/KeyAccess.php b/wip/php/access/KeyAccess.php index cd6dcf8..8010a12 100644 --- a/wip/php/access/KeyAccess.php +++ b/wip/php/access/KeyAccess.php @@ -36,15 +36,13 @@ class KeyAccess extends AbstractAccess { function exists(): bool { $key = $this->key; if ($key === null) return false; - $exists = cl::has($this->dest, $key); - if (!$exists) return false; - if ($this->allowNull) return true; - return cl::get($this->dest, $key) !== null; + return cl::has($this->dest, $key); } function available(): bool { if (!$this->exists()) return false; - $value = $this->get(); + $value = cl::get($this->dest, $this->key); + if ($value === null) return $this->allowNull; if ($value === false) return $this->allowFalse; if ($value === "") return $this->allowEmpty; return true; diff --git a/wip/php/access/ValueAccess.php b/wip/php/access/ValueAccess.php index 910e7c8..3088785 100644 --- a/wip/php/access/ValueAccess.php +++ b/wip/php/access/ValueAccess.php @@ -32,7 +32,7 @@ class ValueAccess extends AbstractAccess { function available(): bool { if (!$this->exists()) return false; - $value = $this->get(); + $value = $this->dest; if ($value === false) return $this->allowFalse; if ($value === "") return $this->allowEmpty; return true; diff --git a/wip/schema/input/FormInput.php b/wip/schema/input/FormInput.php index ce840e9..01b56c2 100644 --- a/wip/schema/input/FormInput.php +++ b/wip/schema/input/FormInput.php @@ -3,6 +3,11 @@ namespace nur\sery\wip\schema\input; #XXX implémenter le renommage de paramètres et faire des méthodes pour # construire des querystring et paramètres de formulaires +use nur\sery\wip\php\access\FormAccess; +use nur\sery\wip\php\access\IAccess; +use nur\sery\wip\php\access\KeyAccess; +use nur\sery\wip\php\access\ShadowAccess; + /** * Class FormInput: accès à des paramètres de formulaire (POST ou GET, dans cet * ordre) @@ -13,34 +18,15 @@ namespace nur\sery\wip\schema\input; class FormInput extends Input { const ALLOW_EMPTY = false; - function isPresent($key=null): bool { - if ($key === null) return false; - return array_key_exists($key, $_POST) || array_key_exists($key, $_GET); + protected function formAccess($key): IAccess { + return new FormAccess($key, [ + "allow_empty" => $this->allowEmpty, + ]); } - function isAvailable($key=null): bool { - if ($key === null) return false; - if (array_key_exists($key, $_POST)) { - return $this->allowEmpty || $_POST[$key] !== ""; - } elseif (array_key_exists($key, $_GET)) { - return $this->allowEmpty || $_GET[$key] !== ""; - } else { - return false; - } - } - - function get($key=null) { - if ($key === null) return null; - if (array_key_exists($key, $_POST)) { - $value = $_POST[$key]; - if ($value === "" && !$this->allowEmpty) return null; - return $value; - } elseif (array_key_exists($key, $_GET)) { - $value = $_GET[$key]; - if ($value === "" && !$this->allowEmpty) return null; - return $value; - } else { - return null; - } + protected function access($key): IAccess { + return $this->keyAccess[$key] ??= new ShadowAccess($this->formAccess($key), new KeyAccess($this->dest, $key, [ + "allow_empty" => $this->allowEmpty, + ])); } } diff --git a/wip/schema/input/GetInput.php b/wip/schema/input/GetInput.php index af4bde8..766c8e5 100644 --- a/wip/schema/input/GetInput.php +++ b/wip/schema/input/GetInput.php @@ -1,6 +1,9 @@ allowEmpty || $_GET[$key] !== ""; - } else { - return false; - } - } - - function get($key=null) { - if ($key === null) return null; - if (array_key_exists($key, $_GET)) { - $value = $_GET[$key]; - if ($value === "" && !$this->allowEmpty) return null; - return $value; - } else { - return null; - } + protected function formAccess($key): IAccess { + return new GetAccess($key, [ + "allow_empty" => $this->allowEmpty, + ]); } } diff --git a/wip/schema/input/Input.php b/wip/schema/input/Input.php index 86f907a..881adb0 100644 --- a/wip/schema/input/Input.php +++ b/wip/schema/input/Input.php @@ -1,7 +1,9 @@ dest =& $dest; - $allowEmpty = cl::get($params, "allow_empty"); - if ($allowEmpty === null) $allowEmpty = static::ALLOW_EMPTY; - $this->allowEmpty = $allowEmpty; + $this->allowEmpty = $params["allow_empty"] ?? static::ALLOW_EMPTY; } + /** @var mixed */ protected $dest; - /** tester si la valeur existe sans tenir compte de $allowEmpty */ - function isPresent($key=null): bool { - if ($key === null) return true; - $dest = $this->dest; - return $dest !== null && array_key_exists($key, $dest); - } - /** * @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->dest, [ + "allow_null" => true, + "allow_empty" => $this->allowEmpty, + ]); + } else { + return $this->keyAccess[$key] ??= new KeyAccess($this->dest, $key, [ + "allow_empty" => $this->allowEmpty, + ]); + } + } + + /** tester si la valeur existe sans tenir compte de $allowEmpty */ + function isPresent($key=null): bool { + return $this->access($key)->exists(); + } + /** tester si la valeur est disponible en tenant compte de $allowEmpty */ function isAvailable($key=null): bool { - if ($key === null) return true; - $dest = $this->dest; - if ($dest === null || !array_key_exists($key, $dest)) return false; - return $this->allowEmpty || $dest[$key] !== ""; + return $this->access($key)->available(); } function get($key=null) { - $dest = $this->dest; - if ($key === null) return $dest; - if ($dest === null || !array_key_exists($key, $dest)) return null; - $value = $dest[$key]; - if ($value === "" && !$this->allowEmpty) return null; - return $value; + return $this->access($key)->get(); } function set($value, $key=null): void { - if ($key === null) $this->dest = $value; - else $this->dest[$key] = $value; + $this->access($key)->set($value); } function unset($key=null): void { - if ($key === null) $this->dest = null; - else unset($this->dest[$key]); + $this->access($key)->del(); } } diff --git a/wip/schema/input/PostInput.php b/wip/schema/input/PostInput.php index 47ea40f..bf720ab 100644 --- a/wip/schema/input/PostInput.php +++ b/wip/schema/input/PostInput.php @@ -1,6 +1,9 @@ allowEmpty || $_POST[$key] !== ""; - } else { - return false; - } - } - - function get($key=null) { - if ($key === null) return null; - if (array_key_exists($key, $_POST)) { - $value = $_POST[$key]; - if ($value === "" && !$this->allowEmpty) return null; - return $value; - } else { - return null; - } + protected function formAccess($key): IAccess { + return new PostAccess($key, [ + "allow_empty" => $this->allowEmpty, + ]); } }