modifs.mineures sans commentaires
This commit is contained in:
parent
19f6f9c9e1
commit
f4e252a6e0
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
|
@ -1,62 +0,0 @@
|
||||
<?php
|
||||
namespace nur\sery\wip\php\access;
|
||||
|
||||
/**
|
||||
* Class ValueAccess: accès à une valeur unitaire
|
||||
*/
|
||||
class ValueAccess extends AbstractAccess {
|
||||
function __construct(&$value, ?array $params=null) {
|
||||
parent::__construct($params);
|
||||
$this->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,
|
||||
]);
|
||||
}
|
||||
}
|
@ -1,9 +1,8 @@
|
||||
<?php
|
||||
namespace nur\sery\wip\schema\input;
|
||||
|
||||
use nur\sery\wip\php\access\IAccess;
|
||||
use nur\sery\wip\php\access\KeyAccess;
|
||||
use nur\sery\wip\php\access\ValueAccess;
|
||||
use nur\sery\wip\php\access\PropertyAccess;
|
||||
|
||||
/**
|
||||
* Class Input: accès à une valeur
|
||||
@ -13,55 +12,42 @@ use nur\sery\wip\php\access\ValueAccess;
|
||||
class Input implements IInput {
|
||||
const ALLOW_EMPTY = true;
|
||||
|
||||
function __construct(&$value=null, ?array $params=null) {
|
||||
$this->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 {
|
||||
|
@ -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" => ""];
|
||||
|
||||
|
@ -1,69 +0,0 @@
|
||||
<?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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user