modifs.mineures sans commentaires
This commit is contained in:
parent
527d4c582b
commit
65430de69a
|
@ -4,6 +4,7 @@ namespace nur\sery\schema\_scalar;
|
||||||
use nulib\cl;
|
use nulib\cl;
|
||||||
use nulib\ValueException;
|
use nulib\ValueException;
|
||||||
use nur\sery\schema\ref\ref_analyze;
|
use nur\sery\schema\ref\ref_analyze;
|
||||||
|
use nur\sery\schema\ref\ref_schema;
|
||||||
use nur\sery\schema\Result;
|
use nur\sery\schema\Result;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -58,6 +59,21 @@ class ScalarResult extends Result {
|
||||||
else $message = str_replace("{key}: ", "", $message);
|
else $message = str_replace("{key}: ", "", $message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static function replace_orig(string &$message, $orig): void {
|
||||||
|
$message = str_replace("{orig}", strval($orig), $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getMessages(ScalarSchema $schema): ?array {
|
||||||
|
return $schema->messages;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getMessage(string $key, ScalarSchema $schema): string {
|
||||||
|
$messages = $this->getMessages($schema);
|
||||||
|
$message = cl::get($messages, $key);
|
||||||
|
if ($message !== null) return $message;
|
||||||
|
return cl::get(ref_schema::MESSAGES, $key);
|
||||||
|
}
|
||||||
|
|
||||||
function setMissing(ScalarSchema $schema): int {
|
function setMissing(ScalarSchema $schema): int {
|
||||||
$this->present = false;
|
$this->present = false;
|
||||||
$this->available = false;
|
$this->available = false;
|
||||||
|
@ -67,7 +83,7 @@ class ScalarResult extends Result {
|
||||||
$this->normalized = true;
|
$this->normalized = true;
|
||||||
return ref_analyze::NORMALIZED;
|
return ref_analyze::NORMALIZED;
|
||||||
} else {
|
} else {
|
||||||
$message = cl::get($schema->messages, "missing");
|
$message = $this->getMessage("missing", $schema);
|
||||||
self::replace_key($message, $schema->name);
|
self::replace_key($message, $schema->name);
|
||||||
$this->message = $message;
|
$this->message = $message;
|
||||||
return ref_analyze::MISSING;
|
return ref_analyze::MISSING;
|
||||||
|
@ -83,7 +99,7 @@ class ScalarResult extends Result {
|
||||||
$this->normalized = true;
|
$this->normalized = true;
|
||||||
return ref_analyze::NORMALIZED;
|
return ref_analyze::NORMALIZED;
|
||||||
} else {
|
} else {
|
||||||
$message = cl::get($schema->messages, "unavailable");
|
$message = $this->getMessage("unavailable", $schema);
|
||||||
self::replace_key($message, $schema->name);
|
self::replace_key($message, $schema->name);
|
||||||
$this->message = $message;
|
$this->message = $message;
|
||||||
return ref_analyze::UNAVAILABLE;
|
return ref_analyze::UNAVAILABLE;
|
||||||
|
@ -99,20 +115,22 @@ class ScalarResult extends Result {
|
||||||
$this->normalized = true;
|
$this->normalized = true;
|
||||||
return ref_analyze::NORMALIZED;
|
return ref_analyze::NORMALIZED;
|
||||||
} else {
|
} else {
|
||||||
$message = cl::get($schema->messages, "null");
|
$message = $this->getMessage("null", $schema);
|
||||||
self::replace_key($message, $schema->name);
|
self::replace_key($message, $schema->name);
|
||||||
$this->message = $message;
|
$this->message = $message;
|
||||||
return ref_analyze::NULL;
|
return ref_analyze::NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function setInvalid(ScalarSchema $schema): int {
|
function setInvalid($value, ScalarSchema $schema): int {
|
||||||
$this->present = true;
|
$this->present = true;
|
||||||
$this->available = true;
|
$this->available = true;
|
||||||
$this->null = false;
|
$this->null = false;
|
||||||
$this->valid = false;
|
$this->valid = false;
|
||||||
$message = cl::get($schema->messages, "invalid");
|
$this->orig = $value;
|
||||||
|
$message = $this->getMessage("invalid", $schema);
|
||||||
self::replace_key($message, $schema->name);
|
self::replace_key($message, $schema->name);
|
||||||
|
self::replace_orig($message, $schema->orig);
|
||||||
$this->message = $message;
|
$this->message = $message;
|
||||||
return ref_analyze::INVALID;
|
return ref_analyze::INVALID;
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,14 +90,14 @@ class ScalarValue extends Value {
|
||||||
else return $result->setValid();
|
else return $result->setValid();
|
||||||
}
|
}
|
||||||
if (is_string($value)) return ref_analyze::STRING;
|
if (is_string($value)) return ref_analyze::STRING;
|
||||||
else return $result->setInvalid($schema);
|
else return $result->setInvalid($schema, $schema);
|
||||||
}
|
}
|
||||||
|
|
||||||
function verifix(?bool $throw=null): bool {
|
function verifix(?bool $throw=null): bool {
|
||||||
$type = $this->getType();
|
$type = $this->getType();
|
||||||
$destKey = $this->destKey;
|
$destKey = $this->destKey;
|
||||||
$value = $this->input->get($destKey);
|
$value = $this->input->get($destKey);
|
||||||
$modified = $type->verifix($value, $this->result);
|
$modified = $type->verifix($value, $this->result, $this->schema);
|
||||||
if ($throw === null) $throw = $this->defaultThrow;
|
if ($throw === null) $throw = $this->defaultThrow;
|
||||||
if ($this->result->valid) $this->input->set($value, $destKey);
|
if ($this->result->valid) $this->input->set($value, $destKey);
|
||||||
else $this->result->throw($throw);
|
else $this->result->throw($throw);
|
||||||
|
|
|
@ -3,6 +3,7 @@ namespace nur\sery\schema\types;
|
||||||
|
|
||||||
use nur\sery\schema\input\Input;
|
use nur\sery\schema\input\Input;
|
||||||
use nur\sery\schema\Result;
|
use nur\sery\schema\Result;
|
||||||
|
use nur\sery\schema\Schema;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface IType: un type de données
|
* Interface IType: un type de données
|
||||||
|
@ -25,8 +26,10 @@ interface IType {
|
||||||
*
|
*
|
||||||
* si la valeur était déjà normalisée, retourner false.
|
* si la valeur était déjà normalisée, retourner false.
|
||||||
*/
|
*/
|
||||||
function verifix(&$value, Result &$result): bool;
|
function verifix(&$value, Result &$result, Schema $schema): bool;
|
||||||
|
|
||||||
/** formatter la valeur pour affichage */
|
/**
|
||||||
|
* formatter la valeur pour affichage. $value est garanti d'être du bon type
|
||||||
|
*/
|
||||||
function format($value, $format=null): string;
|
function format($value, $format=null): string;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
namespace nur\sery\schema\types;
|
namespace nur\sery\schema\types;
|
||||||
|
|
||||||
|
use nulib\cl;
|
||||||
|
|
||||||
class Registry {
|
class Registry {
|
||||||
const TYPES = [
|
const TYPES = [
|
||||||
"string" => tstring::class,
|
"string" => tstring::class,
|
||||||
|
@ -24,7 +26,11 @@ class Registry {
|
||||||
protected $types;
|
protected $types;
|
||||||
|
|
||||||
function get(string $name): IType {
|
function get(string $name): IType {
|
||||||
#XXX les instancier
|
$type = cl::get($this->types, $name);
|
||||||
return $this->types[$name];
|
if ($type === null) {
|
||||||
|
$class = self::TYPES[$name];
|
||||||
|
$type = $this->types[$name] = new $class();
|
||||||
|
}
|
||||||
|
return $type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,71 @@
|
||||||
<?php
|
<?php
|
||||||
namespace nur\sery\schema\types;
|
namespace nur\sery\schema\types;
|
||||||
|
|
||||||
abstract class tint implements IType {
|
use nur\sery\schema\_scalar\ScalarResult;
|
||||||
|
use nur\sery\schema\_scalar\ScalarSchema;
|
||||||
|
use nur\sery\schema\input\Input;
|
||||||
|
use nur\sery\schema\Result;
|
||||||
|
use nur\sery\schema\Schema;
|
||||||
|
|
||||||
|
class tint implements IType {
|
||||||
|
static function ensure_int(&$int): void {
|
||||||
|
if (!is_int($int)) $int = intval($int);
|
||||||
|
}
|
||||||
|
|
||||||
|
static function ensure_nint(&$int): void {
|
||||||
|
if ($int !== null) self::ensure_int($int);
|
||||||
|
}
|
||||||
|
|
||||||
|
function canAnalyze(Input $input, $destKey): bool {
|
||||||
|
if (!$input->isAvailable()) return true;
|
||||||
|
$value = $input->get($destKey);
|
||||||
|
if ($value === false || $value === null) return true;
|
||||||
|
return is_scalar($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
function isAvailable(Input $input, $destKey): bool {
|
||||||
|
return $input->isAvailable($destKey) && $input->get($destKey) !== false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isNull($value): bool {
|
||||||
|
return $value === null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const INT_PATTERN = '/^[-+]?[0-9]+(?:\.[0-9]*)?$/';
|
||||||
|
|
||||||
|
function isValid($value, ?bool &$normalized=null): bool {
|
||||||
|
if (is_string($value)) {
|
||||||
|
$valid = preg_match(self::INT_PATTERN, trim($value));
|
||||||
|
} else {
|
||||||
|
$valid = is_scalar($value);
|
||||||
|
}
|
||||||
|
$normalized = is_int($value);
|
||||||
|
return $valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var ScalarResult $result
|
||||||
|
* @var ScalarSchema $schema
|
||||||
|
*/
|
||||||
|
function verifix(&$value, Result &$result, Schema $schema): bool {
|
||||||
|
if (is_int($value)) {
|
||||||
|
$result->setNormalized();
|
||||||
|
return false;
|
||||||
|
} elseif (is_string($value)) {
|
||||||
|
$int = trim($value);
|
||||||
|
if (preg_match(self::INT_PATTERN, $int)) $value = intval($int);
|
||||||
|
else return $result->setInvalid($value, $schema);
|
||||||
|
} elseif (is_scalar($value)) {
|
||||||
|
$value = intval($value);
|
||||||
|
} else {
|
||||||
|
return $result->setInvalid($value, $schema);
|
||||||
|
}
|
||||||
|
$result->setValid();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function format($value, $format=null): string {
|
||||||
|
if ($format !== null) return sprintf($format, $value);
|
||||||
|
else return strval($value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,15 @@
|
||||||
<?php
|
<?php
|
||||||
namespace nur\sery\schema\types;
|
namespace nur\sery\schema\types;
|
||||||
|
|
||||||
abstract class tstring implements IType {
|
use nur\b\ValueException;
|
||||||
|
use nur\sery\schema\_scalar\ScalarResult;
|
||||||
|
use nur\sery\schema\_scalar\ScalarSchema;
|
||||||
|
use nur\sery\schema\_scalar\ScalarValue;
|
||||||
|
use nur\sery\schema\input\Input;
|
||||||
|
use nur\sery\schema\Result;
|
||||||
|
use nur\sery\schema\Schema;
|
||||||
|
|
||||||
|
class tstring implements IType {
|
||||||
static function ensure_string(&$string): void {
|
static function ensure_string(&$string): void {
|
||||||
if (!is_string($string)) $string = strval($string);
|
if (!is_string($string)) $string = strval($string);
|
||||||
}
|
}
|
||||||
|
@ -9,4 +17,46 @@ abstract class tstring implements IType {
|
||||||
static function ensure_nstring(&$string): void {
|
static function ensure_nstring(&$string): void {
|
||||||
if ($string !== null) self::ensure_string($string);
|
if ($string !== null) self::ensure_string($string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function canAnalyze(Input $input, $destKey): bool {
|
||||||
|
if (!$input->isAvailable()) return true;
|
||||||
|
$value = $input->get($destKey);
|
||||||
|
if ($value === false || $value === null) return true;
|
||||||
|
return is_scalar($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
function isAvailable(Input $input, $destKey): bool {
|
||||||
|
return $input->isAvailable($destKey) && $input->get($destKey) !== false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isNull($value): bool {
|
||||||
|
return $value === null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isValid($value, ?bool &$normalized=null): bool {
|
||||||
|
$valid = is_scalar($value);
|
||||||
|
$normalized = is_string($value);
|
||||||
|
return $valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var ScalarResult $result
|
||||||
|
* @var ScalarSchema $schema
|
||||||
|
*/
|
||||||
|
function verifix(&$value, Result &$result, Schema $schema): bool {
|
||||||
|
if (is_string($value)) {
|
||||||
|
$result->setNormalized();
|
||||||
|
return false;
|
||||||
|
} elseif (is_scalar($value)) {
|
||||||
|
$value = strval($value);
|
||||||
|
} else {
|
||||||
|
return $result->setInvalid($value, $schema);
|
||||||
|
}
|
||||||
|
$result->setValid();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function format($value, $format=null): string {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue