modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-04-23 00:13:30 +04:00
parent aa76d613fb
commit 31d67148c3
11 changed files with 103 additions and 42 deletions

View File

@ -21,7 +21,8 @@ interface IType {
/** /**
* analyser, corriger éventuellement et normaliser la valeur * analyser, corriger éventuellement et normaliser la valeur
* *
* si la valeur était déjà normalisée, retourner false. * si la valeur était déjà normalisée, ou si une erreur s'est produite,
* retourner false.
*/ */
function verifix(&$value, Result &$result, Schema $schema): bool; function verifix(&$value, Result &$result, Schema $schema): bool;

View File

@ -0,0 +1,14 @@
<?php
namespace nur\sery\schema\types;
use nur\sery\schema\input\Input;
abstract class _tsimple implements IType {
function isAvailable(Input $input, $destKey): bool {
return $input->isAvailable($destKey) && $input->get($destKey) !== false;
}
function isNull($value): bool {
return $value === null || (is_string($value) && trim($value) === "");
}
}

View File

@ -2,8 +2,10 @@
namespace nur\sery\schema\types; namespace nur\sery\schema\types;
use nur\sery\cl; use nur\sery\cl;
use nur\sery\schema\Result;
use nur\sery\schema\Schema;
abstract class tarray implements IType { class tarray extends _tsimple {
static function ensure_array(&$array): void { static function ensure_array(&$array): void {
if (!is_array($array)) $array = cl::with($array); if (!is_array($array)) $array = cl::with($array);
} }
@ -11,4 +13,15 @@ abstract class tarray implements IType {
static function ensure_narray(&$array): void { static function ensure_narray(&$array): void {
if ($array !== null) self::ensure_array($array); if ($array !== null) self::ensure_array($array);
} }
function isValid($value, ?bool &$normalized=null): bool {
$normalized = is_array($value);
return is_scalar($value) || is_array($value);
}
function verifix(&$value, Result &$result, Schema $schema): bool {
}
function format($value, $format=null): string {
}
} }

View File

@ -9,7 +9,7 @@ use nur\sery\schema\input\Input;
use nur\sery\schema\Result; use nur\sery\schema\Result;
use nur\sery\schema\Schema; use nur\sery\schema\Schema;
class tbool implements IType { class tbool extends _tsimple {
/** liste de valeurs chaines à considérer comme 'OUI' */ /** liste de valeurs chaines à considérer comme 'OUI' */
const YES_VALUES = [ const YES_VALUES = [
# IMPORTANT: ordonner par taille décroissante pour compatibilité avec parse() # IMPORTANT: ordonner par taille décroissante pour compatibilité avec parse()
@ -57,18 +57,14 @@ class tbool implements IType {
return $input->isAvailable($destKey); return $input->isAvailable($destKey);
} }
function isNull($value): bool {
return $value === null || (is_string($value) && trim($value) === "");
}
function isValid($value, ?bool &$normalized=null): bool { function isValid($value, ?bool &$normalized=null): bool {
$normalized = is_bool($value);
if (is_string($value)) { if (is_string($value)) {
$value = trim($value); $value = trim($value);
$valid = self::is_yes($value) || self::is_no($value); $valid = self::is_yes($value) || self::is_no($value);
} else { } else {
$valid = is_scalar($value); $valid = is_scalar($value);
} }
$normalized = is_bool($value);
return $valid; return $valid;
} }

View File

@ -1,9 +1,13 @@
<?php <?php
namespace nur\sery\schema\types; namespace nur\sery\schema\types;
use nur\sery\php\func;
use nur\sery\schema\Result;
use nur\sery\schema\Schema;
use nur\sery\ValueException; use nur\sery\ValueException;
use stdClass;
abstract class tcallable implements IType { class tcallable extends _tsimple {
static function ensure_callable(&$callable): void { static function ensure_callable(&$callable): void {
if (!is_callable($callable)) throw ValueException::invalid_type($callable, "callable"); if (!is_callable($callable)) throw ValueException::invalid_type($callable, "callable");
} }
@ -11,4 +15,15 @@ abstract class tcallable implements IType {
static function ensure_ncallable(&$callable): void { static function ensure_ncallable(&$callable): void {
if ($callable !== null) self::ensure_callable($callable); if ($callable !== null) self::ensure_callable($callable);
} }
function isValid($value, ?bool &$normalized=null): bool {
$normalized = is_callable($value);
return func::check_func($value, stdClass::class);
}
function verifix(&$value, Result &$result, Schema $schema): bool {
}
function format($value, $format=null): string {
}
} }

View File

@ -1,7 +1,10 @@
<?php <?php
namespace nur\sery\schema\types; namespace nur\sery\schema\types;
abstract class tcontent implements IType { use nur\sery\schema\Result;
use nur\sery\schema\Schema;
abstract class tcontent extends _tsimple {
static function ensure_content(&$content): void { static function ensure_content(&$content): void {
if (!is_string($content) && !is_array($content)) $content = strval($content); if (!is_string($content) && !is_array($content)) $content = strval($content);
} }
@ -9,4 +12,15 @@ abstract class tcontent implements IType {
static function ensure_ncontent(&$content): void { static function ensure_ncontent(&$content): void {
if ($content !== null) self::ensure_content($content); if ($content !== null) self::ensure_content($content);
} }
function isValid($value, ?bool &$normalized=null): bool {
$normalized = is_string($value) || is_array($value);
return is_scalar($value) || is_array($value);
}
function verifix(&$value, Result &$result, Schema $schema): bool {
}
function format($value, $format=null): string {
}
} }

View File

@ -7,7 +7,7 @@ use nur\sery\schema\input\Input;
use nur\sery\schema\Result; use nur\sery\schema\Result;
use nur\sery\schema\Schema; use nur\sery\schema\Schema;
class tfloat implements IType { class tfloat extends _tsimple {
static function ensure_float(&$float): void { static function ensure_float(&$float): void {
if (!is_float($float)) $float = floatval($float); if (!is_float($float)) $float = floatval($float);
} }
@ -16,18 +16,10 @@ class tfloat implements IType {
if ($float !== null) self::ensure_float($float); if ($float !== null) self::ensure_float($float);
} }
function isAvailable(Input $input, $destKey): bool {
return $input->isAvailable($destKey) && $input->get($destKey) !== false;
}
function isNull($value): bool {
return $value === null || (is_string($value) && trim($value) === "");
}
function isValid($value, ?bool &$normalized=null): bool { function isValid($value, ?bool &$normalized=null): bool {
$normalized = is_float($value);
if (is_string($value)) $valid = is_numeric(trim($value)); if (is_string($value)) $valid = is_numeric(trim($value));
else $valid = is_scalar($value); else $valid = is_scalar($value);
$normalized = is_float($value);
return $valid; return $valid;
} }

View File

@ -7,7 +7,7 @@ use nur\sery\schema\input\Input;
use nur\sery\schema\Result; use nur\sery\schema\Result;
use nur\sery\schema\Schema; use nur\sery\schema\Schema;
class tint implements IType { class tint extends _tsimple {
static function ensure_int(&$int): void { static function ensure_int(&$int): void {
if (!is_int($int)) $int = intval($int); if (!is_int($int)) $int = intval($int);
} }
@ -16,20 +16,12 @@ class tint implements IType {
if ($int !== null) self::ensure_int($int); if ($int !== null) self::ensure_int($int);
} }
function isAvailable(Input $input, $destKey): bool {
return $input->isAvailable($destKey) && $input->get($destKey) !== false;
}
function isNull($value): bool {
return $value === null || (is_string($value) && trim($value) === "");
}
const INT_PATTERN = '/^[-+]?[0-9]+(?:\.[0-9]*)?$/'; const INT_PATTERN = '/^[-+]?[0-9]+(?:\.[0-9]*)?$/';
function isValid($value, ?bool &$normalized=null): bool { function isValid($value, ?bool &$normalized=null): bool {
$normalized = is_int($value);
if (is_string($value)) $valid = is_numeric(trim($value)); if (is_string($value)) $valid = is_numeric(trim($value));
else $valid = is_scalar($value); else $valid = is_scalar($value);
$normalized = is_int($value);
return $valid; return $valid;
} }

View File

@ -1,7 +1,10 @@
<?php <?php
namespace nur\sery\schema\types; namespace nur\sery\schema\types;
abstract class tkey implements IType { use nur\sery\schema\Result;
use nur\sery\schema\Schema;
class tkey extends _tsimple {
static function ensure_key(&$key): void { static function ensure_key(&$key): void {
if (!is_string($key) && !is_int($key)) $key = strval($key); if (!is_string($key) && !is_int($key)) $key = strval($key);
} }
@ -9,4 +12,15 @@ abstract class tkey implements IType {
static function ensure_nkey(&$key): void { static function ensure_nkey(&$key): void {
if ($key !== null) self::ensure_key($key); if ($key !== null) self::ensure_key($key);
} }
function isValid($value, ?bool &$normalized=null): bool {
$normalized = is_string($value) || is_int($value);
return is_scalar($value);
}
function verifix(&$value, Result &$result, Schema $schema): bool {
}
function format($value, $format=null): string {
}
} }

View File

@ -1,7 +1,10 @@
<?php <?php
namespace nur\sery\schema\types; namespace nur\sery\schema\types;
abstract class tpkey implements IType { use nur\sery\schema\Result;
use nur\sery\schema\Schema;
class tpkey extends _tsimple {
static function ensure_pkey(&$pkey): void { static function ensure_pkey(&$pkey): void {
if (!is_string($pkey) && !is_int($pkey) && !is_array($pkey)) $pkey = strval($pkey); if (!is_string($pkey) && !is_int($pkey) && !is_array($pkey)) $pkey = strval($pkey);
if (is_array($pkey)) { if (is_array($pkey)) {
@ -15,4 +18,15 @@ abstract class tpkey implements IType {
static function ensure_npkey(&$pkey): void { static function ensure_npkey(&$pkey): void {
if ($pkey !== null) self::ensure_pkey($pkey); if ($pkey !== null) self::ensure_pkey($pkey);
} }
function isValid($value, ?bool &$normalized=null): bool {
$normalized = is_string($value) || is_int($value) || is_array($value);
return is_scalar($value) || is_array($value);
}
function verifix(&$value, Result &$result, Schema $schema): bool {
}
function format($value, $format=null): string {
}
} }

View File

@ -7,7 +7,7 @@ use nur\sery\schema\input\Input;
use nur\sery\schema\Result; use nur\sery\schema\Result;
use nur\sery\schema\Schema; use nur\sery\schema\Schema;
class tstring implements IType { class tstring extends _tsimple {
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);
} }
@ -16,18 +16,13 @@ class tstring implements IType {
if ($string !== null) self::ensure_string($string); if ($string !== null) self::ensure_string($string);
} }
function isAvailable(Input $input, $destKey): bool {
return $input->isAvailable($destKey) && $input->get($destKey) !== false;
}
function isNull($value): bool { function isNull($value): bool {
return $value === null; return $value === null;
} }
function isValid($value, ?bool &$normalized=null): bool { function isValid($value, ?bool &$normalized=null): bool {
$valid = is_scalar($value);
$normalized = is_string($value); $normalized = is_string($value);
return $valid; return is_scalar($value);
} }
/** /**
@ -40,11 +35,12 @@ class tstring implements IType {
return false; return false;
} elseif (is_scalar($value)) { } elseif (is_scalar($value)) {
$value = strval($value); $value = strval($value);
} else {
return $result->setInvalid($value, $schema);
}
$result->setValid(); $result->setValid();
return true; return true;
} else {
$result->setInvalid($value, $schema);
return false;
}
} }
function format($value, $format=null): string { function format($value, $format=null): string {