modifs.mineures sans commentaires
This commit is contained in:
		
							parent
							
								
									2da6cef41a
								
							
						
					
					
						commit
						94790bef34
					
				@ -1,30 +1,21 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace nur\sery\schema;
 | 
			
		||||
 | 
			
		||||
use nur\sery\schema\_assoc\AssocResult;
 | 
			
		||||
use nur\sery\schema\_list\ListResult;
 | 
			
		||||
use nur\sery\schema\_scalar\ScalarResult;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Class Result: résultat de l'analyse ou de la normalisation d'une valeur
 | 
			
		||||
 *
 | 
			
		||||
 * @property-read bool $valid
 | 
			
		||||
 * @property-read string|int|null $key si la valeur est dans un tableau, clé de
 | 
			
		||||
 * la valeur dans le tableau
 | 
			
		||||
 * @property-read string|null $orig valeur originale avant analyse
 | 
			
		||||
 * @property-read string|null $message message d'erreur si la valeur n'est pas
 | 
			
		||||
 * valide
 | 
			
		||||
 */
 | 
			
		||||
class Result {
 | 
			
		||||
  const KEYS = ["valid", "key", "orig", "message"];
 | 
			
		||||
 | 
			
		||||
  function __construct(?array $result=null) {
 | 
			
		||||
    if ($result === null) $result = ["valid" => false];
 | 
			
		||||
    foreach (static::KEYS as $key) {
 | 
			
		||||
      if (!array_key_exists($key, $result)) $result[$key] = null;
 | 
			
		||||
    }
 | 
			
		||||
    #XXX interpoler [message] avec les clés de $result
 | 
			
		||||
    $this->result = $result;
 | 
			
		||||
abstract class Result {
 | 
			
		||||
  function __construct() {
 | 
			
		||||
    $this->reset();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** @var array  */
 | 
			
		||||
  protected $result;
 | 
			
		||||
  function isAssoc(?AssocResult &$assoc=null): bool { return false; }
 | 
			
		||||
  function isList(?ListResult &$list=null): bool { return false; }
 | 
			
		||||
  function isScalar(?ScalarResult &$scalar=null): bool { return false; }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Obtenir la liste des clés valides pour les valeurs accessibles via cet
 | 
			
		||||
@ -35,7 +26,5 @@ class Result {
 | 
			
		||||
  /** obtenir un objet pour gérer la valeur spécifiée */
 | 
			
		||||
  abstract function getResult($key=null): Result;
 | 
			
		||||
 | 
			
		||||
  function __get($name) {
 | 
			
		||||
    return $this->result[$name];
 | 
			
		||||
  }
 | 
			
		||||
  abstract function reset(): void;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -13,14 +13,14 @@ abstract class Schema implements ArrayAccess {
 | 
			
		||||
   * créer si besoin une nouvelle instance de {@link Schema} à partir d'une
 | 
			
		||||
   * définition de schéma
 | 
			
		||||
   */
 | 
			
		||||
  static function ns(?Schema &$schema, $definition): self {
 | 
			
		||||
  static function ns(?Schema &$schema, $definition, $definitionKey=null): self {
 | 
			
		||||
    if ($schema === null) {
 | 
			
		||||
      if (AssocSchema::isa_definition($definition)) {
 | 
			
		||||
        $schema = new AssocSchema($definition);
 | 
			
		||||
        $schema = new AssocSchema($definition, $definitionKey);
 | 
			
		||||
      } elseif (ListSchema::isa_definition($definition)) {
 | 
			
		||||
        $schema = new ListSchema($definition);
 | 
			
		||||
        $schema = new ListSchema($definition, $definitionKey);
 | 
			
		||||
      } elseif (ScalarSchema::isa_definition($definition)) {
 | 
			
		||||
        $schema = new ScalarSchema($definition);
 | 
			
		||||
        $schema = new ScalarSchema($definition, $definitionKey);
 | 
			
		||||
      } else {
 | 
			
		||||
        throw SchemaException::invalid_schema();
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
@ -10,15 +10,13 @@ class SchemaException extends Exception {
 | 
			
		||||
    return new static($invalid_schema);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static final function invalid_type($value, ?string $message=null): self {
 | 
			
		||||
    $invalid_type = var_export($value, true).": invalid type";
 | 
			
		||||
    if ($message !== null) $invalid_type .= ": $message";
 | 
			
		||||
    return new static($invalid_type);
 | 
			
		||||
  static final function invalid_kind(string $kind, $value, ?string $message=null): self {
 | 
			
		||||
    $invalid_kind = var_export($value, true).": invalid $kind";
 | 
			
		||||
    if ($message !== null) $invalid_kind .= ": $message";
 | 
			
		||||
    return new static($invalid_kind);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static final function invalid_callable($value, ?string $message=null): self {
 | 
			
		||||
    $invalid_callable = var_export($value, true).": invalid callable";
 | 
			
		||||
    if ($message !== null) $invalid_callable .= ": $message";
 | 
			
		||||
    return new static($invalid_callable);
 | 
			
		||||
  static final function invalid_type($value, ?string $message=null): self {
 | 
			
		||||
    return self::invalid_kind("type", $value, $message);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -3,9 +3,16 @@ namespace nur\sery\schema;
 | 
			
		||||
 | 
			
		||||
use ArrayAccess;
 | 
			
		||||
use IteratorAggregate;
 | 
			
		||||
use nur\sery\schema\_assoc\AssocValue;
 | 
			
		||||
use nur\sery\schema\_list\ListValue;
 | 
			
		||||
use nur\sery\schema\_scalar\ScalarValue;
 | 
			
		||||
use nur\sery\schema\types\IType;
 | 
			
		||||
 | 
			
		||||
abstract class Value implements ArrayAccess, IteratorAggregate {
 | 
			
		||||
  function isAssoc(?AssocValue &$assoc=null): bool { return false; }
 | 
			
		||||
  function isList(?ListValue &$list=null): bool { return false; }
 | 
			
		||||
  function isScalar(?ScalarValue &$scalar=null): bool { return false; }
 | 
			
		||||
 | 
			
		||||
  /** spécifier la valeur destination gérée par cet objet */
 | 
			
		||||
  abstract function reset(&$dest, $destKey=null, bool $verifix=true): self;
 | 
			
		||||
 | 
			
		||||
@ -28,7 +35,7 @@ abstract class Value implements ArrayAccess, IteratorAggregate {
 | 
			
		||||
  abstract function getType(): IType;
 | 
			
		||||
 | 
			
		||||
  /** retourner true si la valeur existe */
 | 
			
		||||
  abstract function exists(): bool;
 | 
			
		||||
  abstract function available(): bool;
 | 
			
		||||
 | 
			
		||||
  /** supprimer la valeur */
 | 
			
		||||
  abstract function unset(): void;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										20
									
								
								src/schema/ValueException.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/schema/ValueException.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,20 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace nur\sery\schema;
 | 
			
		||||
 | 
			
		||||
use Exception;
 | 
			
		||||
 | 
			
		||||
class ValueException extends Exception {
 | 
			
		||||
  static final function invalid_kind(string $kind, $value, ?string $message=null): self {
 | 
			
		||||
    $invalid_kind = var_export($value, true).": invalid $kind";
 | 
			
		||||
    if ($message !== null) $invalid_kind .= ": $message";
 | 
			
		||||
    return new static($invalid_kind);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static final function invalid_key($value, ?string $message=null): self {
 | 
			
		||||
    return self::invalid_kind("key", $value, $message);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static final function invalid_value($value, ?string $message=null): self {
 | 
			
		||||
    return self::invalid_kind("value", $value, $message);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@ -4,4 +4,5 @@ namespace nur\sery\schema\_assoc;
 | 
			
		||||
use nur\sery\schema\Result;
 | 
			
		||||
 | 
			
		||||
class AssocResult extends Result {
 | 
			
		||||
  function isAssoc(?AssocResult &$assoc=null): bool { $assoc = $this; return true;}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -31,12 +31,12 @@ class AssocSchema extends Schema {
 | 
			
		||||
    return !cl::have_num_keys($definition);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static function normalize($definition): array {
 | 
			
		||||
  static function normalize($definition, $definitionKey=null): array {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function __construct($definition=null, bool $normalize=true) {
 | 
			
		||||
  function __construct($definition=null, $definitionKey=null, bool $normalize=true) {
 | 
			
		||||
    if ($definition === null) $definition = static::SCHEMA;
 | 
			
		||||
    if ($normalize) $definition = self::normalize($definition);
 | 
			
		||||
    if ($normalize) $definition = self::normalize($definition, $definitionKey);
 | 
			
		||||
    $this->definition = $definition;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -4,4 +4,5 @@ namespace nur\sery\schema\_list;
 | 
			
		||||
use nur\sery\schema\Result;
 | 
			
		||||
 | 
			
		||||
class ListResult extends Result {
 | 
			
		||||
  function isList(?ListResult &$list=null): bool { $list = $this; return true;}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -31,12 +31,12 @@ class ListSchema extends Schema {
 | 
			
		||||
    return $count == 1 && $haveIndex0 && is_array($definition[0]);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static function normalize($definition): array {
 | 
			
		||||
  static function normalize($definition, $definitionKey=null): array {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function __construct($definition=null, bool $normalize=true) {
 | 
			
		||||
  function __construct($definition=null, $definitionKey=null, bool $normalize=true) {
 | 
			
		||||
    if ($definition === null) $definition = static::SCHEMA;
 | 
			
		||||
    if ($normalize) $definition = self::normalize($definition);
 | 
			
		||||
    if ($normalize) $definition = self::normalize($definition, $definitionKey);
 | 
			
		||||
    $this->definition = $definition;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,7 +1,82 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace nur\sery\schema\_scalar;
 | 
			
		||||
 | 
			
		||||
use nulib\cl;
 | 
			
		||||
use nur\sery\schema\ref\ref_analyze;
 | 
			
		||||
use nur\sery\schema\Result;
 | 
			
		||||
use nur\sery\schema\ValueException;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Class ScalarResult: résultat de l'analyse ou de la normalisation d'une valeur
 | 
			
		||||
 *
 | 
			
		||||
 * @property bool $missing la valeur est-elle inexistante?
 | 
			
		||||
 * @property bool $null si elle existe, la valeur est-nulle?
 | 
			
		||||
 * @property bool $valid si elle existe, la valeur est-elle valide?
 | 
			
		||||
 * @property bool $normalized si la valeur est valide, est-elle normalisée?
 | 
			
		||||
 * @property string|null $orig valeur originale avant analyse avec parse()
 | 
			
		||||
 * @property string|null $message message si la valeur n'est pas valide
 | 
			
		||||
 */
 | 
			
		||||
class ScalarResult extends Result {
 | 
			
		||||
  const KEYS = ["missing", "null", "valid", "normalized", "orig", "message"];
 | 
			
		||||
 | 
			
		||||
  function isScalar(?ScalarResult &$scalar=null): bool { $scalar = $this; return true; }
 | 
			
		||||
 | 
			
		||||
  function getKeys(): array {
 | 
			
		||||
    return [null];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function getResult($key=null): Result {
 | 
			
		||||
    if ($key === null) return $this;
 | 
			
		||||
    else throw ValueException::invalid_key($key);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** @var array  */
 | 
			
		||||
  protected $result;
 | 
			
		||||
  
 | 
			
		||||
  function reset(): void {
 | 
			
		||||
    $this->result = array_merge(
 | 
			
		||||
      array_fill_keys(static::KEYS, null), [
 | 
			
		||||
      "missing" => true,
 | 
			
		||||
      "null" => false,
 | 
			
		||||
      "valid" => false,
 | 
			
		||||
      "normalized" => false,
 | 
			
		||||
    ]);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function __get(string $name) {
 | 
			
		||||
    return $this->result[$name];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function __set(string $name, $value): void {
 | 
			
		||||
    $this->result[$name] = $value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function setMissing(ScalarSchema $schema, ?string $key=null): int {
 | 
			
		||||
    $this->missing = true;
 | 
			
		||||
    if (!$schema->required) {
 | 
			
		||||
      $this->null = true;
 | 
			
		||||
      $this->valid = true;
 | 
			
		||||
      $this->normalized = true;
 | 
			
		||||
      return ref_analyze::NORMALIZED;
 | 
			
		||||
    } else {
 | 
			
		||||
      $message = cl::get($schema->messages, "missing");
 | 
			
		||||
      $message = str_replace("{key}", $key, $message);
 | 
			
		||||
      $this->message = $message;
 | 
			
		||||
      return ref_analyze::MISSING;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function setNull(ScalarSchema $schema, ?string $key=null): int {
 | 
			
		||||
    $this->missing = true;
 | 
			
		||||
    if ($schema->nullable) {
 | 
			
		||||
      $this->valid = true;
 | 
			
		||||
      $this->normalized = true;
 | 
			
		||||
      return ref_analyze::NORMALIZED;
 | 
			
		||||
    } else {
 | 
			
		||||
      $message = cl::get($schema->messages, "null");
 | 
			
		||||
      $message = str_replace("{key}", $key, $message);
 | 
			
		||||
      $this->message = $message;
 | 
			
		||||
      return ref_analyze::NULL;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -169,9 +169,9 @@ class ScalarSchema extends Schema {
 | 
			
		||||
    return $definition;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function __construct($definition=null, bool $normalize=true) {
 | 
			
		||||
  function __construct($definition=null, $definitionKey=null, bool $normalize=true) {
 | 
			
		||||
    if ($definition === null) $definition = static::SCHEMA;
 | 
			
		||||
    if ($normalize) $definition = self::normalize($definition);
 | 
			
		||||
    if ($normalize) $definition = self::normalize($definition, $definitionKey);
 | 
			
		||||
    $this->definition = $definition;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -2,13 +2,16 @@
 | 
			
		||||
namespace nur\sery\schema\_scalar;
 | 
			
		||||
 | 
			
		||||
use nur\sery\schema\input\Input;
 | 
			
		||||
use nur\sery\schema\ref\ref_analyze;
 | 
			
		||||
use nur\sery\schema\Result;
 | 
			
		||||
use nur\sery\schema\types\IType;
 | 
			
		||||
use nur\sery\schema\Value;
 | 
			
		||||
use nur\sery\schema\ValueException;
 | 
			
		||||
 | 
			
		||||
class ScalarValue extends Value {
 | 
			
		||||
  function __construct(ScalarSchema $schema, &$dest=null, $key=null, bool $verifix=true) {
 | 
			
		||||
    $this->schema = $schema;
 | 
			
		||||
    $this->result = new ScalarResult();
 | 
			
		||||
    $this->reset($dest, $key, $verifix);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@ -23,7 +26,10 @@ class ScalarValue extends Value {
 | 
			
		||||
  /** @var string|int clé de la valeur dans le tableau destination */
 | 
			
		||||
  protected $destKey;
 | 
			
		||||
 | 
			
		||||
  /** @var ?Result résultat de l'analyse de la valeur */
 | 
			
		||||
  /** @var IType */
 | 
			
		||||
  protected $type;
 | 
			
		||||
 | 
			
		||||
  /** @var ?ScalarResult résultat de l'analyse de la valeur */
 | 
			
		||||
  protected $result;
 | 
			
		||||
 | 
			
		||||
  function reset(&$dest, $destKey=null, bool $verifix=true): Value {
 | 
			
		||||
@ -34,28 +40,53 @@ class ScalarValue extends Value {
 | 
			
		||||
    $this->result = null;
 | 
			
		||||
    #XXX résoudre les types ici?
 | 
			
		||||
    if ($verifix) $this->verifix();
 | 
			
		||||
    else $this->_analyze();
 | 
			
		||||
    return $this;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function getKeys(): array {
 | 
			
		||||
    return [null];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function getValue($key=null): Value {
 | 
			
		||||
    if ($key === null) return $this;
 | 
			
		||||
    throw ValueException::invalid_key($key);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** analyser la valeur et résoudre son type */
 | 
			
		||||
  function _analyze(): int {
 | 
			
		||||
    $schema = $this->schema;
 | 
			
		||||
    $input = $this->input;
 | 
			
		||||
    $destKey = $this->destKey;
 | 
			
		||||
    $result = $this->result;
 | 
			
		||||
    $result->reset();
 | 
			
		||||
    if (!$input->available()) return $result->setMissing($schema, $destKey);
 | 
			
		||||
    $value = $input->get($destKey);
 | 
			
		||||
    if ($value === null) return $result->setNull($schema, $destKey);
 | 
			
		||||
    #XXX résoudre le type
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function exists(): bool {
 | 
			
		||||
    return $this->input->exists($this->destKey);
 | 
			
		||||
    return $this->input->exits($this->destKey);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function available(): bool {
 | 
			
		||||
    return $this->input->available($this->destKey);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function get($default=null) {
 | 
			
		||||
    $destKey = $this->destKey;
 | 
			
		||||
    $input = $this->input;
 | 
			
		||||
    if ($input->exists($destKey)) return $input->get($destKey);
 | 
			
		||||
    if ($input->available($destKey)) return $input->get($destKey);
 | 
			
		||||
    else return $default;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function set($value): self {
 | 
			
		||||
  function set($value, bool $verifix=true): Value {
 | 
			
		||||
    $this->input->set($value, $this->destKey);
 | 
			
		||||
    if ($verifix) $this->verifix();
 | 
			
		||||
    return $this;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** @var IType */
 | 
			
		||||
  protected $type;
 | 
			
		||||
 | 
			
		||||
  function getType(): IType {
 | 
			
		||||
    if ($this->type === null) $this->type = $this->schema->getType($this->destKey);
 | 
			
		||||
    return $this->type;
 | 
			
		||||
 | 
			
		||||
@ -12,15 +12,36 @@ namespace nur\sery\schema\input;
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
class FormInput extends Input {
 | 
			
		||||
  const ALLOW_EMPTY = false;
 | 
			
		||||
 | 
			
		||||
  function exists($key=null): bool {
 | 
			
		||||
    if ($key === null) return false;
 | 
			
		||||
    return array_key_exists($key, $_POST) || array_key_exists($key, $_GET);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function available($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)) return $_POST[$key];
 | 
			
		||||
    elseif (array_key_exists($key, $_GET)) return $_GET[$key];
 | 
			
		||||
    else 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;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -13,9 +13,23 @@ class GetInput extends FormInput {
 | 
			
		||||
    return array_key_exists($key, $_GET);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function available($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)) return $_GET[$key];
 | 
			
		||||
    else return null;
 | 
			
		||||
    if (array_key_exists($key, $_GET)) {
 | 
			
		||||
      $value = $_GET[$key];
 | 
			
		||||
      if ($value === "" && !$this->allowEmpty) return null;
 | 
			
		||||
      return $value;
 | 
			
		||||
    } else {
 | 
			
		||||
      return null;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -4,29 +4,52 @@ namespace nur\sery\schema\input;
 | 
			
		||||
#XXX available permet de préciser qu'une valeur doit exister et être non vide
 | 
			
		||||
# pour être considérée. en faire un paramètre, à moins que ce soit au niveau du
 | 
			
		||||
# type qu'il faille le gérer?
 | 
			
		||||
use nulib\cl;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Class Input: accès à une valeur
 | 
			
		||||
 *
 | 
			
		||||
 * cette implémentation lit depuis et écrit dans une référence
 | 
			
		||||
 */
 | 
			
		||||
class Input {
 | 
			
		||||
  function __construct(&$dest=null) {
 | 
			
		||||
  const ALLOW_EMPTY = true;
 | 
			
		||||
 | 
			
		||||
  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;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  protected $dest;
 | 
			
		||||
 | 
			
		||||
  /** tester si la valeur existe */
 | 
			
		||||
  function exists($key=null): bool {
 | 
			
		||||
  /** tester si la valeur existe sans tenir compte de $allowEmpty */
 | 
			
		||||
  function exits($key=null): bool {
 | 
			
		||||
    if ($key === null) return true;
 | 
			
		||||
    return $this->dest !== null && array_key_exists($key, $this->dest);
 | 
			
		||||
    $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;
 | 
			
		||||
 | 
			
		||||
  /** tester si la valeur est disponible en tenant compte de $allowEmpty */
 | 
			
		||||
  function available($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] !== "";
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function get($key=null) {
 | 
			
		||||
    if ($key === null) return $this->dest;
 | 
			
		||||
    elseif ($this->dest === null) return null;
 | 
			
		||||
    elseif (!array_key_exists($key, $this->dest)) return null;
 | 
			
		||||
    else return $this->dest[$key];
 | 
			
		||||
    $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;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function set($value, $key=null): void {
 | 
			
		||||
 | 
			
		||||
@ -13,9 +13,23 @@ class PostInput extends FormInput {
 | 
			
		||||
    return array_key_exists($key, $_POST);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function available($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)) return $_POST[$key];
 | 
			
		||||
    else return null;
 | 
			
		||||
    if (array_key_exists($key, $_POST)) {
 | 
			
		||||
      $value = $_POST[$key];
 | 
			
		||||
      if ($value === "" && !$this->allowEmpty) return null;
 | 
			
		||||
      return $value;
 | 
			
		||||
    } else {
 | 
			
		||||
      return null;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -41,7 +41,7 @@ class ref_schema {
 | 
			
		||||
  ];
 | 
			
		||||
 | 
			
		||||
  const MESSAGES = [
 | 
			
		||||
    "absent" => "{key}: Vous devez spécifier cette valeur",
 | 
			
		||||
    "missing" => "{key}: Vous devez spécifier cette valeur",
 | 
			
		||||
    "null" => "{key}: Cette valeur ne doit pas être nulle",
 | 
			
		||||
    "empty" => "{key}: Cette valeur ne doit pas être vide",
 | 
			
		||||
    "invalid" => "{key}: {orig}: cette valeur est invalide",
 | 
			
		||||
 | 
			
		||||
@ -1,11 +1,11 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace nur\sery\schema\types;
 | 
			
		||||
 | 
			
		||||
use nur\sery\schema\SchemaException;
 | 
			
		||||
use nur\sery\schema\ValueException;
 | 
			
		||||
 | 
			
		||||
class tcallable implements IType {
 | 
			
		||||
  static function ensure_callable(&$callable): void {
 | 
			
		||||
    if (!is_callable($callable)) throw SchemaException::invalid_callable($callable);
 | 
			
		||||
    if (!is_callable($callable)) throw ValueException::invalid_value($callable);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static function ensure_ncallable(&$callable): void {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user