modifs.mineures sans commentaires
This commit is contained in:
		
							parent
							
								
									66a46c9743
								
							
						
					
					
						commit
						09cd738458
					
				@ -35,7 +35,10 @@ abstract class Value implements ArrayAccess, IteratorAggregate {
 | 
			
		||||
  abstract function getType(): IType;
 | 
			
		||||
 | 
			
		||||
  /** retourner true si la valeur existe */
 | 
			
		||||
  abstract function available(): bool;
 | 
			
		||||
  abstract function isPresent(): bool;
 | 
			
		||||
 | 
			
		||||
  /** retourner true si la valeur est disponible */
 | 
			
		||||
  abstract function isAvailable(): bool;
 | 
			
		||||
 | 
			
		||||
  /** supprimer la valeur */
 | 
			
		||||
  abstract function unset(): void;
 | 
			
		||||
@ -44,16 +47,16 @@ abstract class Value implements ArrayAccess, IteratorAggregate {
 | 
			
		||||
  abstract function set($value): self;
 | 
			
		||||
 | 
			
		||||
  /** obtenir le résultat de l'appel de la fonction {@link set()} */
 | 
			
		||||
  abstract function result(): Result;
 | 
			
		||||
  abstract function getResult(): Result;
 | 
			
		||||
 | 
			
		||||
  /** retourner true si la valeur est valide */
 | 
			
		||||
  abstract function valid(): bool;
 | 
			
		||||
  abstract function isValid(): bool;
 | 
			
		||||
 | 
			
		||||
  /** obtenir la valeur */
 | 
			
		||||
  abstract function get($default=null);
 | 
			
		||||
 | 
			
		||||
  /** retourner true si la valeur est dans sa forme normalisée */
 | 
			
		||||
  abstract function normalized(): bool;
 | 
			
		||||
  abstract function isNormalized(): bool;
 | 
			
		||||
 | 
			
		||||
  /** formatter la valeur pour affichage */
 | 
			
		||||
  abstract function format($format=null): string;
 | 
			
		||||
 | 
			
		||||
@ -9,15 +9,16 @@ 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 $present la valeur existe-t-elle?
 | 
			
		||||
 * @property bool $available si la valeur existe, est-elle disponible?
 | 
			
		||||
 * @property bool $null si la valeur existe, est-elle nulle?
 | 
			
		||||
 * @property bool $valid si la valeur existe, 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"];
 | 
			
		||||
  const KEYS = ["present", "available", "null", "valid", "normalized", "orig", "message"];
 | 
			
		||||
 | 
			
		||||
  function isScalar(?ScalarResult &$scalar=null): bool { $scalar = $this; return true; }
 | 
			
		||||
 | 
			
		||||
@ -36,7 +37,8 @@ class ScalarResult extends Result {
 | 
			
		||||
  function reset(): void {
 | 
			
		||||
    $this->result = array_merge(
 | 
			
		||||
      array_fill_keys(static::KEYS, null), [
 | 
			
		||||
      "missing" => true,
 | 
			
		||||
      "present" => false,
 | 
			
		||||
      "available" => false,
 | 
			
		||||
      "null" => false,
 | 
			
		||||
      "valid" => false,
 | 
			
		||||
      "normalized" => false,
 | 
			
		||||
@ -57,9 +59,10 @@ class ScalarResult extends Result {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function setMissing(ScalarSchema $schema): int {
 | 
			
		||||
    $this->missing = true;
 | 
			
		||||
    $this->present = false;
 | 
			
		||||
    $this->available = false;
 | 
			
		||||
    if (!$schema->required) {
 | 
			
		||||
      $this->null = true;
 | 
			
		||||
      $this->null = false;
 | 
			
		||||
      $this->valid = true;
 | 
			
		||||
      $this->normalized = true;
 | 
			
		||||
      return ref_analyze::NORMALIZED;
 | 
			
		||||
@ -71,8 +74,26 @@ class ScalarResult extends Result {
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function setUnavailable(ScalarSchema $schema): int {
 | 
			
		||||
    $this->present = true;
 | 
			
		||||
    $this->available = false;
 | 
			
		||||
    if (!$schema->required) {
 | 
			
		||||
      $this->null = false;
 | 
			
		||||
      $this->valid = true;
 | 
			
		||||
      $this->normalized = true;
 | 
			
		||||
      return ref_analyze::NORMALIZED;
 | 
			
		||||
    } else {
 | 
			
		||||
      $message = cl::get($schema->messages, "unavailable");
 | 
			
		||||
      self::replace_key($message, $schema->name);
 | 
			
		||||
      $this->message = $message;
 | 
			
		||||
      return ref_analyze::UNAVAILABLE;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function setNull(ScalarSchema $schema): int {
 | 
			
		||||
    $this->missing = true;
 | 
			
		||||
    $this->present = true;
 | 
			
		||||
    $this->available = true;
 | 
			
		||||
    $this->null = true;
 | 
			
		||||
    if ($schema->nullable) {
 | 
			
		||||
      $this->valid = true;
 | 
			
		||||
      $this->normalized = true;
 | 
			
		||||
 | 
			
		||||
@ -61,23 +61,23 @@ class ScalarValue extends Value {
 | 
			
		||||
    $result = $this->result;
 | 
			
		||||
    $result->reset();
 | 
			
		||||
    #XXX résoudre le type
 | 
			
		||||
    if (!$input->available()) return $result->setMissing($schema);
 | 
			
		||||
    if (!$input->isAvailable()) return $result->setUnavailable($schema);
 | 
			
		||||
    $value = $input->get($destKey);
 | 
			
		||||
    if ($value === null) return $result->setNull($schema);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function exists(): bool {
 | 
			
		||||
    return $this->input->exits($this->destKey);
 | 
			
		||||
  function isPresent(): bool {
 | 
			
		||||
    return $this->input->isPresent($this->destKey);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function available(): bool {
 | 
			
		||||
    return $this->input->available($this->destKey);
 | 
			
		||||
  function isAvailable(): bool {
 | 
			
		||||
    return $this->input->isAvailable($this->destKey);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function get($default=null) {
 | 
			
		||||
    $destKey = $this->destKey;
 | 
			
		||||
    $input = $this->input;
 | 
			
		||||
    if ($input->available($destKey)) return $input->get($destKey);
 | 
			
		||||
    if ($input->isAvailable($destKey)) return $input->get($destKey);
 | 
			
		||||
    else return $default;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@ -92,10 +92,10 @@ class ScalarValue extends Value {
 | 
			
		||||
    return $this->type;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function valid(): bool {
 | 
			
		||||
  function isValid(): bool {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function normalized(): bool {
 | 
			
		||||
  function isNormalized(): bool {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
 | 
			
		||||
@ -14,12 +14,12 @@ namespace nur\sery\schema\input;
 | 
			
		||||
class FormInput extends Input {
 | 
			
		||||
  const ALLOW_EMPTY = false;
 | 
			
		||||
 | 
			
		||||
  function exists($key=null): bool {
 | 
			
		||||
  function isPresent($key=null): bool {
 | 
			
		||||
    if ($key === null) return false;
 | 
			
		||||
    return array_key_exists($key, $_POST) || array_key_exists($key, $_GET);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function available($key=null): bool {
 | 
			
		||||
  function isAvailable($key=null): bool {
 | 
			
		||||
    if ($key === null) return false;
 | 
			
		||||
    if (array_key_exists($key, $_POST)) {
 | 
			
		||||
      return $this->allowEmpty || $_POST[$key] !== "";
 | 
			
		||||
 | 
			
		||||
@ -8,12 +8,12 @@ namespace nur\sery\schema\input;
 | 
			
		||||
 * une référence
 | 
			
		||||
 */
 | 
			
		||||
class GetInput extends FormInput {
 | 
			
		||||
  function exists($key=null): bool {
 | 
			
		||||
  function isPresent($key=null): bool {
 | 
			
		||||
    if ($key === null) return false;
 | 
			
		||||
    return array_key_exists($key, $_GET);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function available($key=null): bool {
 | 
			
		||||
  function isAvailable($key=null): bool {
 | 
			
		||||
    if ($key === null) return false;
 | 
			
		||||
    if (array_key_exists($key, $_GET)) {
 | 
			
		||||
      return $this->allowEmpty || $_GET[$key] !== "";
 | 
			
		||||
 | 
			
		||||
@ -24,7 +24,7 @@ class Input {
 | 
			
		||||
  protected $dest;
 | 
			
		||||
 | 
			
		||||
  /** tester si la valeur existe sans tenir compte de $allowEmpty */
 | 
			
		||||
  function exits($key=null): bool {
 | 
			
		||||
  function isPresent($key=null): bool {
 | 
			
		||||
    if ($key === null) return true;
 | 
			
		||||
    $dest = $this->dest;
 | 
			
		||||
    return $dest !== null && array_key_exists($key, $dest);
 | 
			
		||||
@ -36,7 +36,7 @@ class Input {
 | 
			
		||||
  protected $allowEmpty;
 | 
			
		||||
 | 
			
		||||
  /** tester si la valeur est disponible en tenant compte de $allowEmpty */
 | 
			
		||||
  function available($key=null): bool {
 | 
			
		||||
  function isAvailable($key=null): bool {
 | 
			
		||||
    if ($key === null) return true;
 | 
			
		||||
    $dest = $this->dest;
 | 
			
		||||
    if ($dest === null || !array_key_exists($key, $dest)) return false;
 | 
			
		||||
 | 
			
		||||
@ -8,12 +8,12 @@ namespace nur\sery\schema\input;
 | 
			
		||||
 * une référence
 | 
			
		||||
 */
 | 
			
		||||
class PostInput extends FormInput {
 | 
			
		||||
  function exists($key=null): bool {
 | 
			
		||||
  function isPresent($key=null): bool {
 | 
			
		||||
    if ($key === null) return false;
 | 
			
		||||
    return array_key_exists($key, $_POST);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function available($key=null): bool {
 | 
			
		||||
  function isAvailable($key=null): bool {
 | 
			
		||||
    if ($key === null) return false;
 | 
			
		||||
    if (array_key_exists($key, $_POST)) {
 | 
			
		||||
      return $this->allowEmpty || $_POST[$key] !== "";
 | 
			
		||||
 | 
			
		||||
@ -4,19 +4,21 @@ namespace nur\sery\schema\ref;
 | 
			
		||||
class ref_analyze {
 | 
			
		||||
  /** @var int résultat de l'analyse: valeur inexistante */
 | 
			
		||||
  const MISSING = 0;
 | 
			
		||||
  /** @var int résultat de l'analyse: valeur non disponible */
 | 
			
		||||
  const UNAVAILABLE = 1;
 | 
			
		||||
 | 
			
		||||
  /** @var int résultat de l'analyse: valeur nulle */
 | 
			
		||||
  const NULL = 1;
 | 
			
		||||
  const NULL = 2;
 | 
			
		||||
 | 
			
		||||
  /** @var int résultat de l'analyse: valeur chaine à parser */
 | 
			
		||||
  const STRING = 2;
 | 
			
		||||
  const STRING = 3;
 | 
			
		||||
 | 
			
		||||
  /** @var int résultat de l'analyse: valeur invalide */
 | 
			
		||||
  const INVALID = 3;
 | 
			
		||||
  const INVALID = 4;
 | 
			
		||||
 | 
			
		||||
  /** @var int résultat de l'analyse: valeur valide mais pas normalisée */
 | 
			
		||||
  const VALID = 4;
 | 
			
		||||
  const VALID = 5;
 | 
			
		||||
 | 
			
		||||
  /** @var int résultat de l'analyse: valeur valide normalisée */
 | 
			
		||||
  const NORMALIZED = 5;
 | 
			
		||||
  const NORMALIZED = 6;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -42,6 +42,7 @@ class ref_schema {
 | 
			
		||||
 | 
			
		||||
  const MESSAGES = [
 | 
			
		||||
    "missing" => "{key}: Vous devez spécifier cette valeur",
 | 
			
		||||
    "unavailable" => "{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",
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user