modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-08-17 22:29:55 +04:00
parent ab7f7942d6
commit 28a857ee03
9 changed files with 210 additions and 104 deletions

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PhpTestFrameworkVersionCache">
<tools_cache>
<tool tool_name="PHPUnit">
<cache>
<versions>
<info id="Local/home/jclain/wop/php/nur-sery/vendor/autoload.php" version="9.6.20" />
</versions>
</cache>
</tool>
</tools_cache>
</component>
</project>

View File

@ -0,0 +1,66 @@
<?php
namespace nur\sery\wip\php\access;
use nulib\tests\TestCase;
use stdClass;
class KeyAccessTest extends TestCase {
function testAccess() {
$default = new stdClass();
$array = ["null" => 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));
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace nur\sery\wip\php\access;
use nulib\tests\TestCase;
use stdClass;
class ValueAccessTest extends TestCase {
function testAccess() {
$default = new stdClass();
#
$i = null;
$a = new ValueAccess($i);
self::assertFalse($a->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));
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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,
]));
}
}

View File

@ -1,6 +1,9 @@
<?php
namespace nur\sery\wip\schema\input;
use nur\sery\wip\php\access\GetAccess;
use nur\sery\wip\php\access\IAccess;
/**
* Class GetInput: accès à des paramètres de formulaire de type GET uniquement
*
@ -8,28 +11,9 @@ namespace nur\sery\wip\schema\input;
* une référence
*/
class GetInput extends FormInput {
function isPresent($key=null): bool {
if ($key === null) return false;
return array_key_exists($key, $_GET);
}
function isAvailable($key=null): bool {
if ($key === null) return false;
if (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, $_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,
]);
}
}

View File

@ -1,7 +1,9 @@
<?php
namespace nur\sery\wip\schema\input;
use nur\sery\cl;
use nur\sery\wip\php\access\IAccess;
use nur\sery\wip\php\access\KeyAccess;
use nur\sery\wip\php\access\ValueAccess;
/**
* Class Input: accès à une valeur
@ -13,49 +15,52 @@ class Input {
function __construct(&$dest=null, ?array $params=null) {
$this->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();
}
}

View File

@ -1,6 +1,9 @@
<?php
namespace nur\sery\wip\schema\input;
use nur\sery\wip\php\access\IAccess;
use nur\sery\wip\php\access\PostAccess;
/**
* Class PostInput: accès à des paramètres de formulaire de type POST uniquement
*
@ -8,28 +11,9 @@ namespace nur\sery\wip\schema\input;
* une référence
*/
class PostInput extends FormInput {
function isPresent($key=null): bool {
if ($key === null) return false;
return array_key_exists($key, $_POST);
}
function isAvailable($key=null): bool {
if ($key === null) return false;
if (array_key_exists($key, $_POST)) {
return $this->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,
]);
}
}