<pman>Intégration de la branche dev74
This commit is contained in:
		
						commit
						c7dd912c54
					
				
							
								
								
									
										6
									
								
								.idea/nur-ture.iml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										6
									
								
								.idea/nur-ture.iml
									
									
									
										generated
									
									
									
								
							| @ -4,10 +4,8 @@ | |||||||
|     <content url="file://$MODULE_DIR$"> |     <content url="file://$MODULE_DIR$"> | ||||||
|       <sourceFolder url="file://$MODULE_DIR$/nur_src" isTestSource="false" packagePrefix="nur\" /> |       <sourceFolder url="file://$MODULE_DIR$/nur_src" isTestSource="false" packagePrefix="nur\" /> | ||||||
|       <sourceFolder url="file://$MODULE_DIR$/nur_tests" isTestSource="true" packagePrefix="nur\" /> |       <sourceFolder url="file://$MODULE_DIR$/nur_tests" isTestSource="true" packagePrefix="nur\" /> | ||||||
|       <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" packagePrefix="nur\sery\wip\" /> |       <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" packagePrefix="nulib\" /> | ||||||
|       <sourceFolder url="file://$MODULE_DIR$/src_app" isTestSource="false" packagePrefix="nur\sery\" /> |       <sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" packagePrefix="nulib\" /> | ||||||
|       <sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" packagePrefix="nur\sery\" /> |  | ||||||
|       <sourceFolder url="file://$MODULE_DIR$/src_glue" isTestSource="false" packagePrefix="nulib\" /> |  | ||||||
|       <excludeFolder url="file://$MODULE_DIR$/vendor" /> |       <excludeFolder url="file://$MODULE_DIR$/vendor" /> | ||||||
|     </content> |     </content> | ||||||
|     <orderEntry type="inheritedJdk" /> |     <orderEntry type="inheritedJdk" /> | ||||||
|  | |||||||
							
								
								
									
										7
									
								
								bin/json2yml.php
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										7
									
								
								bin/json2yml.php
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,7 @@ | |||||||
|  | #!/usr/bin/php
 | ||||||
|  | <?php | ||||||
|  | require $_composer_autoload_path?? __DIR__.'/../vendor/autoload.php'; | ||||||
|  | 
 | ||||||
|  | use nulib\tools\Json2yamlApp; | ||||||
|  | 
 | ||||||
|  | Json2yamlApp::run(); | ||||||
							
								
								
									
										7
									
								
								bin/yml2json.php
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										7
									
								
								bin/yml2json.php
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,7 @@ | |||||||
|  | #!/usr/bin/php
 | ||||||
|  | <?php | ||||||
|  | require $_composer_autoload_path?? __DIR__.'/../vendor/autoload.php'; | ||||||
|  | 
 | ||||||
|  | use nulib\tools\Yaml2jsonApp; | ||||||
|  | 
 | ||||||
|  | Yaml2jsonApp::run(); | ||||||
| @ -64,9 +64,7 @@ | |||||||
| 	}, | 	}, | ||||||
| 	"autoload": { | 	"autoload": { | ||||||
| 		"psr-4": { | 		"psr-4": { | ||||||
| 			"nulib\\": "src_glue", | 			"nulib\\": "src", | ||||||
| 			"nur\\sery\\wip\\": "src", |  | ||||||
| 			"nur\\sery\\": "src_app", |  | ||||||
| 			"nur\\": "nur_src" | 			"nur\\": "nur_src" | ||||||
| 		}, | 		}, | ||||||
| 		"files": [ | 		"files": [ | ||||||
| @ -75,13 +73,15 @@ | |||||||
| 	}, | 	}, | ||||||
| 	"autoload-dev": { | 	"autoload-dev": { | ||||||
| 		"psr-4": { | 		"psr-4": { | ||||||
| 			"nur\\sery\\": "tests", | 			"nulib\\": "tests", | ||||||
| 			"nur\\": "nur_tests" | 			"nur\\": "nur_tests" | ||||||
| 		} | 		} | ||||||
| 	}, | 	}, | ||||||
| 	"bin": [ | 	"bin": [ | ||||||
| 		"bin/dumpser.php", | 		"bin/dumpser.php", | ||||||
| 		"bin/csv2xlsx.php", | 		"bin/csv2xlsx.php", | ||||||
|  | 		"bin/json2yml.php", | ||||||
|  | 		"bin/yml2json.php", | ||||||
| 		"nur_bin/compctl.php", | 		"nur_bin/compctl.php", | ||||||
| 		"nur_bin/compdep.php", | 		"nur_bin/compdep.php", | ||||||
| 		"nur_bin/datectl.php", | 		"nur_bin/datectl.php", | ||||||
|  | |||||||
							
								
								
									
										2
									
								
								composer.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										2
									
								
								composer.lock
									
									
									
										generated
									
									
									
								
							| @ -405,6 +405,8 @@ | |||||||
|             "require-dev": { |             "require-dev": { | ||||||
|                 "ext-curl": "*", |                 "ext-curl": "*", | ||||||
|                 "ext-pcntl": "*", |                 "ext-pcntl": "*", | ||||||
|  |                 "ext-pdo": "*", | ||||||
|  |                 "ext-pgsql": "*", | ||||||
|                 "ext-posix": "*", |                 "ext-posix": "*", | ||||||
|                 "ext-sqlite3": "*", |                 "ext-sqlite3": "*", | ||||||
|                 "nulib/tests": "^8.2" |                 "nulib/tests": "^8.2" | ||||||
|  | |||||||
| @ -2,6 +2,6 @@ | |||||||
| <?php | <?php | ||||||
| require $_composer_autoload_path?? __DIR__.'/../vendor/autoload.php'; | require $_composer_autoload_path?? __DIR__.'/../vendor/autoload.php'; | ||||||
| 
 | 
 | ||||||
| use nur\sery\tools\SteamTrainApp; | use nulib\tools\SteamTrainApp; | ||||||
| 
 | 
 | ||||||
| SteamTrainApp::run(); | SteamTrainApp::run(); | ||||||
|  | |||||||
| @ -287,10 +287,10 @@ abstract class FileCachedValue extends Parametrable implements ArrayAccess, Coun | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** supprimer le fichier s'il a expiré */ |   /** supprimer le fichier s'il a expiré */ | ||||||
|   function deleteExpired(): bool { |   function deleteExpired(bool $force=false): bool { | ||||||
|     try { |     try { | ||||||
|       if ($this->shouldUpdate()) { |       if ($force || $this->shouldUpdate()) { | ||||||
|         unlink($this->ppFile); |         @unlink($this->ppFile); | ||||||
|         return true; |         return true; | ||||||
|       } |       } | ||||||
|     } finally { |     } finally { | ||||||
|  | |||||||
| @ -2,6 +2,7 @@ | |||||||
| namespace nur\cli; | namespace nur\cli; | ||||||
| 
 | 
 | ||||||
| use Exception; | use Exception; | ||||||
|  | use nulib\app; | ||||||
| use nulib\app\RunFile; | use nulib\app\RunFile; | ||||||
| use nulib\ExitError; | use nulib\ExitError; | ||||||
| use nulib\ext\yaml; | use nulib\ext\yaml; | ||||||
| @ -16,7 +17,6 @@ use nur\config\ArrayConfig; | |||||||
| use nur\msg; | use nur\msg; | ||||||
| use nur\os; | use nur\os; | ||||||
| use nur\path; | use nur\path; | ||||||
| use nur\sery\app; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class Application: application de base |  * Class Application: application de base | ||||||
|  | |||||||
| @ -105,7 +105,7 @@ class ref_type { | |||||||
|   /** comme {@link c} mais nullable */ |   /** comme {@link c} mais nullable */ | ||||||
|   const NCONTENT = "?".self::CONTENT; |   const NCONTENT = "?".self::CONTENT; | ||||||
| 
 | 
 | ||||||
|   /** comme {@link \nur\sery\FILE} mais nullable */ |   /** comme {@link FILE} mais nullable */ | ||||||
|   const NFILE = "?".self::FILE; |   const NFILE = "?".self::FILE; | ||||||
| 
 | 
 | ||||||
|   /** comme {@link DATETIME} mais nullable */ |   /** comme {@link DATETIME} mais nullable */ | ||||||
|  | |||||||
| @ -599,16 +599,16 @@ class Form extends ComponentPrintable implements IParametrable, ArrayAccess, Cou | |||||||
|   /** @var ?array */ |   /** @var ?array */ | ||||||
|   protected $hiddenControls; |   protected $hiddenControls; | ||||||
| 
 | 
 | ||||||
|   function addHiddenControl($control, ?string $name=null): self { |   function addHiddenControl($control, ?string $id=null): self { | ||||||
|     A::set($this->hiddenControls, $name, $control); |     A::set($this->hiddenControls, $id, $control); | ||||||
|     return $this; |     return $this; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** @var ?array */ |   /** @var ?array */ | ||||||
|   protected $controls; |   protected $controls; | ||||||
| 
 | 
 | ||||||
|   function addControl($control, ?string $name=null): self { |   function addControl($control, ?string $id=null): self { | ||||||
|     A::set($this->controls, $name, $control); |     A::set($this->controls, $id, $control); | ||||||
|     return $this; |     return $this; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
| @ -660,7 +660,8 @@ class Form extends ComponentPrintable implements IParametrable, ArrayAccess, Cou | |||||||
|       $param["value"] = $value; |       $param["value"] = $value; | ||||||
|       #XXX en attendant le formattage ci-dessus, forcer la format texte pour que
 |       #XXX en attendant le formattage ci-dessus, forcer la format texte pour que
 | ||||||
|       # la comparaison puisse se faire
 |       # la comparaison puisse se faire
 | ||||||
|       $param["checked"] = strval($currentValue) === strval($value); |       #XXX si $name est un tableau e.g values[] le test ci-dessous ne fonctionne pas
 | ||||||
|  |       $param["checked"] ??= strval($currentValue) === strval($value); | ||||||
|       break; |       break; | ||||||
|     case self::NV: |     case self::NV: | ||||||
|       if ($value === null) $value = $this->get($key, $default); |       if ($value === null) $value = $this->get($key, $default); | ||||||
| @ -672,7 +673,7 @@ class Form extends ComponentPrintable implements IParametrable, ArrayAccess, Cou | |||||||
| 
 | 
 | ||||||
|     if ($params === null) $params = $param; |     if ($params === null) $params = $param; | ||||||
|     else A::update_n($params, $param); |     else A::update_n($params, $param); | ||||||
|     return [new $controlClass($this, $params), $name]; |     return [new $controlClass($this, $params), $key]; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   private function _prepareControls(): ?array { |   private function _prepareControls(): ?array { | ||||||
|  | |||||||
| @ -32,6 +32,7 @@ class nb { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   static final function menu($text, ?array $links=null, ?array $options=null): array { |   static final function menu($text, ?array $links=null, ?array $options=null): array { | ||||||
|  |     $links = array_filter($links, function($link) { return $link !== null; }); | ||||||
|     $item = ["item" => "menu", "links" => $links, "value" => $text]; |     $item = ["item" => "menu", "links" => $links, "value" => $text]; | ||||||
|     if ($options !== null) $item = array_merge($item, $options); |     if ($options !== null) $item = array_merge($item, $options); | ||||||
|     return $item; |     return $item; | ||||||
|  | |||||||
| @ -5,6 +5,23 @@ use nur\v\BasePlugin; | |||||||
| use nur\v\v; | use nur\v\v; | ||||||
| use nur\v\vo; | use nur\v\vo; | ||||||
| 
 | 
 | ||||||
|  | /** | ||||||
|  |  * Class showmorePlugin: un outil pour masquer par défaut un panneau de détails | ||||||
|  |  * et donner la possibilité à l'utilisateur de l'afficher | ||||||
|  |  * | ||||||
|  |  * s'utilise de cette façon: | ||||||
|  |  * <pre> | ||||||
|  |  * $sm = new showmorePlugin(); | ||||||
|  |  * // le tout doit être dans le container startc-endc
 | ||||||
|  |  * $sm->printStartc(); | ||||||
|  |  * // l'invite contient un lien pour afficher le panneau caché
 | ||||||
|  |  * $sm->printInvite(); | ||||||
|  |  * // le panneau caché est dans le container startp-endp
 | ||||||
|  |  * $sm->printStartp(); | ||||||
|  |  * $sm->printEndp(); | ||||||
|  |  * $sm->printEndc(); | ||||||
|  |  * </pre> | ||||||
|  |  */ | ||||||
| class showmorePlugin extends BasePlugin { | class showmorePlugin extends BasePlugin { | ||||||
|   const HAVE_JQUERY = true; |   const HAVE_JQUERY = true; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,6 +1,7 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\v\vp; | namespace nur\v\vp; | ||||||
| 
 | 
 | ||||||
|  | use nulib\app; | ||||||
| use nur\authz; | use nur\authz; | ||||||
| use nur\b\authnz\IAuthzUser; | use nur\b\authnz\IAuthzUser; | ||||||
| use nur\config; | use nur\config; | ||||||
| @ -46,6 +47,21 @@ class NavigablePage extends AInitAuthzPage implements INavigablePage { | |||||||
| 
 | 
 | ||||||
|   const MENU_SULOGIN = true; |   const MENU_SULOGIN = true; | ||||||
| 
 | 
 | ||||||
|  |   protected function getAppVersionNbtext(): ?array { | ||||||
|  |     $app = app::get(); | ||||||
|  |     $projdir = $app->getProjdir(); | ||||||
|  |     $versionfile =  "$projdir/VERSION.txt"; | ||||||
|  |     if (file_exists($versionfile)) { | ||||||
|  |       $name = $app->getName(); | ||||||
|  |       $version = file_get_contents($versionfile); | ||||||
|  |       return nb::text([ | ||||||
|  |         "style" => "margin: 0 15px", | ||||||
|  |         "$name v$version" | ||||||
|  |       ]); | ||||||
|  |     } | ||||||
|  |     return null; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   protected function getAuthzNbtext(IAuthzUser $user): array { |   protected function getAuthzNbtext(IAuthzUser $user): array { | ||||||
|     $username = $user->getUsername(); |     $username = $user->getUsername(); | ||||||
|     $role = $user->getRole(); |     $role = $user->getRole(); | ||||||
| @ -95,6 +111,7 @@ class NavigablePage extends AInitAuthzPage implements INavigablePage { | |||||||
|       $user = authz::get(); |       $user = authz::get(); | ||||||
|       navbar::nav(["align" => "right"], [ |       navbar::nav(["align" => "right"], [ | ||||||
|         nb::menu(icon::user($user->getShortName()), [ |         nb::menu(icon::user($user->getShortName()), [ | ||||||
|  |           $this->getAppVersionNbtext(), | ||||||
|           $this->getAuthzNbtext($user), |           $this->getAuthzNbtext($user), | ||||||
|           $this->getLogoutNblink(), |           $this->getLogoutNblink(), | ||||||
|         ]), |         ]), | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ | |||||||
| require(__DIR__.'/../../vendor/autoload.php'); | require(__DIR__.'/../../vendor/autoload.php'); | ||||||
| 
 | 
 | ||||||
| use nur\cli\Application; | use nur\cli\Application; | ||||||
| use nur\sery\output\msg; | use nulib\output\msg; | ||||||
| 
 | 
 | ||||||
| class TestArgs4 extends Application { | class TestArgs4 extends Application { | ||||||
|   protected $query; |   protected $query; | ||||||
|  | |||||||
| @ -1,18 +1,13 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery; | namespace nulib; | ||||||
| 
 | 
 | ||||||
| use nulib\A; | use nulib\app\cli\Application; | ||||||
| use nulib\app\LockFile; | use nulib\app\LockFile; | ||||||
| use nulib\app\RunFile; | use nulib\app\RunFile; | ||||||
| use nulib\cl; |  | ||||||
| use nulib\ExitError; |  | ||||||
| use nulib\os\path; | use nulib\os\path; | ||||||
| use nulib\os\sh; | use nulib\os\sh; | ||||||
| use nulib\php\func; | use nulib\php\func; | ||||||
| use nulib\str; |  | ||||||
| use nulib\ValueException; |  | ||||||
| use nur\cli\Application as nur_Application; | use nur\cli\Application as nur_Application; | ||||||
| use nur\sery\app\cli\Application; |  | ||||||
| 
 | 
 | ||||||
| class app { | class app { | ||||||
|   private static function isa_Application($app): bool { |   private static function isa_Application($app): bool { | ||||||
| @ -1,7 +1,8 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\app\cli; | namespace nulib\app\cli; | ||||||
| 
 | 
 | ||||||
| use Exception; | use Exception; | ||||||
|  | use nulib\app; | ||||||
| use nulib\app\RunFile; | use nulib\app\RunFile; | ||||||
| use nulib\ExitError; | use nulib\ExitError; | ||||||
| use nulib\ext\yaml; | use nulib\ext\yaml; | ||||||
| @ -13,7 +14,6 @@ use nulib\ValueException; | |||||||
| use nur\cli\ArgsException; | use nur\cli\ArgsException; | ||||||
| use nur\cli\ArgsParser; | use nur\cli\ArgsParser; | ||||||
| use nur\config; | use nur\config; | ||||||
| use nur\sery\app; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class Application: application de base |  * Class Application: application de base | ||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\access; | namespace nulib\php\access; | ||||||
| 
 | 
 | ||||||
| use nulib\cl; | use nulib\cl; | ||||||
| 
 | 
 | ||||||
| @ -49,7 +49,10 @@ abstract class AbstractAccess implements IAccess { | |||||||
|   function ensureAssoc(array $keys, ?array $params=null): void { |   function ensureAssoc(array $keys, ?array $params=null): void { | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function ensureKeys(array $defaults, ?array $params=null): void { |   function ensureKeys(array $defaults, ?array $missings, ?array $params=null): void { | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function deleteMissings(array $missings, ?array $params=null): void { | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function ensureOrder(array $keys, ?array $params=null): void { |   function ensureOrder(array $keys, ?array $params=null): void { | ||||||
|  | |||||||
							
								
								
									
										8
									
								
								src/php/access/ArrayAccess.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/php/access/ArrayAccess.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,8 @@ | |||||||
|  | <?php | ||||||
|  | namespace nulib\php\access; | ||||||
|  | 
 | ||||||
|  | class ArrayAccess extends KeyAccess { | ||||||
|  |   const ALLOW_NULL = true; | ||||||
|  |   const ALLOW_FALSE = false; | ||||||
|  |   const PROTECT_DEST = true; | ||||||
|  | } | ||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\access; | namespace nulib\php\access; | ||||||
| 
 | 
 | ||||||
| use nulib\cl; | use nulib\cl; | ||||||
| use nulib\StateException; | use nulib\StateException; | ||||||
| @ -170,7 +170,7 @@ class ChainAccess extends AbstractAccess { | |||||||
|     #$this->access->ensureAssoc($keys, $params);
 |     #$this->access->ensureAssoc($keys, $params);
 | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function ensureKeys(array $defaults, ?array $params=null): void { |   function ensureKeys(array $defaults, ?array $missings, ?array $params=null): void { | ||||||
|     #XXX fonction de $accessType?
 |     #XXX fonction de $accessType?
 | ||||||
|     #$this->access->ensureKeys($defaults, $params);
 |     #$this->access->ensureKeys($defaults, $params);
 | ||||||
|   } |   } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\access; | namespace nulib\php\access; | ||||||
| 
 | 
 | ||||||
| use nulib\cl; | use nulib\cl; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,7 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\access; | namespace nulib\php\access; | ||||||
| 
 |  | ||||||
| use nulib\cl; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class GetAccess: accès à une valeur de $_GET |  * Class GetAccess: accès à une valeur de $_GET | ||||||
|  | |||||||
| @ -1,7 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\access; | namespace nulib\php\access; | ||||||
| 
 |  | ||||||
| use ReflectionClass; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Interface IAccess: abstraction d'un accès complet à une valeur |  * Interface IAccess: abstraction d'un accès complet à une valeur | ||||||
| @ -39,10 +37,16 @@ interface IAccess extends IGetter, ISetter, IDeleter { | |||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
|    * s'assurer que toutes les clés mentionnées dans le tableau $defaults |    * s'assurer que toutes les clés mentionnées dans le tableau $defaults | ||||||
|    * existent. si elles n'existent pas, leur donner la valeur du tableau |    * existent. si elles n'existent pas, ou si elles ont la valeur correspondante | ||||||
|    * $defaults |    * du tableau $missings, leur donner la valeur du tableau $defaults | ||||||
|    */ |    */ | ||||||
|   function ensureKeys(array $defaults, ?array $params=null): void; |   function ensureKeys(array $defaults, ?array $missings, ?array $params=null): void; | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * supprimer toutes les clés dont la valeur est celle mentionnée dans le | ||||||
|  |    * tableau $missings | ||||||
|  |    */ | ||||||
|  |   function deleteMissings(array $missings, ?array $params=null): void; | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
|    * s'assure que les clés de la destination sont dans l'ordre mentionné dans le |    * s'assure que les clés de la destination sont dans l'ordre mentionné dans le | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\access; | namespace nulib\php\access; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class IDeleter: une abstraction d'un objet qui permet de supprimer une valeur |  * Class IDeleter: une abstraction d'un objet qui permet de supprimer une valeur | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\access; | namespace nulib\php\access; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class IGetter: une abstraction d'un objet qui permet d'obtenir une valeur |  * Class IGetter: une abstraction d'un objet qui permet d'obtenir une valeur | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\access; | namespace nulib\php\access; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class ISetter: une abstraction d'un objet qui permet de modifier une valeur |  * Class ISetter: une abstraction d'un objet qui permet de modifier une valeur | ||||||
|  | |||||||
| @ -1,9 +1,8 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\access; | namespace nulib\php\access; | ||||||
| 
 | 
 | ||||||
| use ArrayAccess; | use ArrayAccess; | ||||||
| use nulib\cl; | use nulib\cl; | ||||||
| use nulib\ref\schema\ref_schema; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class KeyAccess: accès |  * Class KeyAccess: accès | ||||||
| @ -142,15 +141,30 @@ class KeyAccess extends AbstractAccess { | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function ensureKeys(array $defaults, ?array $params=null): void { |   function ensureKeys(array $defaults, ?array $missings, ?array $params=null): void { | ||||||
|     $dest =& $this->dest; |     $dest =& $this->dest; | ||||||
|     $keys = array_keys($defaults); |     $keys = array_keys($defaults); | ||||||
|     $prefix = $params["key_prefix"] ?? null; |     $prefix = $params["key_prefix"] ?? null; | ||||||
|     $suffix = $params["key_suffix"] ?? null; |     $suffix = $params["key_suffix"] ?? null; | ||||||
|     foreach ($keys as $key) { |     foreach ($keys as $key) { | ||||||
|       $destKey = "$prefix$key$suffix"; |       $destKey = "$prefix$key$suffix"; | ||||||
|  |       $haveMissing = $missings !== null && array_key_exists($key, $missings); | ||||||
|       if ($dest === null || !array_key_exists($destKey, $dest)) { |       if ($dest === null || !array_key_exists($destKey, $dest)) { | ||||||
|         $dest[$destKey] = $defaults[$key]; |         $dest[$destKey] = $defaults[$key]; | ||||||
|  |       } elseif ($haveMissing && $dest[$destKey] === $missings[$key]) { | ||||||
|  |         $dest[$destKey] = $defaults[$key]; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function deleteMissings(array $missings, ?array $params=null): void { | ||||||
|  |     $dest =& $this->dest; | ||||||
|  |     $prefix = $params["key_prefix"] ?? null; | ||||||
|  |     $suffix = $params["key_suffix"] ?? null; | ||||||
|  |     foreach ($missings as $key => $missing) { | ||||||
|  |       $destKey = "$prefix$key$suffix"; | ||||||
|  |       if (array_key_exists($destKey, $dest) && $dest[$destKey] === $missing) { | ||||||
|  |         unset($dest[$destKey]); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  | |||||||
| @ -1,7 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\access; | namespace nulib\php\access; | ||||||
| 
 |  | ||||||
| use nulib\cl; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class PostAccess: accès à une valeur de $_POST |  * Class PostAccess: accès à une valeur de $_POST | ||||||
|  | |||||||
| @ -1,13 +1,11 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\access; | namespace nulib\php\access; | ||||||
| 
 | 
 | ||||||
| use nulib\StateException; | use nulib\StateException; | ||||||
| use nulib\str; | use nulib\str; | ||||||
| use ReflectionClass; | use ReflectionClass; | ||||||
| use ReflectionException; | use ReflectionException; | ||||||
| use ReflectionNamedType; |  | ||||||
| use ReflectionProperty; | use ReflectionProperty; | ||||||
| use stdClass; |  | ||||||
| 
 | 
 | ||||||
| class PropertyAccess extends AbstractAccess { | class PropertyAccess extends AbstractAccess { | ||||||
|   const PROTECT_DEST = true; |   const PROTECT_DEST = true; | ||||||
| @ -146,7 +144,7 @@ class PropertyAccess extends AbstractAccess { | |||||||
|     return new ChainAccess($this, $key); |     return new ChainAccess($this, $key); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function ensureKeys(array $defaults, ?array $params=null): void { |   function ensureKeys(array $defaults, ?array $missings, ?array $params=null): void { | ||||||
|     $dest = $this->dest; |     $dest = $this->dest; | ||||||
|     if ($dest === null) { |     if ($dest === null) { | ||||||
|       # comme ne connait pas la classe de l'objet destination, on n'essaie pas
 |       # comme ne connait pas la classe de l'objet destination, on n'essaie pas
 | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\access; | namespace nulib\php\access; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class ShadowAccess: accès en lecture depuis une instance de {@link IAccess} |  * Class ShadowAccess: accès en lecture depuis une instance de {@link IAccess} | ||||||
| @ -62,8 +62,8 @@ class ShadowAccess extends AbstractAccess { | |||||||
|     $this->writer->ensureAssoc($keys, $params); |     $this->writer->ensureAssoc($keys, $params); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function ensureKeys(array $defaults, ?array $params=null): void { |   function ensureKeys(array $defaults, ?array $missings, ?array $params=null): void { | ||||||
|     $this->writer->ensureKeys($defaults, $params); |     $this->writer->ensureKeys($defaults, $missings, $params); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function ensureOrder(array $keys, ?array $params=null): void { |   function ensureOrder(array $keys, ?array $params=null): void { | ||||||
|  | |||||||
							
								
								
									
										8
									
								
								src/php/access/ValueAccess.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/php/access/ValueAccess.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,8 @@ | |||||||
|  | <?php | ||||||
|  | namespace nulib\php\access; | ||||||
|  | 
 | ||||||
|  | class ValueAccess extends KeyAccess { | ||||||
|  |   const ALLOW_NULL = false; | ||||||
|  |   const ALLOW_FALSE = true; | ||||||
|  |   const PROTECT_DEST = false; | ||||||
|  | } | ||||||
| @ -1,11 +1,11 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\coll; | namespace nulib\php\coll; | ||||||
| 
 | 
 | ||||||
| use Iterator; | use Iterator; | ||||||
| use IteratorAggregate; | use IteratorAggregate; | ||||||
| use nulib\cl; | use nulib\cl; | ||||||
| use nulib\php\func; | use nulib\php\func; | ||||||
| use nur\sery\wip\php\iter; | use nulib\php\iter; | ||||||
| use Traversable; | use Traversable; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  | |||||||
| @ -1,11 +1,10 @@ | |||||||
| <?php # -*- coding: utf-8 mode: php -*- vim:sw=2:sts=2:et:ai:si:sta:fenc=utf-8
 | <?php # -*- coding: utf-8 mode: php -*- vim:sw=2:sts=2:et:ai:si:sta:fenc=utf-8
 | ||||||
| namespace nur\sery\wip\php; | namespace nulib\php; | ||||||
| 
 | 
 | ||||||
| use Exception; | use Exception; | ||||||
| use Generator; | use Generator; | ||||||
| use Iterator; | use Iterator; | ||||||
| use IteratorAggregate; | use IteratorAggregate; | ||||||
| use nulib\php\ICloseable; |  | ||||||
| use nulib\StopException; | use nulib\StopException; | ||||||
| use nulib\ValueException; | use nulib\ValueException; | ||||||
| use Traversable; | use Traversable; | ||||||
|  | |||||||
							
								
								
									
										67
									
								
								src/schema/ConsolidatedResult.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								src/schema/ConsolidatedResult.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,67 @@ | |||||||
|  | <?php | ||||||
|  | namespace nulib\schema; | ||||||
|  | 
 | ||||||
|  | use nulib\ref\schema\ref_analyze; | ||||||
|  | use nulib\ValueException; | ||||||
|  | 
 | ||||||
|  | class ConsolidatedResult extends Result { | ||||||
|  |   protected int $highestResult; | ||||||
|  | 
 | ||||||
|  |   function reset(): void { | ||||||
|  |     parent::reset(); | ||||||
|  |     $this->highestResult = -1; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   protected function _addMessage(Wrapper $wrapper, $prefix=null): void { | ||||||
|  |     $result = $wrapper->getResult(); | ||||||
|  |     $message = $this->message; | ||||||
|  |     if ($message) $message .= "\n"; | ||||||
|  |     if ($prefix !== null) $message .= "$prefix: "; | ||||||
|  |     $message .= $result->message; | ||||||
|  |     $this->message = $message; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function addMissingMessage(Wrapper $wrapper, $prefix=null): void { | ||||||
|  |     if ($this->highestResult < ref_analyze::MISSING) { | ||||||
|  |       $this->present = false; | ||||||
|  |       $this->available = false; | ||||||
|  |       $this->null = false; | ||||||
|  |       $this->valid = false; | ||||||
|  |       $this->messageKey = "missing"; | ||||||
|  |     } | ||||||
|  |     $this->_addMessage($wrapper, $prefix); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function addUnavailableMessage(Wrapper $wrapper, $prefix=null): void { | ||||||
|  |     if ($this->highestResult < ref_analyze::UNAVAILABLE) { | ||||||
|  |       $this->present = true; | ||||||
|  |       $this->available = false; | ||||||
|  |       $this->null = false; | ||||||
|  |       $this->valid = false; | ||||||
|  |       $this->messageKey = "unavailable"; | ||||||
|  |     } | ||||||
|  |     $this->_addMessage($wrapper, $prefix); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function addNullMessage(Wrapper $wrapper, $prefix=null): void { | ||||||
|  |     if ($this->highestResult < ref_analyze::NULL) { | ||||||
|  |       $this->present = true; | ||||||
|  |       $this->available = true; | ||||||
|  |       $this->null = true; | ||||||
|  |       $this->valid = false; | ||||||
|  |       $this->messageKey = "null"; | ||||||
|  |     } | ||||||
|  |     $this->_addMessage($wrapper, $prefix); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function addInvalidMessage(Wrapper $wrapper, $prefix=null): void { | ||||||
|  |     if ($this->highestResult < ref_analyze::INVALID) { | ||||||
|  |       $this->present = true; | ||||||
|  |       $this->available = true; | ||||||
|  |       $this->null = false; | ||||||
|  |       $this->valid = false; | ||||||
|  |       $this->messageKey = "invalid"; | ||||||
|  |     } | ||||||
|  |     $this->_addMessage($wrapper, $prefix); | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema; | namespace nulib\schema; | ||||||
| 
 | 
 | ||||||
| class OldSchema { | class OldSchema { | ||||||
|   /** |   /** | ||||||
|  | |||||||
| @ -1,7 +1,10 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema; | namespace nulib\schema; | ||||||
| 
 | 
 | ||||||
| use IteratorAggregate; | use nulib\cl; | ||||||
|  | use nulib\ref\schema\ref_analyze; | ||||||
|  | use nulib\ref\schema\ref_schema; | ||||||
|  | use nulib\ValueException; | ||||||
| use Throwable; | use Throwable; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
| @ -22,39 +25,133 @@ use Throwable; | |||||||
|  *  disponible, null sinon. ce champ est utilisé comme optimisation si la valeur |  *  disponible, null sinon. ce champ est utilisé comme optimisation si la valeur | ||||||
|  *  normalisée a déjà été calculée |  *  normalisée a déjà été calculée | ||||||
|  */ |  */ | ||||||
| abstract class Result implements IteratorAggregate { | class Result { | ||||||
|   const KEYS = [ |  | ||||||
|     "resultAvailable", |  | ||||||
|     "present", "available", "null", "valid", "normalized", |  | ||||||
|     "messageKey", "message", "exception", |  | ||||||
|     "origValue", "normalizedValue", |  | ||||||
|   ]; |  | ||||||
| 
 |  | ||||||
|   function __construct() { |   function __construct() { | ||||||
|     $this->reset(); |     $this->reset(); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** |   public bool $resultAvailable; | ||||||
|    * Obtenir la liste des clés valides pour les valeurs accessibles via cet |   public bool $present; | ||||||
|    * objet |   public bool $available; | ||||||
|    */ |   public bool $null; | ||||||
|   abstract function getKeys(): array; |   public bool $valid; | ||||||
| 
 |   public bool $normalized; | ||||||
|   /** |   public ?string $messageKey; | ||||||
|    * sélectionner le résultat associé à la clé spécifiée |   public ?string $message; | ||||||
|    * |   public ?Throwable $exception; | ||||||
|    * @param string|int|null $key |   public $origValue; | ||||||
|    * @return Result $this |   public $normalizedValue; | ||||||
|    */ |  | ||||||
|   abstract function select($key): Result; |  | ||||||
| 
 |  | ||||||
|   function getIterator() { |  | ||||||
|     foreach ($this->getKeys() as $key) { |  | ||||||
|       yield $key => $this->select($key); |  | ||||||
|     } |  | ||||||
|     $this->select(null); |  | ||||||
|   } |  | ||||||
| 
 | 
 | ||||||
|   /** réinitialiser tous les objets résultats accessibles via cet objet */ |   /** réinitialiser tous les objets résultats accessibles via cet objet */ | ||||||
|   abstract function reset(): void; |   function reset(): void { | ||||||
|  |     $this->resultAvailable = false; | ||||||
|  |     $this->present = false; | ||||||
|  |     $this->available = false; | ||||||
|  |     $this->null = false; | ||||||
|  |     $this->valid = false; | ||||||
|  |     $this->normalized = false; | ||||||
|  |     $this->messageKey = null; | ||||||
|  |     $this->message = null; | ||||||
|  |     $this->exception = null; | ||||||
|  |     $this->origValue = null; | ||||||
|  |     $this->normalizedValue = null; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   protected function getMessage(string $key,  Schema $schema): string { | ||||||
|  |     $message = cl::get($schema->messages, $key); | ||||||
|  |     if ($message !== null) return $message; | ||||||
|  |     return cl::get(ref_schema::MESSAGES, $key); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function setMissing( Schema $schema): int { | ||||||
|  |     $this->resultAvailable = true; | ||||||
|  |     $this->present = false; | ||||||
|  |     $this->available = false; | ||||||
|  |     if (!$schema->required) { | ||||||
|  |       $this->null = false; | ||||||
|  |       $this->valid = true; | ||||||
|  |       $this->normalized = true; | ||||||
|  |       return ref_analyze::NORMALIZED; | ||||||
|  |     } else { | ||||||
|  |       $this->messageKey = $messageKey = "missing"; | ||||||
|  |       $this->message = $this->getMessage($messageKey, $schema); | ||||||
|  |       return ref_analyze::MISSING; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function setUnavailable( Schema $schema): int { | ||||||
|  |     $this->resultAvailable = true; | ||||||
|  |     $this->present = true; | ||||||
|  |     $this->available = false; | ||||||
|  |     if (!$schema->required) { | ||||||
|  |       $this->null = false; | ||||||
|  |       $this->valid = true; | ||||||
|  |       $this->normalized = true; | ||||||
|  |       return ref_analyze::NORMALIZED; | ||||||
|  |     } else { | ||||||
|  |       $this->messageKey = $messageKey = "unavailable"; | ||||||
|  |       $this->message = $this->getMessage($messageKey, $schema); | ||||||
|  |       return ref_analyze::UNAVAILABLE; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function setNull( Schema $schema): int { | ||||||
|  |     $this->resultAvailable = true; | ||||||
|  |     $this->present = true; | ||||||
|  |     $this->available = true; | ||||||
|  |     $this->null = true; | ||||||
|  |     if ($schema->nullable) { | ||||||
|  |       $this->valid = true; | ||||||
|  |       $this->normalized = true; | ||||||
|  |       return ref_analyze::NORMALIZED; | ||||||
|  |     } else { | ||||||
|  |       $this->messageKey = $messageKey = "null"; | ||||||
|  |       $this->message = $this->getMessage($messageKey, $schema); | ||||||
|  |       return ref_analyze::NULL; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function setInvalid($value,  Schema $schema, ?Throwable $exception=null): int { | ||||||
|  |     $this->resultAvailable = true; | ||||||
|  |     $this->present = true; | ||||||
|  |     $this->available = true; | ||||||
|  |     $this->null = false; | ||||||
|  |     $this->valid = false; | ||||||
|  |     $this->origValue = $value; | ||||||
|  |     $this->messageKey = $messageKey = "invalid"; | ||||||
|  |     $message = null; | ||||||
|  |     if ($exception !== null) $message = ValueException::get_message($exception); | ||||||
|  |     if (!$message) $message = $this->getMessage($messageKey, $schema); | ||||||
|  |     $this->message = $message; | ||||||
|  |     $this->exception = $exception; | ||||||
|  |     return ref_analyze::INVALID; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function setValid($normalizedValue=null): int { | ||||||
|  |     $this->resultAvailable = true; | ||||||
|  |     $this->present = true; | ||||||
|  |     $this->available = true; | ||||||
|  |     $this->null = false; | ||||||
|  |     $this->valid = true; | ||||||
|  |     $this->normalizedValue = $normalizedValue; | ||||||
|  |     return ref_analyze::VALID; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function setNormalized(): int { | ||||||
|  |     $this->resultAvailable = true; | ||||||
|  |     $this->present = true; | ||||||
|  |     $this->available = true; | ||||||
|  |     $this->null = false; | ||||||
|  |     $this->valid = true; | ||||||
|  |     $this->normalized = true; | ||||||
|  |     return ref_analyze::NORMALIZED; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function throw(bool $throw): void { | ||||||
|  |     if ($throw) { | ||||||
|  |       $exception = $this->exception; | ||||||
|  |       if ($exception !== null) throw $exception; | ||||||
|  |       else throw new ValueException($this->message); | ||||||
|  |     } | ||||||
|  |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,21 +1,21 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema; | namespace nulib\schema; | ||||||
| 
 | 
 | ||||||
| use ArrayAccess; | use ArrayAccess; | ||||||
| use nulib\AccessException; | use nulib\AccessException; | ||||||
| use nulib\cl; | use nulib\cl; | ||||||
| use nulib\ref\schema\ref_schema; | use nulib\ref\schema\ref_schema; | ||||||
| use nulib\ref\schema\ref_types; | use nulib\ref\schema\ref_types; | ||||||
| use nur\sery\wip\schema\_assoc\AssocSchema; | use nulib\schema\_assoc\AssocSchema; | ||||||
| use nur\sery\wip\schema\_list\ListSchema; | use nulib\schema\_list\ListSchema; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarSchema; | use nulib\schema\_scalar\ScalarSchema; | ||||||
| use nur\sery\wip\schema\types\IType; | use nulib\schema\types\IType; | ||||||
| use nur\sery\wip\schema\types\tarray; | use nulib\schema\types\tarray; | ||||||
| use nur\sery\wip\schema\types\tbool; | use nulib\schema\types\tbool; | ||||||
| use nur\sery\wip\schema\types\tcallable; | use nulib\schema\types\tcontent; | ||||||
| use nur\sery\wip\schema\types\tcontent; | use nulib\schema\types\tfunc; | ||||||
| use nur\sery\wip\schema\types\tpkey; | use nulib\schema\types\tpkey; | ||||||
| use nur\sery\wip\schema\types\trawstring; | use nulib\schema\types\trawstring; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class Schema |  * Class Schema | ||||||
| @ -50,7 +50,7 @@ abstract class Schema implements ArrayAccess { | |||||||
|    * l'instance de Schema nouvelle créée |    * l'instance de Schema nouvelle créée | ||||||
|    * - sinon, prendre $definition comme définition |    * - sinon, prendre $definition comme définition | ||||||
|    */ |    */ | ||||||
|   static function ns(&$schema, $definition=null, $definitionKey=null, bool $normalize=true): self { |   static function ns($definition=null, $definitionKey=null, &$schema=null, bool $normalize=true): self { | ||||||
|     if (is_array($schema)) { |     if (is_array($schema)) { | ||||||
|       $definition = $schema; |       $definition = $schema; | ||||||
|       $schema = null; |       $schema = null; | ||||||
| @ -74,13 +74,13 @@ abstract class Schema implements ArrayAccess { | |||||||
|    * variable $value (si $valueKey===null) ou $value[$valueKey] si $valueKey |    * variable $value (si $valueKey===null) ou $value[$valueKey] si $valueKey | ||||||
|    * n'est pas null |    * n'est pas null | ||||||
|    */ |    */ | ||||||
|   static function nw(&$value=null, $valueKey=null, &$schema=null, $definition=null, ?Wrapper &$wrapper=null): Wrapper { |   static function nw(&$value=null, $valueKey=null, $definition=null, &$schema=null, ?Wrapper &$wrapper=null): Wrapper { | ||||||
|     if ($definition === null) { |     if ($definition === null) { | ||||||
|       # bien que techniquement, $definition peut être null (il s'agit alors du
 |       # bien que techniquement, $definition peut être null (il s'agit alors du
 | ||||||
|       # schéma d'un scalaire quelconque), on ne l'autorise pas ici
 |       # schéma d'un scalaire quelconque), on ne l'autorise pas ici
 | ||||||
|       throw SchemaException::invalid_schema("definition is required"); |       throw SchemaException::invalid_schema("definition is required"); | ||||||
|     } |     } | ||||||
|     return self::ns($schema, $definition)->getWrapper($value, $valueKey, null, $wrapper); |     return self::ns($definition, null, $schema)->getWrapper($value, $valueKey, null, $wrapper); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   protected static function have_nature(array $definition, ?string &$nature=null): bool { |   protected static function have_nature(array $definition, ?string &$nature=null): bool { | ||||||
| @ -187,12 +187,12 @@ abstract class Schema implements ArrayAccess { | |||||||
|     tbool::ensure_bool($definition["required"]); |     tbool::ensure_bool($definition["required"]); | ||||||
|     tbool::ensure_bool($definition["nullable"]); |     tbool::ensure_bool($definition["nullable"]); | ||||||
|     tcontent::ensure_ncontent($definition["desc"]); |     tcontent::ensure_ncontent($definition["desc"]); | ||||||
|     tcallable::ensure_ncallable($definition["analyzer_func"]); |     tfunc::ensure_nfunc($definition["analyzer_func"]); | ||||||
|     tcallable::ensure_ncallable($definition["extractor_func"]); |     tfunc::ensure_nfunc($definition["extractor_func"]); | ||||||
|     tcallable::ensure_ncallable($definition["parser_func"]); |     tfunc::ensure_nfunc($definition["parser_func"]); | ||||||
|     tcallable::ensure_ncallable($definition["normalizer_func"]); |     tfunc::ensure_nfunc($definition["normalizer_func"]); | ||||||
|     tarray::ensure_narray($definition["messages"]); |     tarray::ensure_narray($definition["messages"]); | ||||||
|     tcallable::ensure_ncallable($definition["formatter_func"]); |     tfunc::ensure_nfunc($definition["formatter_func"]); | ||||||
|     tbool::ensure_nbool($definition["computed"]); |     tbool::ensure_nbool($definition["computed"]); | ||||||
| 
 | 
 | ||||||
|     switch ($nature[0] ?? null) { |     switch ($nature[0] ?? null) { | ||||||
| @ -252,11 +252,11 @@ abstract class Schema implements ArrayAccess { | |||||||
|     case "assoc": |     case "assoc": | ||||||
|       foreach ($definition["schema"] as &$keydef) { |       foreach ($definition["schema"] as &$keydef) { | ||||||
|         self::_ensure_schema_instances($keydef); |         self::_ensure_schema_instances($keydef); | ||||||
|         Schema::ns($keydef, null, null, false); |         Schema::ns(null, null, $keydef, false); | ||||||
|       }; unset($keydef); |       }; unset($keydef); | ||||||
|       break; |       break; | ||||||
|     case "list": |     case "list": | ||||||
|       Schema::ns($definition["schema"], null, null, false); |       Schema::ns(null, null, $definition["schema"], false); | ||||||
|       break; |       break; | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| @ -280,14 +280,7 @@ abstract class Schema implements ArrayAccess { | |||||||
|    */ |    */ | ||||||
|   abstract function getKeys(): array; |   abstract function getKeys(): array; | ||||||
| 
 | 
 | ||||||
|   abstract function getSchema($key): Schema; |   abstract function getSchema($key=false): Schema; | ||||||
| 
 |  | ||||||
|   /** retourner true si le schéma est de nature tableau associatif */ |  | ||||||
|   function isAssoc(?AssocSchema &$schema=null): bool { return false; } |  | ||||||
|   /** retourner true si le schéma est de nature liste */ |  | ||||||
|   function isList(?ListSchema &$schema=null): bool { return false; } |  | ||||||
|   /** retourner true si le schéma est de nature scalaire */ |  | ||||||
|   function isScalar(?ScalarSchema &$schema=null): bool { return false; } |  | ||||||
| 
 | 
 | ||||||
|   abstract protected function newWrapper(): Wrapper; |   abstract protected function newWrapper(): Wrapper; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema; | namespace nulib\schema; | ||||||
| 
 | 
 | ||||||
| use Exception; | use Exception; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,15 +1,11 @@ | |||||||
| # nulib\schema | # nulib\schema | ||||||
| 
 | 
 | ||||||
| * ensureKeys() et orderKeys() se fait au niveau de access (ou input?) |  | ||||||
|   * access/input ne pouvant pas connaître les valeurs appropriées, c'est le |  | ||||||
|     schéma qui les génère. ensureKeys($values) |  | ||||||
|   * méthode ensureAssoc() transforme les clés séquentielles en clés associatives |  | ||||||
| * l'ordre est `ensureAssoc [--> ensureKeys] [--> orderKeys]` |  | ||||||
| 
 |  | ||||||
| * rajouter l'attribut "size" pour spécifier la taille maximale des valeurs | * rajouter l'attribut "size" pour spécifier la taille maximale des valeurs | ||||||
|   * cela pourrait servir pour générer automatiquement des tables SQL |   * cela pourrait servir pour générer automatiquement des tables SQL | ||||||
|   * ou pour modéliser un schéma FSV |   * ou pour modéliser un schéma FSV | ||||||
| 
 | 
 | ||||||
|  | * support allowed_values | ||||||
|  | 
 | ||||||
| * valeurs composite/computed | * valeurs composite/computed | ||||||
|   * analyse / vérification de la valeur complète après calcul du résultat, si |   * analyse / vérification de la valeur complète après calcul du résultat, si | ||||||
|     tous les résultats sont bons |     tous les résultats sont bons | ||||||
| @ -18,7 +14,8 @@ | |||||||
|   * fonction getter_func, setter_func, deleter_func pour les propriétés de type |   * fonction getter_func, setter_func, deleter_func pour les propriétés de type | ||||||
|     computed |     computed | ||||||
| * tdate et tdatetime. qu'en est-il des autres classes (delay, etc.) | * tdate et tdatetime. qu'en est-il des autres classes (delay, etc.) | ||||||
|   * possibilité de spécifier le format de la date à analyser  |   * parse_format pour spécifier le format d'analyse au lieu de l'auto-détecter | ||||||
|  | 
 | ||||||
| * ScalarSchema::from_property() | * ScalarSchema::from_property() | ||||||
| 
 | 
 | ||||||
| * l'argument $format de AssocWrapper::format() est un tableau associatif | * l'argument $format de AssocWrapper::format() est un tableau associatif | ||||||
|  | |||||||
| @ -1,21 +1,14 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema; | namespace nulib\schema; | ||||||
| 
 | 
 | ||||||
| use ArrayAccess; | use ArrayAccess; | ||||||
| use IteratorAggregate; | use IteratorAggregate; | ||||||
| use nulib\php\func; | use nulib\php\func; | ||||||
| use nur\sery\wip\schema\_assoc\AssocWrapper; | use nulib\schema\input\Input; | ||||||
| use nur\sery\wip\schema\_list\ListWrapper; | use nulib\schema\types\IType; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarResult; | use nulib\ValueException; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarWrapper; |  | ||||||
| use nur\sery\wip\schema\input\Input; |  | ||||||
| use nur\sery\wip\schema\types\IType; |  | ||||||
| 
 | 
 | ||||||
| abstract class Wrapper implements ArrayAccess, IteratorAggregate { | abstract class Wrapper implements ArrayAccess, IteratorAggregate { | ||||||
|   function isAssoc(?AssocWrapper &$wrapper=null): bool { return false; } |  | ||||||
|   function isList(?ListWrapper &$wrapper=null): bool { return false; } |  | ||||||
|   function isScalar(?ScalarWrapper &$wrapper=null): bool { return false; } |  | ||||||
| 
 |  | ||||||
|   protected WrapperContext $context; |   protected WrapperContext $context; | ||||||
| 
 | 
 | ||||||
|   /** changer les paramètres de gestion des valeurs */ |   /** changer les paramètres de gestion des valeurs */ | ||||||
| @ -23,24 +16,25 @@ abstract class Wrapper implements ArrayAccess, IteratorAggregate { | |||||||
|     $this->context->resetParams($params); |     $this->context->resetParams($params); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   protected function resetContext($resetSelectedKey): void { |   protected function resetContext(bool $resetSelectedKey): void { | ||||||
|     $context = $this->context; |     $context = $this->context; | ||||||
|     $context->type = null; |     $type = $context->schema->type; | ||||||
|  |     if (is_array($type)) $type = $type[0]; | ||||||
|  |     if (is_string($type)) $type = types::get($context->schema->nullable, $type); | ||||||
|  |     $context->type = $type; | ||||||
|     $context->result->reset(); |     $context->result->reset(); | ||||||
|     $context->analyzed = false; |     $context->analyzed = false; | ||||||
|     $context->normalized = false; |     $context->normalized = false; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   protected function afterModify(?array $params, $resetSelectedKey=false): void { |   protected function afterModify(?array $params, bool $resetSelectedKey=false): void { | ||||||
|     $context = $this->context; |     $context = $this->context; | ||||||
|     $this->resetContext($resetSelectedKey); |     $this->resetContext($resetSelectedKey); | ||||||
|     if ($params["analyze"] ?? $context->analyze) { |     if ($params["analyze"] ?? $context->analyze) { | ||||||
|       $this->analyze($params); |       $this->analyze($params); | ||||||
|     } |     } | ||||||
|     if ($context->analyzed) { |     if ($context->analyzed && ($params["normalize"] ?? $context->normalize)) { | ||||||
|       if ($params["normalize"] ?? $context->normalize) { |       $this->normalize($params); | ||||||
|         $this->normalize($params); |  | ||||||
|       } |  | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
| @ -80,6 +74,10 @@ abstract class Wrapper implements ArrayAccess, IteratorAggregate { | |||||||
|   /** normaliser la valeur */ |   /** normaliser la valeur */ | ||||||
|   abstract static function _normalize(WrapperContext $context, Wrapper $wrapper, ?array $params): bool; |   abstract static function _normalize(WrapperContext $context, Wrapper $wrapper, ?array $params): bool; | ||||||
| 
 | 
 | ||||||
|  |   protected function getConsolidatedResult(): Result { | ||||||
|  |     return $this->context->result; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   function normalize(?array $params=null): bool { |   function normalize(?array $params=null): bool { | ||||||
|     $context = $this->context; |     $context = $this->context; | ||||||
| 
 | 
 | ||||||
| @ -95,11 +93,11 @@ abstract class Wrapper implements ArrayAccess, IteratorAggregate { | |||||||
|       $modified = false; |       $modified = false; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** @var ScalarResult $result */ |     $result = $this->getConsolidatedResult(); | ||||||
|     $result = $context->result; |  | ||||||
|     if (!$result->valid) { |     if (!$result->valid) { | ||||||
|       $result->throw($params["throw"] ?? $context->throw); |       $result->throw($params["throw"] ?? $context->throw); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|     return $modified; |     return $modified; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
| @ -112,16 +110,22 @@ abstract class Wrapper implements ArrayAccess, IteratorAggregate { | |||||||
|   /** |   /** | ||||||
|    * sélectionner le wrapper associé à la clé spécifiée |    * sélectionner le wrapper associé à la clé spécifiée | ||||||
|    * |    * | ||||||
|  |    * $key peut valoir: | ||||||
|  |    * - false pour la clé courante (ne pas changer la sélection) | ||||||
|  |    * - null ou "" le wrapper de la valeur principale | ||||||
|  |    * - ou toute autre valeur présente dans {@link getKeys()} pour les valeurs | ||||||
|  |    * accessible via cet objet | ||||||
|  |    * | ||||||
|    * @param string|int|null $key |    * @param string|int|null $key | ||||||
|    * @return Wrapper $this |    * @return Wrapper $this | ||||||
|    */ |    */ | ||||||
|   abstract function select($key): Wrapper; |   abstract function select($key=null): Wrapper; | ||||||
| 
 | 
 | ||||||
|   function getIterator() { |   function getIterator() { | ||||||
|     foreach ($this->getKeys() as $key) { |     foreach ($this->getKeys() as $key) { | ||||||
|       yield $key => $this->select($key); |       yield $key => $this->select($key); | ||||||
|     } |     } | ||||||
|     $this->select(null); |     $this->select(); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
| @ -129,9 +133,18 @@ abstract class Wrapper implements ArrayAccess, IteratorAggregate { | |||||||
|    * |    * | ||||||
|    * cette fonction doit être appelée après {@link set()} ou {@link unset()} et |    * cette fonction doit être appelée après {@link set()} ou {@link unset()} et | ||||||
|    * après que le wrapper aie été sélectionné avec {@link select()} |    * après que le wrapper aie été sélectionné avec {@link select()} | ||||||
|  |    * | ||||||
|  |    * $key peut valoir: | ||||||
|  |    * - false pour la clé sélectionnée avec {@link select()} | ||||||
|  |    * - null pour le résultat consolidé | ||||||
|  |    * - "" pour le résultat de l'analyse de la valeur principale | ||||||
|  |    * - ou toute autre valeur présente dans {@link getKeys()} pour le résultat | ||||||
|  |    * de l'analyse des valeurs correspondantes | ||||||
|    */ |    */ | ||||||
|   function getResult($key=false): Result { |   function getResult($key=false): Result { | ||||||
|     return $this->context->result; |     if ($key === false || $key === "") return $this->context->result; | ||||||
|  |     if ($key === null) return $this->getConsolidatedResult(); | ||||||
|  |     throw ValueException::invalid_key($key); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** retourner true si la valeur existe */ |   /** retourner true si la valeur existe */ | ||||||
| @ -149,6 +162,11 @@ abstract class Wrapper implements ArrayAccess, IteratorAggregate { | |||||||
|     return $this->getResult($key)->available; |     return $this->getResult($key)->available; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   /** retourner true si la valeur est nulle */ | ||||||
|  |   function isNull($key=false): bool { | ||||||
|  |     return $this->getResult($key)->null; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   /** retourner true si la valeur est valide */ |   /** retourner true si la valeur est valide */ | ||||||
|   function isValid($key=false): bool { |   function isValid($key=false): bool { | ||||||
|     return $this->getResult($key)->valid; |     return $this->getResult($key)->valid; | ||||||
|  | |||||||
| @ -1,13 +1,11 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema; | namespace nulib\schema; | ||||||
| 
 | 
 | ||||||
| use nur\sery\wip\schema\input\Input; | use nulib\ref\schema\ref_schema; | ||||||
| use nur\sery\wip\schema\types\IType; | use nulib\schema\input\Input; | ||||||
|  | use nulib\schema\types\IType; | ||||||
| 
 | 
 | ||||||
| class WrapperContext { | class WrapperContext { | ||||||
|   const DEFAULT_ANALYZE = true; |  | ||||||
|   const DEFAULT_NORMALIZE = true; |  | ||||||
|   const DEFAULT_THROW = true; |  | ||||||
| 
 | 
 | ||||||
|   function __construct(Schema $schema, ?Input $input, $valueKey, ?array $params) { |   function __construct(Schema $schema, ?Input $input, $valueKey, ?array $params) { | ||||||
|     $this->resetParams($params); |     $this->resetParams($params); | ||||||
| @ -23,9 +21,9 @@ class WrapperContext { | |||||||
| 
 | 
 | ||||||
|   function resetParams(?array $params): void { |   function resetParams(?array $params): void { | ||||||
|     $this->params = $params; |     $this->params = $params; | ||||||
|     $this->analyze = $params["analyze"] ?? self::DEFAULT_ANALYZE; |     $this->analyze = $params["analyze"] ?? ref_schema::PARAMS_SCHEMA["analyze"][1]; | ||||||
|     $this->normalize = $params["normalize"] ?? self::DEFAULT_NORMALIZE; |     $this->normalize = $params["normalize"] ?? ref_schema::PARAMS_SCHEMA["normalize"][1]; | ||||||
|     $this->throw = $params["throw"] ?? self::DEFAULT_THROW; |     $this->throw = $params["throw"] ?? ref_schema::PARAMS_SCHEMA["throw"][1]; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** schéma de la valeur */ |   /** schéma de la valeur */ | ||||||
| @ -39,8 +37,6 @@ class WrapperContext { | |||||||
|   /** @var mixed */ |   /** @var mixed */ | ||||||
|   public $value = null; |   public $value = null; | ||||||
| 
 | 
 | ||||||
|   /** @var string|int|null clé sélectionnée */ |  | ||||||
|   public $selectedKey = null; |  | ||||||
|   /** type de la valeur de la clé sélectionnée après analyse */ |   /** type de la valeur de la clé sélectionnée après analyse */ | ||||||
|   public ?IType $type = null; |   public ?IType $type = null; | ||||||
|   /** résultat de l'analyse de la valeur de la clé sélectionnée */ |   /** résultat de l'analyse de la valeur de la clé sélectionnée */ | ||||||
|  | |||||||
| @ -1,11 +1,11 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\_assoc; | namespace nulib\schema\_assoc; | ||||||
| 
 | 
 | ||||||
| use nulib\cl; | use nulib\cl; | ||||||
| use nulib\ref\schema\ref_schema; | use nulib\ref\schema\ref_schema; | ||||||
|  | use nulib\schema\Schema; | ||||||
|  | use nulib\schema\Wrapper; | ||||||
| use nulib\ValueException; | use nulib\ValueException; | ||||||
| use nur\sery\wip\schema\Schema; |  | ||||||
| use nur\sery\wip\schema\Wrapper; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class AssocSchema |  * Class AssocSchema | ||||||
| @ -47,6 +47,10 @@ class AssocSchema extends Schema { | |||||||
|       $this->_definition = $definition; |       $this->_definition = $definition; | ||||||
|       self::_ensure_type($definition); |       self::_ensure_type($definition); | ||||||
|       self::_ensure_schema_instances($definition); |       self::_ensure_schema_instances($definition); | ||||||
|  |     } else { | ||||||
|  |       # ici, $definition contient un schema déjà instancié, mais c'est le mieux
 | ||||||
|  |       # qu'on puisse faire
 | ||||||
|  |       $this->_definition = $definition; | ||||||
|     } |     } | ||||||
|     $this->definition = $definition; |     $this->definition = $definition; | ||||||
|     $keys = []; |     $keys = []; | ||||||
| @ -56,19 +60,14 @@ class AssocSchema extends Schema { | |||||||
|     $this->keys = $keys; |     $this->keys = $keys; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function isAssoc(?AssocSchema &$schema=null): bool { |  | ||||||
|     $schema = $this; |  | ||||||
|     return true; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   protected array $keys; |   protected array $keys; | ||||||
| 
 | 
 | ||||||
|   function getKeys(): array { |   function getKeys(): array { | ||||||
|     return $this->keys; |     return $this->keys; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function getSchema($key): Schema { |   function getSchema($key=false): Schema { | ||||||
|     if ($key === null) return $this; |     if ($key === null || $key === false) return $this; | ||||||
|     $schema = $this->definition["schema"][$key] ?? null; |     $schema = $this->definition["schema"][$key] ?? null; | ||||||
|     if ($schema === null) throw ValueException::invalid_key($key); |     if ($schema === null) throw ValueException::invalid_key($key); | ||||||
|     return $schema; |     return $schema; | ||||||
| @ -83,7 +82,15 @@ class AssocSchema extends Schema { | |||||||
|     # cf le code similaire dans AssocWrapper::__construct()
 |     # cf le code similaire dans AssocWrapper::__construct()
 | ||||||
|     $dontAnalyze = $value === null && $wrapper === null; |     $dontAnalyze = $value === null && $wrapper === null; | ||||||
|     if (!($wrapper instanceof AssocWrapper)) $wrapper = $this->newWrapper(); |     if (!($wrapper instanceof AssocWrapper)) $wrapper = $this->newWrapper(); | ||||||
|  | 
 | ||||||
|  |     # la nature du schéma peut contenir des paramètres par défaut
 | ||||||
|  |     $nature = $this->definition[""]; | ||||||
|  |     foreach (array_keys(ref_schema::ASSOC_PARAMS_SCHEMA) as $paramKey) { | ||||||
|  |       $paramValue = $nature[$paramKey] ?? null; | ||||||
|  |       if ($paramValue !== null) $params[$paramKey] = $paramValue; | ||||||
|  |     } | ||||||
|     if ($params !== null) $wrapper->resetParams($params); |     if ($params !== null) $wrapper->resetParams($params); | ||||||
|  | 
 | ||||||
|     return $wrapper->reset($value, $valueKey, $dontAnalyze? ["analyze" => false]: null); |     return $wrapper->reset($value, $valueKey, $dontAnalyze? ["analyze" => false]: null); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,53 +1,54 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\_assoc; | namespace nulib\schema\_assoc; | ||||||
| 
 | 
 | ||||||
|  | use nulib\cl; | ||||||
| use nulib\ref\schema\ref_analyze; | use nulib\ref\schema\ref_analyze; | ||||||
|  | use nulib\schema\_scalar\ScalarWrapper; | ||||||
|  | use nulib\schema\ConsolidatedResult; | ||||||
|  | use nulib\schema\input\Input; | ||||||
|  | use nulib\schema\Result; | ||||||
|  | use nulib\schema\types\IType; | ||||||
|  | use nulib\schema\Wrapper; | ||||||
|  | use nulib\schema\WrapperContext; | ||||||
| use nulib\ValueException; | use nulib\ValueException; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarResult; |  | ||||||
| use nur\sery\wip\schema\_scalar\ScalarWrapper; |  | ||||||
| use nur\sery\wip\schema\input\Input; |  | ||||||
| use nur\sery\wip\schema\Result; |  | ||||||
| use nur\sery\wip\schema\types\IType; |  | ||||||
| use nur\sery\wip\schema\Wrapper; |  | ||||||
| use nur\sery\wip\schema\WrapperContext; |  | ||||||
| 
 | 
 | ||||||
| class AssocWrapper extends Wrapper { | class AssocWrapper extends Wrapper { | ||||||
|   function __construct(AssocSchema $schema, &$value=null, $valueKey=null, ?array $params=null) { |   function __construct(AssocSchema $schema, &$value=null, $valueKey=null, ?array $params=null) { | ||||||
|  |     $this->context = $context = new AssocWrapperContext($schema, null, null, $params); | ||||||
|  |     $paramsNoThrow = cl::merge($params, [ | ||||||
|  |       "throw" => false, | ||||||
|  |     ]); | ||||||
|  | 
 | ||||||
|     $keys = $schema->getKeys(); |     $keys = $schema->getKeys(); | ||||||
|     $keyWrappers = []; |     $keyWrappers = []; | ||||||
|     foreach ($keys as $key) { |     foreach ($keys as $key) { | ||||||
|       $keyWrappers[$key] = $schema->getSchema($key)->getWrapper(); |       $keyDummy = null; | ||||||
|  |       $keyWrappers[$key] = $schema->getSchema($key)->getWrapper($keyDummy, null, $paramsNoThrow); | ||||||
|     } |     } | ||||||
|     $this->context = $context = new AssocWrapperContext($schema, null, null, $params); |  | ||||||
|     $context->arrayWrapper = new ScalarWrapper($schema, $dummy, null, null, $context); |  | ||||||
|     $context->keys = $keys; |     $context->keys = $keys; | ||||||
|     $context->keyWrappers = $keyWrappers; |     $context->keyWrappers = $keyWrappers; | ||||||
| 
 | 
 | ||||||
|     # calculer manuellemet throw ici parce que WrapperContext le met à true par
 |     $arrayDummy = null; | ||||||
|     # défaut. on veut pouvoir mettre temporairement throw à false si jamais il
 |     $context->arrayWrapper = new ScalarWrapper($schema, $arrayDummy, null, $paramsNoThrow, $context); | ||||||
|     # n'est pas spécifié par l'utilisateur
 |  | ||||||
|     $throw = $params["throw"] ?? null; |  | ||||||
|     # Si $value est null, ne pas lancer d'exception, parce qu'on considère que
 |  | ||||||
|     # c'est une initialisation sans conséquences
 |  | ||||||
|     if ($throw === null && $value !== null) $throw = true; |  | ||||||
|     $context->throw = $throw ?? false; |  | ||||||
|     $this->reset($value, $valueKey); |  | ||||||
|     $context->throw = $throw ?? true; |  | ||||||
|   } |  | ||||||
| 
 | 
 | ||||||
|   function isAssoc(?AssocWrapper &$wrapper=null): bool { $wrapper = $this; return true; } |     $context->consolidatedResult = new ConsolidatedResult(); | ||||||
|  | 
 | ||||||
|  |     if ($value !== null) { | ||||||
|  |       # n'initialiser que si $value n'est pas null
 | ||||||
|  |       $this->reset($value, $valueKey); | ||||||
|  |     } | ||||||
|  |   } | ||||||
| 
 | 
 | ||||||
|   /** @var AssocWrapperContext  */ |   /** @var AssocWrapperContext  */ | ||||||
|   protected WrapperContext $context; |   protected WrapperContext $context; | ||||||
| 
 | 
 | ||||||
|   protected function resetContext($resetSelectedKey): void { |   protected function resetContext(bool $resetSelectedKey): void { | ||||||
|  |     parent::resetContext($resetSelectedKey); | ||||||
|     $context = $this->context; |     $context = $this->context; | ||||||
|     $context->arrayWrapper->getResult()->reset(); |     $context->arrayWrapper->getResult()->reset(); | ||||||
|     foreach ($context->keyWrappers as $wrapper) { |     foreach ($context->keyWrappers as $wrapper) { | ||||||
|       $wrapper->getResult()->reset(); |       $wrapper->getResult()->reset(); | ||||||
|     } |     } | ||||||
|     $context->analyzed = false; |  | ||||||
|     $context->normalized = false; |  | ||||||
|     if ($resetSelectedKey) $context->selectedKey = null; |     if ($resetSelectedKey) $context->selectedKey = null; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
| @ -70,7 +71,8 @@ class AssocWrapper extends Wrapper { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   protected function _getWrapper($key): Wrapper { |   protected function _getWrapper($key): Wrapper { | ||||||
|     if ($key === null) return $this->context->arrayWrapper; |     $context = $this->context; | ||||||
|  |     if ($key === null || $key === "") return $context->arrayWrapper; | ||||||
|     $wrapper = $context->keyWrappers[$key] ?? null; |     $wrapper = $context->keyWrappers[$key] ?? null; | ||||||
|     if ($wrapper === null) throw ValueException::invalid_key($key); |     if ($wrapper === null) throw ValueException::invalid_key($key); | ||||||
|     return $wrapper; |     return $wrapper; | ||||||
| @ -88,22 +90,40 @@ class AssocWrapper extends Wrapper { | |||||||
|    * @param AssocWrapper $wrapper |    * @param AssocWrapper $wrapper | ||||||
|    */ |    */ | ||||||
|   static function _analyze(WrapperContext $context, Wrapper $wrapper, ?array $params): int { |   static function _analyze(WrapperContext $context, Wrapper $wrapper, ?array $params): int { | ||||||
|     if ($context->ensureArray) { |     if ($params["ensure_array"] ?? $context->ensureArray) { | ||||||
|       $valueKey = $context->valueKey; |       $valueKey = $context->valueKey; | ||||||
|       $array = $context->input->get($valueKey); |       $array = $context->input->get($valueKey); | ||||||
|       if ($array === null) $context->input->set([], $valueKey); |       if ($array === null) $context->input->set([], $valueKey); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     if ($params["ensure_assoc"] ?? $context->ensureAssoc) { | ||||||
|  |       $context->input->ensureAssoc($context->schema->getKeys()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     $what = ScalarWrapper::_analyze($context, $wrapper, $params); |     $what = ScalarWrapper::_analyze($context, $wrapper, $params); | ||||||
|     /** @var ScalarResult $result */ |     if (!$context->result->valid) return $what; | ||||||
|     $result = $context->result; | 
 | ||||||
|     if (!$result->valid) return $what; |     $result = $context->consolidatedResult; | ||||||
|     foreach ($context->keyWrappers as $keyWrapper) { |     $result->setValid(); | ||||||
|  |     foreach ($context->keyWrappers as $key => $keyWrapper) { | ||||||
|       $keyWrapper->analyze($params); |       $keyWrapper->analyze($params); | ||||||
|       if (!$keyWrapper->isValid()) { |       if ($keyWrapper->isValid()) continue; | ||||||
|         $what = ref_analyze::INVALID; |       $what = ref_analyze::INVALID; | ||||||
|         $result->addInvalidMessage($keyWrapper); |       #XXX pour $prefix, utiliser si possible la description ou une autre valeur
 | ||||||
|  |       # "user-friendly". possibilité de sélectionner la valeur à utiliser avec
 | ||||||
|  |       # $params?
 | ||||||
|  |       $prefix = $key; | ||||||
|  |       if (!$keyWrapper->isPresent()) { | ||||||
|  |         $result->addMissingMessage($keyWrapper, $prefix); | ||||||
|  |       } elseif (!$keyWrapper->isAvailable()) { | ||||||
|  |         $result->addUnavailableMessage($keyWrapper, $prefix); | ||||||
|  |       } elseif ($keyWrapper->isNull()) { | ||||||
|  |         $result->addNullMessage($keyWrapper, $prefix); | ||||||
|  |       } else { | ||||||
|  |         $result->addInvalidMessage($keyWrapper, $prefix); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|     return $what; |     return $what; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
| @ -112,6 +132,29 @@ class AssocWrapper extends Wrapper { | |||||||
|    * @param AssocWrapper $wrapper |    * @param AssocWrapper $wrapper | ||||||
|    */ |    */ | ||||||
|   static function _normalize(WrapperContext $context, Wrapper $wrapper, ?array $params): bool { |   static function _normalize(WrapperContext $context, Wrapper $wrapper, ?array $params): bool { | ||||||
|  |     $schema = $context->schema; | ||||||
|  |     $keys = $schema->getKeys(); | ||||||
|  | 
 | ||||||
|  |     $defaults = []; | ||||||
|  |     $missings = null; | ||||||
|  |     foreach ($keys as $key) { | ||||||
|  |       $type = $wrapper->getType($key); | ||||||
|  |       $default = $schema->getSchema($key)->default; | ||||||
|  |       if ($default === null) $default = $type->getNullValue(); | ||||||
|  |       $defaults[$key] = $default; | ||||||
|  |       $missing = $type->getMissingValue($valid); | ||||||
|  |       if ($valid) $missings[$key] = $missing; | ||||||
|  |     } | ||||||
|  |     if ($params["ensure_keys"] ?? $context->ensureKeys) { | ||||||
|  |       $context->input->ensureKeys($defaults, $missings, $params); | ||||||
|  |     } else { | ||||||
|  |       $context->input->deleteMissings($missings, $params); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     if ($params["ensure_order"] ?? $context->ensureOrder) { | ||||||
|  |       $context->input->ensureOrder($keys, $params); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     $modified = ScalarWrapper::_normalize($context, $wrapper, $params); |     $modified = ScalarWrapper::_normalize($context, $wrapper, $params); | ||||||
|     foreach ($context->keyWrappers as $keyWrapper) { |     foreach ($context->keyWrappers as $keyWrapper) { | ||||||
|       if ($keyWrapper->normalize($params)) $modified = true; |       if ($keyWrapper->normalize($params)) $modified = true; | ||||||
| @ -119,7 +162,12 @@ class AssocWrapper extends Wrapper { | |||||||
|     return $modified; |     return $modified; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   protected function getConsolidatedResult(): Result { | ||||||
|  |     return $this->context->consolidatedResult; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   function getResult($key=false): Result { |   function getResult($key=false): Result { | ||||||
|  |     if ($key === null) return $this->getConsolidatedResult(); | ||||||
|     if ($key === false) $key = $this->context->selectedKey; |     if ($key === false) $key = $this->context->selectedKey; | ||||||
|     return $this->_getWrapper($key)->getResult(); |     return $this->_getWrapper($key)->getResult(); | ||||||
|   } |   } | ||||||
|  | |||||||
| @ -1,28 +1,27 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\_assoc; | namespace nulib\schema\_assoc; | ||||||
| 
 | 
 | ||||||
| use nur\sery\wip\schema\_scalar\ScalarWrapper; | use nulib\ref\schema\ref_schema; | ||||||
| use nur\sery\wip\schema\input\Input; | use nulib\schema\_scalar\ScalarWrapper; | ||||||
| use nur\sery\wip\schema\Schema; | use nulib\schema\ConsolidatedResult; | ||||||
| use nur\sery\wip\schema\Wrapper; | use nulib\schema\Wrapper; | ||||||
| use nur\sery\wip\schema\WrapperContext; | use nulib\schema\WrapperContext; | ||||||
| 
 | 
 | ||||||
| class AssocWrapperContext extends WrapperContext { | class AssocWrapperContext extends WrapperContext { | ||||||
|   const DEFAULT_ENSURE_ARRAY = false; |  | ||||||
|   const DEFAULT_ENSURE_KEYS = true; |  | ||||||
|   const DEFAULT_ENSURE_ORDER = true; |  | ||||||
| 
 |  | ||||||
|   function __construct(Schema $schema, ?Input $input, $valueKey, ?array $params) { |  | ||||||
|     parent::__construct($schema, $input, $valueKey, $params); |  | ||||||
|     $this->ensureArray = $params["ensure_array"] ?? self::DEFAULT_ENSURE_ARRAY; |  | ||||||
|     $this->ensureKeys = $params["ensure_keys"] ?? self::DEFAULT_ENSURE_KEYS; |  | ||||||
|     $this->ensureOrder = $params["ensure_order"] ?? self::DEFAULT_ENSURE_ORDER; |  | ||||||
|   } |  | ||||||
| 
 | 
 | ||||||
|   public bool $ensureArray; |   public bool $ensureArray; | ||||||
|  |   public bool $ensureAssoc; | ||||||
|   public bool $ensureKeys; |   public bool $ensureKeys; | ||||||
|   public bool $ensureOrder; |   public bool $ensureOrder; | ||||||
| 
 | 
 | ||||||
|  |   public function resetParams(?array $params): void { | ||||||
|  |     parent::resetParams($params); | ||||||
|  |     $this->ensureArray = $params["ensure_array"] ?? ref_schema::ASSOC_PARAMS_SCHEMA["ensure_array"][1]; | ||||||
|  |     $this->ensureAssoc = $params["ensure_assoc"] ?? ref_schema::ASSOC_PARAMS_SCHEMA["ensure_assoc"][1]; | ||||||
|  |     $this->ensureKeys = $params["ensure_keys"] ?? ref_schema::ASSOC_PARAMS_SCHEMA["ensure_keys"][1]; | ||||||
|  |     $this->ensureOrder = $params["ensure_order"] ?? ref_schema::ASSOC_PARAMS_SCHEMA["ensure_order"][1]; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   public ?ScalarWrapper $arrayWrapper = null; |   public ?ScalarWrapper $arrayWrapper = null; | ||||||
| 
 | 
 | ||||||
|   /** liste des clés valides */ |   /** liste des clés valides */ | ||||||
| @ -30,4 +29,10 @@ class AssocWrapperContext extends WrapperContext { | |||||||
| 
 | 
 | ||||||
|   /** @var Wrapper[] */ |   /** @var Wrapper[] */ | ||||||
|   public array $keyWrappers; |   public array $keyWrappers; | ||||||
|  | 
 | ||||||
|  |   /** @var string|int|null clé sélectionnée */ | ||||||
|  |   public $selectedKey = null; | ||||||
|  | 
 | ||||||
|  |   /** résultat consolidé de l'analyse du tableau et de ses composants */ | ||||||
|  |   public ConsolidatedResult $consolidatedResult; | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,53 +0,0 @@ | |||||||
| <?php |  | ||||||
| namespace nur\sery\wip\schema\_list; |  | ||||||
| 
 |  | ||||||
| use nulib\ValueException; |  | ||||||
| use nur\sery\wip\schema\Result; |  | ||||||
| 
 |  | ||||||
| class ListResult extends Result { |  | ||||||
|   function __construct(Result $arrayResult, array &$keyResults) { |  | ||||||
|     $this->arrayResult = $arrayResult; |  | ||||||
|     $this->keyResults =& $keyResults; |  | ||||||
|     $this->result =& $this->arrayResult; |  | ||||||
|       parent::__construct(); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function isList(?ListResult &$result=null): bool { $result = $this; return true;} |  | ||||||
| 
 |  | ||||||
|   protected Result $arrayResult; |  | ||||||
| 
 |  | ||||||
|   /** @var Result[] */ |  | ||||||
|   protected array $keyResults; |  | ||||||
| 
 |  | ||||||
|   function getKeys(): array { |  | ||||||
|     return array_keys($this->keyResults); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   protected Result $result; |  | ||||||
| 
 |  | ||||||
|   function select($key): Result { |  | ||||||
|     if ($key === null) { |  | ||||||
|       $this->result =& $this->arrayResult; |  | ||||||
|     } elseif (array_key_exists($key, $this->keyResults)) { |  | ||||||
|       $this->result =& $this->keyResults[$key]; |  | ||||||
|     } else { |  | ||||||
|       throw ValueException::invalid_key($key); |  | ||||||
|     } |  | ||||||
|     return $this; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function reset(): void { |  | ||||||
|     $this->arrayResult->reset(); |  | ||||||
|     foreach ($this->keyResults as $result) { |  | ||||||
|       $result->reset(); |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function __get(string $name) { |  | ||||||
|     return $this->result[$name]; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function __set(string $name, $value): void { |  | ||||||
|     $this->result[$name] = $value; |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| @ -1,10 +1,10 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\_list; | namespace nulib\schema\_list; | ||||||
| 
 | 
 | ||||||
| use nulib\ref\schema\ref_schema; | use nulib\ref\schema\ref_schema; | ||||||
|  | use nulib\schema\Schema; | ||||||
|  | use nulib\schema\Wrapper; | ||||||
| use nulib\ValueException; | use nulib\ValueException; | ||||||
| use nur\sery\wip\schema\Schema; |  | ||||||
| use nur\sery\wip\schema\Wrapper; |  | ||||||
| 
 | 
 | ||||||
| class ListSchema extends Schema { | class ListSchema extends Schema { | ||||||
|   /** @var array meta-schema d'un schéma de nature liste */ |   /** @var array meta-schema d'un schéma de nature liste */ | ||||||
| @ -51,18 +51,13 @@ class ListSchema extends Schema { | |||||||
|     $this->definition = $definition; |     $this->definition = $definition; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function isList(?ListSchema &$schema=null): bool { |  | ||||||
|     $schema = $this; |  | ||||||
|     return true; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   const KEYS = [null]; |   const KEYS = [null]; | ||||||
| 
 | 
 | ||||||
|   function getKeys(): array { |   function getKeys(): array { | ||||||
|     return self::KEYS; |     return self::KEYS; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   public function getSchema($key): Schema { |   public function getSchema($key=false): Schema { | ||||||
|     if ($key !== null) throw ValueException::invalid_key($key); |     if ($key !== null) throw ValueException::invalid_key($key); | ||||||
|     return $this; |     return $this; | ||||||
|   } |   } | ||||||
|  | |||||||
| @ -1,12 +1,10 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\_list; | namespace nulib\schema\_list; | ||||||
| 
 | 
 | ||||||
| use nur\sery\wip\schema\Result; | use nulib\schema\Result; | ||||||
| use nur\sery\wip\schema\Wrapper; | use nulib\schema\Wrapper; | ||||||
| 
 | 
 | ||||||
| abstract/*XXX*/ class ListWrapper extends Wrapper { | abstract/*XXX*/ class ListWrapper extends Wrapper { | ||||||
|   function isList(?ListWrapper &$wrapper=null): bool { $wrapper = $this; return true; } |  | ||||||
| 
 |  | ||||||
|   function ensureKeys(): bool { |   function ensureKeys(): bool { | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,172 +0,0 @@ | |||||||
| <?php |  | ||||||
| namespace nur\sery\wip\schema\_scalar; |  | ||||||
| 
 |  | ||||||
| use nulib\cl; |  | ||||||
| use nulib\ref\schema\ref_analyze; |  | ||||||
| use nulib\ref\schema\ref_schema; |  | ||||||
| use nulib\ValueException; |  | ||||||
| use nur\sery\wip\schema\Result; |  | ||||||
| use nur\sery\wip\schema\Schema; |  | ||||||
| use nur\sery\wip\schema\Wrapper; |  | ||||||
| use Throwable; |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * Class ScalarResult: résultat de l'analyse ou de la normalisation d'une valeur |  | ||||||
|  */ |  | ||||||
| class ScalarResult extends Result { |  | ||||||
|   function isScalar(?ScalarResult &$result=null): bool { $result = $this; return true; } |  | ||||||
| 
 |  | ||||||
|   function getKeys(): array { |  | ||||||
|     return ScalarSchema::KEYS; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function select($key): Result { |  | ||||||
|     if ($key !== null) throw ValueException::invalid_key($key); |  | ||||||
|     return $this; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   protected array $result; |  | ||||||
|    |  | ||||||
|   function reset(): void { |  | ||||||
|     $this->result = array_merge( |  | ||||||
|       array_fill_keys(static::KEYS, null), [ |  | ||||||
|       "resultAvailable" => false, |  | ||||||
|       "present" => false, |  | ||||||
|       "available" => false, |  | ||||||
|       "null" => false, |  | ||||||
|       "valid" => false, |  | ||||||
|       "normalized" => false, |  | ||||||
|     ]); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function __get(string $name) { |  | ||||||
|     return $this->result[$name]; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function __set(string $name, $value): void { |  | ||||||
|     $this->result[$name] = $value; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   protected function getMessage(string $key,  Schema $schema): string { |  | ||||||
|     $message = cl::get($schema->messages, $key); |  | ||||||
|     if ($message !== null) return $message; |  | ||||||
|     return cl::get(ref_schema::MESSAGES, $key); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function setMissing( Schema $schema): int { |  | ||||||
|     $this->resultAvailable = true; |  | ||||||
|     $this->present = false; |  | ||||||
|     $this->available = false; |  | ||||||
|     if (!$schema->required) { |  | ||||||
|       $this->null = false; |  | ||||||
|       $this->valid = true; |  | ||||||
|       $this->normalized = true; |  | ||||||
|       return ref_analyze::NORMALIZED; |  | ||||||
|     } else { |  | ||||||
|       $this->messageKey = $messageKey = "missing"; |  | ||||||
|       $this->message = $this->getMessage($messageKey, $schema); |  | ||||||
|       return ref_analyze::MISSING; |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function setUnavailable( Schema $schema): int { |  | ||||||
|     $this->resultAvailable = true; |  | ||||||
|     $this->present = true; |  | ||||||
|     $this->available = false; |  | ||||||
|     if (!$schema->required) { |  | ||||||
|       $this->null = false; |  | ||||||
|       $this->valid = true; |  | ||||||
|       $this->normalized = true; |  | ||||||
|       return ref_analyze::NORMALIZED; |  | ||||||
|     } else { |  | ||||||
|       $this->messageKey = $messageKey = "unavailable"; |  | ||||||
|       $this->message = $this->getMessage($messageKey, $schema); |  | ||||||
|       return ref_analyze::UNAVAILABLE; |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function setNull( Schema $schema): int { |  | ||||||
|     $this->resultAvailable = true; |  | ||||||
|     $this->present = true; |  | ||||||
|     $this->available = true; |  | ||||||
|     $this->null = true; |  | ||||||
|     if ($schema->nullable) { |  | ||||||
|       $this->valid = true; |  | ||||||
|       $this->normalized = true; |  | ||||||
|       return ref_analyze::NORMALIZED; |  | ||||||
|     } else { |  | ||||||
|       $this->messageKey = $messageKey = "null"; |  | ||||||
|       $this->message = $this->getMessage($messageKey, $schema); |  | ||||||
|       return ref_analyze::NULL; |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function setInvalid($value,  Schema $schema, ?Throwable $exception=null): int { |  | ||||||
|     $this->resultAvailable = true; |  | ||||||
|     $this->present = true; |  | ||||||
|     $this->available = true; |  | ||||||
|     $this->null = false; |  | ||||||
|     $this->valid = false; |  | ||||||
|     $this->origValue = $value; |  | ||||||
|     $this->messageKey = $messageKey = "invalid"; |  | ||||||
|     $message = $this->getMessage($messageKey, $schema); |  | ||||||
|     if ($exception !== null) { |  | ||||||
|       $tmessage = ValueException::get_message($exception); |  | ||||||
|       if ($tmessage) $message = $tmessage; |  | ||||||
|     } |  | ||||||
|     $this->message = $message; |  | ||||||
|     $this->exception = $exception; |  | ||||||
|     return ref_analyze::INVALID; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function addInvalidMessage(Wrapper $wrapper): void { |  | ||||||
|     $this->resultAvailable = true; |  | ||||||
|     $this->present = true; |  | ||||||
|     $this->available = true; |  | ||||||
|     $this->null = false; |  | ||||||
|     $this->valid = false; |  | ||||||
|     $this->messageKey = "invalid"; |  | ||||||
|     $result = $wrapper->getResult(); |  | ||||||
|     $resultException = $result->exception; |  | ||||||
|     $resultMessage = $result->message; |  | ||||||
|     if ($resultException !== null) { |  | ||||||
|       $tmessage = ValueException::get_message($resultException); |  | ||||||
|       if ($tmessage) { |  | ||||||
|         if ($resultMessage !== null) $resultMessage .= ": "; |  | ||||||
|         $resultMessage .= $tmessage; |  | ||||||
|       } |  | ||||||
|     } |  | ||||||
|     $message = $this->message; |  | ||||||
|     if ($message) $message .= "\n"; |  | ||||||
|     $message .= $resultMessage; |  | ||||||
|     $this->message = $message; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function setValid($normalizedValue=null): int { |  | ||||||
|     $this->resultAvailable = true; |  | ||||||
|     $this->present = true; |  | ||||||
|     $this->available = true; |  | ||||||
|     $this->null = false; |  | ||||||
|     $this->valid = true; |  | ||||||
|     $this->normalizedValue = $normalizedValue; |  | ||||||
|     return ref_analyze::VALID; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function setNormalized(): int { |  | ||||||
|     $this->resultAvailable = true; |  | ||||||
|     $this->present = true; |  | ||||||
|     $this->available = true; |  | ||||||
|     $this->null = false; |  | ||||||
|     $this->valid = true; |  | ||||||
|     $this->normalized = true; |  | ||||||
|     return ref_analyze::NORMALIZED; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function throw(bool $throw): void { |  | ||||||
|     if ($throw) { |  | ||||||
|       $exception = $this->exception; |  | ||||||
|       if ($exception !== null) throw $exception; |  | ||||||
|       else throw new ValueException($this->message); |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| @ -1,11 +1,10 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\_scalar; | namespace nulib\schema\_scalar; | ||||||
| 
 | 
 | ||||||
| use nulib\cl; |  | ||||||
| use nulib\ref\schema\ref_schema; | use nulib\ref\schema\ref_schema; | ||||||
|  | use nulib\schema\Schema; | ||||||
|  | use nulib\schema\Wrapper; | ||||||
| use nulib\ValueException; | use nulib\ValueException; | ||||||
| use nur\sery\wip\schema\Schema; |  | ||||||
| use nur\sery\wip\schema\Wrapper; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class ScalarSchema |  * Class ScalarSchema | ||||||
| @ -56,24 +55,23 @@ class ScalarSchema extends Schema { | |||||||
|       $this->_definition = $definition; |       $this->_definition = $definition; | ||||||
|       self::_ensure_type($definition); |       self::_ensure_type($definition); | ||||||
|       self::_ensure_schema_instances($definition); |       self::_ensure_schema_instances($definition); | ||||||
|  |     } else { | ||||||
|  |       # ici, $definition contient un schema déjà instancié, mais c'est le mieux
 | ||||||
|  |       # qu'on puisse faire
 | ||||||
|  |       $this->_definition = $definition; | ||||||
|     } |     } | ||||||
|     $this->definition = $definition; |     $this->definition = $definition; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function isScalar(?ScalarSchema &$schema=null): bool { |   const KEYS = []; | ||||||
|     $schema = $this; |  | ||||||
|     return true; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   const KEYS = [null]; |  | ||||||
| 
 | 
 | ||||||
|   function getKeys(): array { |   function getKeys(): array { | ||||||
|     return self::KEYS; |     return self::KEYS; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function getSchema($key): Schema { |   function getSchema($key=false): Schema { | ||||||
|     if ($key !== null) throw ValueException::invalid_key($key); |     if ($key === false || $key === null || $key === "") return $this; | ||||||
|     return $this; |     throw ValueException::invalid_key($key); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   protected function newWrapper(): ScalarWrapper { |   protected function newWrapper(): ScalarWrapper { | ||||||
| @ -85,7 +83,15 @@ class ScalarSchema extends Schema { | |||||||
|     # cf le code similaire dans ScalarWrapper::__construct()
 |     # cf le code similaire dans ScalarWrapper::__construct()
 | ||||||
|     $dontAnalyze = $value === null && $wrapper === null; |     $dontAnalyze = $value === null && $wrapper === null; | ||||||
|     if (!($wrapper instanceof ScalarWrapper)) $wrapper = $this->newWrapper(); |     if (!($wrapper instanceof ScalarWrapper)) $wrapper = $this->newWrapper(); | ||||||
|  | 
 | ||||||
|  |     # la nature du schéma peut contenir des paramètres par défaut
 | ||||||
|  |     $nature = $this->definition[""]; | ||||||
|  |     foreach (array_keys(ref_schema::SCALAR_PARAMS_SCHEMA) as $paramKey) { | ||||||
|  |       $paramValue = $nature[$paramKey] ?? null; | ||||||
|  |       if ($paramValue !== null) $params[$paramKey] = $paramValue; | ||||||
|  |     } | ||||||
|     if ($params !== null) $wrapper->resetParams($params); |     if ($params !== null) $wrapper->resetParams($params); | ||||||
|  | 
 | ||||||
|     return $wrapper->reset($value, $valueKey, $dontAnalyze? ["analyze" => false]: null); |     return $wrapper->reset($value, $valueKey, $dontAnalyze? ["analyze" => false]: null); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,45 +1,38 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\_scalar; | namespace nulib\schema\_scalar; | ||||||
| 
 | 
 | ||||||
| use nulib\php\func; | use nulib\php\func; | ||||||
| use nulib\ref\schema\ref_analyze; | use nulib\ref\schema\ref_analyze; | ||||||
|  | use nulib\schema\Result; | ||||||
|  | use nulib\schema\Schema; | ||||||
|  | use nulib\schema\types; | ||||||
|  | use nulib\schema\types\IType; | ||||||
|  | use nulib\schema\Wrapper; | ||||||
|  | use nulib\schema\WrapperContext; | ||||||
| use nulib\ValueException; | use nulib\ValueException; | ||||||
| use nur\sery\wip\schema\_assoc\AssocWrapper; |  | ||||||
| use nur\sery\wip\schema\_assoc\AssocWrapperContext; |  | ||||||
| use nur\sery\wip\schema\Schema; |  | ||||||
| use nur\sery\wip\schema\types; |  | ||||||
| use nur\sery\wip\schema\types\IType; |  | ||||||
| use nur\sery\wip\schema\Wrapper; |  | ||||||
| use nur\sery\wip\schema\WrapperContext; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class ScalarWrapper |  * Class ScalarWrapper | ||||||
|  * |  * | ||||||
|  * @method ScalarWrapper reset(&$value, $valueKey=null, ?array $params=null) |  * @method ScalarWrapper reset(&$value, $valueKey=null, ?array $params=null) | ||||||
|  * @method ScalarResult getResult($key=false) |  | ||||||
|  * @method self set($value, ?array $params=null, $key=false) |  * @method self set($value, ?array $params=null, $key=false) | ||||||
|  * @method self unset(?array $params=null, $key=false) |  * @method self unset(?array $params=null, $key=false) | ||||||
|  */ |  */ | ||||||
| class ScalarWrapper extends Wrapper { | class ScalarWrapper extends Wrapper { | ||||||
|   function __construct(Schema $schema, &$value=null, $valueKey=null, ?array $params=null, ?WrapperContext $context=null) { |   function __construct(Schema $schema, &$value=null, $valueKey=null, ?array $params=null, ?WrapperContext $context=null) { | ||||||
|     if ($context === null) $context = new WrapperContext($schema, null, null, $params); |     if ($context === null) $context = new WrapperContext($schema, null, null, $params); | ||||||
|     $context->result = new ScalarResult(); |     $context->result = new Result(); | ||||||
|     $this->context = $context; |     $this->context = $context; | ||||||
| 
 | 
 | ||||||
|     # calculer manuellemet throw ici parce que WrapperContext le met à true par
 |     if ($value !== null) { | ||||||
|     # défaut. on veut pouvoir mettre temporairement throw à false si jamais il
 |       # n'initialiser que si $value n'est pas null
 | ||||||
|     # n'est pas spécifié par l'utilisateur
 |       $this->reset($value, $valueKey); | ||||||
|     $throw = $params["throw"] ?? null; |     } else { | ||||||
|     # Si $value est null, ne pas lancer d'exception, parce qu'on considère que
 |       # il faut au moins que le type soit disponible
 | ||||||
|     # c'est une initialisation sans conséquences
 |       $this->resetContext(false); | ||||||
|     if ($throw === null && $value !== null) $throw = true; |     } | ||||||
|     $context->throw = $throw ?? false; |  | ||||||
|     $this->reset($value, $valueKey); |  | ||||||
|     $context->throw = $throw ?? true; |  | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function isScalar(?ScalarWrapper &$wrapper=null): bool { $wrapper = $this; return true; } |  | ||||||
| 
 |  | ||||||
|   protected WrapperContext $context; |   protected WrapperContext $context; | ||||||
| 
 | 
 | ||||||
|   function getKeys(): array { |   function getKeys(): array { | ||||||
| @ -47,9 +40,9 @@ class ScalarWrapper extends Wrapper { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** @param string|int|null $key */ |   /** @param string|int|null $key */ | ||||||
|   function select($key): ScalarWrapper { |   function select($key=null): ScalarWrapper { | ||||||
|     if ($key !== null) throw ValueException::invalid_key($key); |     if ($key === null || $key === "") return $this; | ||||||
|     return $this; |     throw ValueException::invalid_key($key); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** analyser la valeur et résoudre son type */ |   /** analyser la valeur et résoudre son type */ | ||||||
| @ -58,7 +51,6 @@ class ScalarWrapper extends Wrapper { | |||||||
|     $schema = $context->schema; |     $schema = $context->schema; | ||||||
|     $input = $context->input; |     $input = $context->input; | ||||||
|     $valueKey = $context->valueKey; |     $valueKey = $context->valueKey; | ||||||
|     /** @var ScalarResult $result */ |  | ||||||
|     $result = $context->result; |     $result = $context->result; | ||||||
| 
 | 
 | ||||||
|     $default = $schema->default; |     $default = $schema->default; | ||||||
| @ -131,6 +123,16 @@ class ScalarWrapper extends Wrapper { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     $value = $input->get($valueKey); |     $value = $input->get($valueKey); | ||||||
|  |     $missing = $type->getMissingValue($haveMissing); | ||||||
|  |     if ($haveMissing && $value === $missing) { | ||||||
|  |       if ($default !== null) { | ||||||
|  |         $input->set($default, $valueKey); | ||||||
|  |         return $result->setNormalized(); | ||||||
|  |       } else { | ||||||
|  |         return $result->setMissing($schema); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     $context->origValue = $context->value = $value; |     $context->origValue = $context->value = $value; | ||||||
|     if ($type->isNull($value)) { |     if ($type->isNull($value)) { | ||||||
|       return $result->setNull($schema); |       return $result->setNull($schema); | ||||||
| @ -152,7 +154,6 @@ class ScalarWrapper extends Wrapper { | |||||||
|     $schema = $context->schema; |     $schema = $context->schema; | ||||||
|     $input = $context->input; |     $input = $context->input; | ||||||
|     $valueKey = $context->valueKey; |     $valueKey = $context->valueKey; | ||||||
|     /** @var ScalarResult $result */ |  | ||||||
|     $result = $context->result; |     $result = $context->result; | ||||||
| 
 | 
 | ||||||
|     /** @var func $analyzerFunc */ |     /** @var func $analyzerFunc */ | ||||||
| @ -201,7 +202,6 @@ class ScalarWrapper extends Wrapper { | |||||||
|     $schema = $context->schema; |     $schema = $context->schema; | ||||||
|     $input = $context->input; |     $input = $context->input; | ||||||
|     $valueKey = $context->valueKey; |     $valueKey = $context->valueKey; | ||||||
|     /** @var ScalarResult $result */ |  | ||||||
|     $result = $context->result; |     $result = $context->result; | ||||||
| 
 | 
 | ||||||
|     $normalize = false; |     $normalize = false; | ||||||
|  | |||||||
| @ -1,11 +1,11 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\input; | namespace nulib\schema\input; | ||||||
| 
 | 
 | ||||||
| #XXX implémenter le renommage de paramètres et faire des méthodes pour
 | #XXX implémenter le renommage de paramètres et faire des méthodes pour
 | ||||||
| # construire des querystring et paramètres de formulaires
 | # construire des querystring et paramètres de formulaires
 | ||||||
| use nur\sery\wip\php\access\FormAccess; | use nulib\php\access\FormAccess; | ||||||
| use nur\sery\wip\php\access\IAccess; | use nulib\php\access\IAccess; | ||||||
| use nur\sery\wip\php\access\ShadowAccess; | use nulib\php\access\ShadowAccess; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class FormInput: accès à des paramètres de formulaire (POST ou GET, dans cet |  * Class FormInput: accès à des paramètres de formulaire (POST ou GET, dans cet | ||||||
|  | |||||||
| @ -1,8 +1,8 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\input; | namespace nulib\schema\input; | ||||||
| 
 | 
 | ||||||
| use nur\sery\wip\php\access\GetAccess; | use nulib\php\access\GetAccess; | ||||||
| use nur\sery\wip\php\access\IAccess; | use nulib\php\access\IAccess; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class GetInput: accès à des paramètres de formulaire de type GET uniquement |  * Class GetInput: accès à des paramètres de formulaire de type GET uniquement | ||||||
|  | |||||||
| @ -1,10 +1,11 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\input; | namespace nulib\schema\input; | ||||||
| 
 | 
 | ||||||
|  | use nulib\php\access\IAccess; | ||||||
|  | use nulib\php\access\KeyAccess; | ||||||
|  | use nulib\php\access\PropertyAccess; | ||||||
|  | use nulib\ref\schema\ref_input; | ||||||
| use nulib\StateException; | use nulib\StateException; | ||||||
| use nur\sery\wip\php\access\IAccess; |  | ||||||
| use nur\sery\wip\php\access\KeyAccess; |  | ||||||
| use nur\sery\wip\php\access\PropertyAccess; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class Input: accès à une valeur |  * Class Input: accès à une valeur | ||||||
| @ -14,25 +15,23 @@ use nur\sery\wip\php\access\PropertyAccess; | |||||||
| class Input { | class Input { | ||||||
|   const ALLOW_EMPTY = true; |   const ALLOW_EMPTY = true; | ||||||
| 
 | 
 | ||||||
|   const ACCESS_AUTO = 0, ACCESS_KEY = 1, ACCESS_PROPERTY = 2; |  | ||||||
| 
 |  | ||||||
|   private static function unexpected_access_type(): StateException { |   private static function unexpected_access_type(): StateException { | ||||||
|     return StateException::unexpected_state("access_type"); |     return StateException::unexpected_state("access_type"); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function __construct(&$dest=null, ?array $params=null) { |   function __construct(&$dest=null, ?array $params=null) { | ||||||
|     $accessType = $params["access_type"] ?? self::ACCESS_AUTO; |     $accessType = $params["access_type"] ?? ref_input::ACCESS_AUTO; | ||||||
|     if ($accessType === self::ACCESS_AUTO) { |     if ($accessType === ref_input::ACCESS_AUTO) { | ||||||
|       $accessType = is_object($dest)? self::ACCESS_PROPERTY: self::ACCESS_KEY; |       $accessType = is_object($dest)? ref_input::ACCESS_PROPERTY : ref_input::ACCESS_KEY; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     $allowEmpty = $params["allow_empty"] ?? static::ALLOW_EMPTY; |     $allowEmpty = $params["allow_empty"] ?? static::ALLOW_EMPTY; | ||||||
|     if ($accessType == self::ACCESS_PROPERTY) { |     if ($accessType == ref_input::ACCESS_PROPERTY) { | ||||||
|       $this->access = new PropertyAccess($dest, null, [ |       $this->access = new PropertyAccess($dest, null, [ | ||||||
|         "allow_empty" => $allowEmpty, |         "allow_empty" => $allowEmpty, | ||||||
|         "allow_null" => true, |         "allow_null" => true, | ||||||
|       ]); |       ]); | ||||||
|     } elseif ($accessType == self::ACCESS_KEY) { |     } elseif ($accessType == ref_input::ACCESS_KEY) { | ||||||
|       $this->access = new KeyAccess($dest, null, [ |       $this->access = new KeyAccess($dest, null, [ | ||||||
|         "allow_empty" => $allowEmpty, |         "allow_empty" => $allowEmpty, | ||||||
|         "allow_null" => true, |         "allow_null" => true, | ||||||
| @ -72,4 +71,20 @@ class Input { | |||||||
|     $input->access = $this->access->addKey($key); |     $input->access = $this->access->addKey($key); | ||||||
|     return $input; |     return $input; | ||||||
|   } |   } | ||||||
|  | 
 | ||||||
|  |   function ensureAssoc(array $keys, ?array $params=null): void { | ||||||
|  |     $this->access->ensureAssoc($keys, $params); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function ensureKeys(array $defaults, ?array $missings, ?array $params=null): void { | ||||||
|  |     $this->access->ensureKeys($defaults, $missings, $params); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function deleteMissings(array $missings, ?array $params=null): void { | ||||||
|  |     $this->access->deleteMissings($missings, $params); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function ensureOrder(array $keys, ?array $params=null): void { | ||||||
|  |     $this->access->ensureOrder($keys, $params); | ||||||
|  |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,8 +1,8 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\input; | namespace nulib\schema\input; | ||||||
| 
 | 
 | ||||||
| use nur\sery\wip\php\access\IAccess; | use nulib\php\access\IAccess; | ||||||
| use nur\sery\wip\php\access\PostAccess; | use nulib\php\access\PostAccess; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class PostInput: accès à des paramètres de formulaire de type POST uniquement |  * Class PostInput: accès à des paramètres de formulaire de type POST uniquement | ||||||
|  | |||||||
| @ -1,22 +1,22 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema; | namespace nulib\schema; | ||||||
| 
 | 
 | ||||||
|  | use nulib\schema\types\IType; | ||||||
|  | use nulib\schema\types\Registry; | ||||||
|  | use nulib\schema\types\tarray; | ||||||
|  | use nulib\schema\types\tbool; | ||||||
|  | use nulib\schema\types\tcontent; | ||||||
|  | use nulib\schema\types\tfloat; | ||||||
|  | use nulib\schema\types\tfunc; | ||||||
|  | use nulib\schema\types\tint; | ||||||
|  | use nulib\schema\types\tkey; | ||||||
|  | use nulib\schema\types\tmixed; | ||||||
|  | use nulib\schema\types\tpkey; | ||||||
|  | use nulib\schema\types\traw; | ||||||
|  | use nulib\schema\types\trawstring; | ||||||
|  | use nulib\schema\types\tstring; | ||||||
|  | use nulib\schema\types\ttext; | ||||||
| use nulib\ValueException; | use nulib\ValueException; | ||||||
| use nur\sery\wip\schema\types\IType; |  | ||||||
| use nur\sery\wip\schema\types\Registry; |  | ||||||
| use nur\sery\wip\schema\types\tarray; |  | ||||||
| use nur\sery\wip\schema\types\tbool; |  | ||||||
| use nur\sery\wip\schema\types\tcallable; |  | ||||||
| use nur\sery\wip\schema\types\tcontent; |  | ||||||
| use nur\sery\wip\schema\types\tfloat; |  | ||||||
| use nur\sery\wip\schema\types\tint; |  | ||||||
| use nur\sery\wip\schema\types\tkey; |  | ||||||
| use nur\sery\wip\schema\types\tmixed; |  | ||||||
| use nur\sery\wip\schema\types\tpkey; |  | ||||||
| use nur\sery\wip\schema\types\traw; |  | ||||||
| use nur\sery\wip\schema\types\trawstring; |  | ||||||
| use nur\sery\wip\schema\types\tstring; |  | ||||||
| use nur\sery\wip\schema\types\ttext; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class types: classe outil pour gérer le registre de types |  * Class types: classe outil pour gérer le registre de types | ||||||
| @ -47,7 +47,7 @@ class types { | |||||||
|   static function int(bool $nullable=true): tint { return self::get($nullable, "int"); } |   static function int(bool $nullable=true): tint { return self::get($nullable, "int"); } | ||||||
|   static function float(bool $nullable=true): tfloat { return self::get($nullable, "float"); } |   static function float(bool $nullable=true): tfloat { return self::get($nullable, "float"); } | ||||||
|   static function array(bool $nullable=true): tarray { return self::get($nullable, "array"); } |   static function array(bool $nullable=true): tarray { return self::get($nullable, "array"); } | ||||||
|   static function callable(bool $nullable=true): tcallable { return self::get($nullable, "callable"); } |   static function callable(bool $nullable=true): tfunc { return self::get($nullable, "callable"); } | ||||||
|   static function raw(bool $nullable=true): traw { return self::get($nullable, "raw"); } |   static function raw(bool $nullable=true): traw { return self::get($nullable, "raw"); } | ||||||
|   static function mixed(bool $nullable=true): tmixed { return self::get($nullable, "mixed"); } |   static function mixed(bool $nullable=true): tmixed { return self::get($nullable, "mixed"); } | ||||||
|   static function key(bool $nullable=true): tkey { return self::get($nullable, "key"); } |   static function key(bool $nullable=true): tkey { return self::get($nullable, "key"); } | ||||||
|  | |||||||
| @ -1,10 +1,10 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
|  | use nulib\schema\input\Input; | ||||||
|  | use nulib\schema\Result; | ||||||
|  | use nulib\schema\Schema; | ||||||
| use nulib\ValueException; | use nulib\ValueException; | ||||||
| use nur\sery\wip\schema\input\Input; |  | ||||||
| use nur\sery\wip\schema\Result; |  | ||||||
| use nur\sery\wip\schema\Schema; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Interface IType: un type de données |  * Interface IType: un type de données | ||||||
| @ -48,26 +48,25 @@ interface IType { | |||||||
|   function getPhpType(bool $allowNullable=true): ?string; |   function getPhpType(bool $allowNullable=true): ?string; | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
|    * indiquer si c'est le type d'une valeur qui ne peut prendre que 2 états: une |    * obtenir la valeur "inexistante" pour les objets de ce type | ||||||
|    * "vraie" et une "fausse" |    * | ||||||
|  |    * si $valid reçoit la valeur false, il faut ignorer la valeur de retour: | ||||||
|  |    * cela veut dire qu'il n'y a pas de valeur "inexistant" pour les valeurs de | ||||||
|  |    * ce type | ||||||
|    */ |    */ | ||||||
|   function is2States(): bool; |   function getMissingValue(?bool &$valid=null); | ||||||
|  | 
 | ||||||
|  |   /** obtenir la valeur "nulle" pour les objets de ce type */ | ||||||
|  |   function getNullValue(); | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
|    * Si {@link is2States()} est vrai, retourner les deux valeurs [faux, vrai] |    * si c'est le type d'une valeur qui ne prendre qu'une liste prédéterminée | ||||||
|  |    * d'états spécifiques, retourner le nombre d'états possibles, et mettre à | ||||||
|  |    * jour $states avec les valeurs possibles | ||||||
|  |    * | ||||||
|  |    * sinon, retourner 0 et ne pas mettre $states à jour | ||||||
|    */ |    */ | ||||||
|   function get2States(): array; |   function getNbStates(?array &$states=null): int; | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * indiquer si c'est le type d'une valeur qui ne peut prendre que 3 états: une |  | ||||||
|    * "vraie", une "fausse", et une "indéterminée" |  | ||||||
|    */ |  | ||||||
|   function is3States(): bool; |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * Si {@link is3States()} est vrai, retourner les 3 valeurs [faux, vrai, undef] |  | ||||||
|    */ |  | ||||||
|   function get3States(): array; |  | ||||||
| 
 | 
 | ||||||
|   /** la donnée $input($valueKey) est-elle disponible? */ |   /** la donnée $input($valueKey) est-elle disponible? */ | ||||||
|   function isAvailable(Input $input, $valueKey): bool; |   function isAvailable(Input $input, $valueKey): bool; | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| use nulib\cl; | use nulib\cl; | ||||||
| use nulib\php\func; | use nulib\php\func; | ||||||
| @ -12,10 +12,9 @@ class Registry { | |||||||
|     "text" => ttext::class, |     "text" => ttext::class, | ||||||
|     "bool" => tbool::class, "boolean" => tbool::class, |     "bool" => tbool::class, "boolean" => tbool::class, | ||||||
|     "int" => tint::class, "integer" => tint::class, |     "int" => tint::class, "integer" => tint::class, | ||||||
|     "float" => tfloat::class, "flt" => tfloat::class, |     "float" => tfloat::class, "flt" => tfloat::class, "double" => tfloat::class, "dbl" => tfloat::class, | ||||||
|     "double" => tfloat::class, "dbl" => tfloat::class, |  | ||||||
|     "array" => tarray::class, |     "array" => tarray::class, | ||||||
|     "callable" => tcallable::class, |     "func" => tfunc::class, "function" => tfunc::class, "callable" => tfunc::class, | ||||||
|     # types spéciaux
 |     # types spéciaux
 | ||||||
|     "raw" => traw::class, |     "raw" => traw::class, | ||||||
|     "mixed" => tmixed::class, |     "mixed" => tmixed::class, | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| abstract class _tformatable extends _tsimple { | abstract class _tformatable extends _tsimple { | ||||||
|   const FORMAT = null; |   const FORMAT = null; | ||||||
|  | |||||||
| @ -1,9 +1,8 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| use nulib\StateException; | use nulib\schema\input\Input; | ||||||
| use nur\prop; | use nur\prop; | ||||||
| use nur\sery\wip\schema\input\Input; |  | ||||||
| use nur\str; | use nur\str; | ||||||
| 
 | 
 | ||||||
| abstract class _tsimple implements IType { | abstract class _tsimple implements IType { | ||||||
| @ -43,20 +42,13 @@ abstract class _tsimple implements IType { | |||||||
|     return $phpType; |     return $phpType; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function is2States(): bool { |   function getMissingValue(?bool &$valid=null) { | ||||||
|  |     $valid = true; | ||||||
|     return false; |     return false; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function get2States(): array { |   function getNbStates(?array &$states=null): int { | ||||||
|     throw StateException::not_implemented(); |     return 0; | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function is3States(): bool { |  | ||||||
|     return false; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function get3States(): array { |  | ||||||
|     throw StateException::not_implemented(); |  | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function isAvailable(Input $input, $valueKey): bool { |   function isAvailable(Input $input, $valueKey): bool { | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| use nulib\str; | use nulib\str; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| abstract class _tunion extends _tsimple { | abstract class _tunion extends _tsimple { | ||||||
|   function getPhpType(bool $allowNullable=true): ?string { |   function getPhpType(bool $allowNullable=true): ?string { | ||||||
|  | |||||||
| @ -1,12 +1,10 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| use nulib\cl; | use nulib\cl; | ||||||
| use nulib\ValueException; | use nulib\schema\_scalar\ScalarSchema; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarResult; | use nulib\schema\Result; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarSchema; | use nulib\schema\Schema; | ||||||
| use nur\sery\wip\schema\Result; |  | ||||||
| use nur\sery\wip\schema\Schema; |  | ||||||
| 
 | 
 | ||||||
| class tarray extends _tstring { | class tarray extends _tstring { | ||||||
|   const NAME = "array"; |   const NAME = "array"; | ||||||
| @ -35,6 +33,10 @@ class tarray extends _tstring { | |||||||
|     return "array"; |     return "array"; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   function getNullValue() { | ||||||
|  |     return $this->nullable? null: []; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   function isValid($value, ?bool &$normalized=null): bool { |   function isValid($value, ?bool &$normalized=null): bool { | ||||||
|     $normalized = is_array($value); |     $normalized = is_array($value); | ||||||
|     return $normalized || is_scalar($value); |     return $normalized || is_scalar($value); | ||||||
| @ -46,7 +48,6 @@ class tarray extends _tstring { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
|    * @var ScalarResult $result |  | ||||||
|    * @var ScalarSchema $schema |    * @var ScalarSchema $schema | ||||||
|    */ |    */ | ||||||
|   function normalize(&$value, Result $result, Schema $schema): bool { |   function normalize(&$value, Result $result, Schema $schema): bool { | ||||||
|  | |||||||
| @ -1,14 +1,13 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| use nulib\cl; | use nulib\cl; | ||||||
|  | use nulib\schema\_scalar\ScalarSchema; | ||||||
|  | use nulib\schema\input\Input; | ||||||
|  | use nulib\schema\Result; | ||||||
|  | use nulib\schema\Schema; | ||||||
| use nulib\ValueException; | use nulib\ValueException; | ||||||
| use nur\prop; | use nur\prop; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarResult; |  | ||||||
| use nur\sery\wip\schema\_scalar\ScalarSchema; |  | ||||||
| use nur\sery\wip\schema\input\Input; |  | ||||||
| use nur\sery\wip\schema\Result; |  | ||||||
| use nur\sery\wip\schema\Schema; |  | ||||||
| 
 | 
 | ||||||
| class tbool extends _tformatable { | class tbool extends _tformatable { | ||||||
|   const NAME = "bool"; |   const NAME = "bool"; | ||||||
| @ -60,20 +59,23 @@ class tbool extends _tformatable { | |||||||
|     return "bool"; |     return "bool"; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function is2States(): bool { |   function getMissingValue(?bool &$valid=null) { | ||||||
|     return !$this->nullable; |     $valid = false; | ||||||
|  |     return null; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function get2States(): array { |   function getNullValue() { | ||||||
|     return [false, true]; |     return $this->nullable? null: false; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function is3States(): bool { |   public function getNbStates(?array &$states=null): int { | ||||||
|     return $this->nullable; |     if ($this->nullable) { | ||||||
|   } |       $states = [false, true, null]; | ||||||
| 
 |       return 3; | ||||||
|   function get3States(): array { |     } else { | ||||||
|     return [false, true, null]; |       $states = [false, true]; | ||||||
|  |       return 2; | ||||||
|  |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function isAvailable(Input $input, $valueKey): bool { |   function isAvailable(Input $input, $valueKey): bool { | ||||||
| @ -96,7 +98,6 @@ class tbool extends _tformatable { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
|    * @var ScalarResult $result |  | ||||||
|    * @var ScalarSchema $schema |    * @var ScalarSchema $schema | ||||||
|    */ |    */ | ||||||
|   function normalize(&$value, Result $result, Schema $schema): bool { |   function normalize(&$value, Result $result, Schema $schema): bool { | ||||||
|  | |||||||
| @ -1,11 +1,10 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| use nulib\php\content\c; | use nulib\php\content\c; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarResult; | use nulib\schema\_scalar\ScalarSchema; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarSchema; | use nulib\schema\Result; | ||||||
| use nur\sery\wip\schema\Result; | use nulib\schema\Schema; | ||||||
| use nur\sery\wip\schema\Schema; |  | ||||||
| 
 | 
 | ||||||
| abstract class tcontent extends _tunion { | abstract class tcontent extends _tunion { | ||||||
|   const NAME = "content"; |   const NAME = "content"; | ||||||
| @ -23,6 +22,10 @@ abstract class tcontent extends _tunion { | |||||||
|     return "string|array"; |     return "string|array"; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   function getNullValue() { | ||||||
|  |     return $this->nullable? null: []; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   function isValid($value, ?bool &$normalized=null): bool { |   function isValid($value, ?bool &$normalized=null): bool { | ||||||
|     $normalized = is_string($value) || is_array($value); |     $normalized = is_string($value) || is_array($value); | ||||||
|     return $normalized || is_scalar($value); |     return $normalized || is_scalar($value); | ||||||
| @ -33,7 +36,6 @@ abstract class tcontent extends _tunion { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
|    * @var ScalarResult $result |  | ||||||
|    * @var ScalarSchema $schema |    * @var ScalarSchema $schema | ||||||
|    */ |    */ | ||||||
|   function normalize(&$value, Result $result, Schema $schema): bool { |   function normalize(&$value, Result $result, Schema $schema): bool { | ||||||
|  | |||||||
| @ -1,11 +1,10 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
|  | use nulib\schema\_scalar\ScalarSchema; | ||||||
|  | use nulib\schema\Result; | ||||||
|  | use nulib\schema\Schema; | ||||||
| use nulib\ValueException; | use nulib\ValueException; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarResult; |  | ||||||
| use nur\sery\wip\schema\_scalar\ScalarSchema; |  | ||||||
| use nur\sery\wip\schema\Result; |  | ||||||
| use nur\sery\wip\schema\Schema; |  | ||||||
| 
 | 
 | ||||||
| class tfloat extends _tformatable { | class tfloat extends _tformatable { | ||||||
|   const NAME = "float"; |   const NAME = "float"; | ||||||
| @ -24,6 +23,10 @@ class tfloat extends _tformatable { | |||||||
|     return "float"; |     return "float"; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   function getNullValue() { | ||||||
|  |     return $this->nullable? null: 0.0; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   function isValid($value, ?bool &$normalized=null): bool { |   function isValid($value, ?bool &$normalized=null): bool { | ||||||
|     $normalized = is_float($value); |     $normalized = is_float($value); | ||||||
|     return is_scalar($value); |     return is_scalar($value); | ||||||
| @ -40,7 +43,6 @@ class tfloat extends _tformatable { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
|    * @var ScalarResult $result |  | ||||||
|    * @var ScalarSchema $schema |    * @var ScalarSchema $schema | ||||||
|    */ |    */ | ||||||
|   function normalize(&$value, Result $result, Schema $schema): bool { |   function normalize(&$value, Result $result, Schema $schema): bool { | ||||||
|  | |||||||
| @ -1,31 +1,34 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| use Exception; | use Exception; | ||||||
| use nulib\php\func; | use nulib\php\func; | ||||||
|  | use nulib\schema\_scalar\ScalarSchema; | ||||||
|  | use nulib\schema\Result; | ||||||
|  | use nulib\schema\Schema; | ||||||
| use nulib\ValueException; | use nulib\ValueException; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarResult; |  | ||||||
| use nur\sery\wip\schema\_scalar\ScalarSchema; |  | ||||||
| use nur\sery\wip\schema\Result; |  | ||||||
| use nur\sery\wip\schema\Schema; |  | ||||||
| 
 | 
 | ||||||
| class tcallable extends _tsimple { | class tfunc extends _tsimple { | ||||||
|   const NAME = "callable"; |   const NAME = "func"; | ||||||
| 
 | 
 | ||||||
|   const ALIASES = ["func", "function"]; |   const ALIASES = ["function", "callable"]; | ||||||
| 
 | 
 | ||||||
|   static function ensure_callable(&$callable): void { |   static function ensure_func(&$func): void { | ||||||
|     $callable = func::ensure($callable); |     $func = func::ensure($func); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   static function ensure_ncallable(&$callable): void { |   static function ensure_nfunc(&$func): void { | ||||||
|     if ($callable !== null) self::ensure_callable($callable); |     if ($func !== null) self::ensure_func($func); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function getClass(): string { |   function getClass(): string { | ||||||
|     return func::class; |     return func::class; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   function getNullValue() { | ||||||
|  |     return null; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   function isValid($value, ?bool &$normalized=null): bool { |   function isValid($value, ?bool &$normalized=null): bool { | ||||||
|     $normalized = $value instanceof func; |     $normalized = $value instanceof func; | ||||||
|     return func::check($value); |     return func::check($value); | ||||||
| @ -40,7 +43,6 @@ class tcallable extends _tsimple { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
|    * @var ScalarResult $result |  | ||||||
|    * @var ScalarSchema $schema |    * @var ScalarSchema $schema | ||||||
|    */ |    */ | ||||||
|   function normalize(&$value, Result $result, Schema $schema): bool { |   function normalize(&$value, Result $result, Schema $schema): bool { | ||||||
| @ -1,12 +1,10 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
|  | use nulib\schema\_scalar\ScalarSchema; | ||||||
|  | use nulib\schema\Result; | ||||||
|  | use nulib\schema\Schema; | ||||||
| use nulib\ValueException; | use nulib\ValueException; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarResult; |  | ||||||
| use nur\sery\wip\schema\_scalar\ScalarSchema; |  | ||||||
| use nur\sery\wip\schema\input\Input; |  | ||||||
| use nur\sery\wip\schema\Result; |  | ||||||
| use nur\sery\wip\schema\Schema; |  | ||||||
| 
 | 
 | ||||||
| class tgeneric extends _tsimple { | class tgeneric extends _tsimple { | ||||||
|   function __construct(string $class, bool $nullable, ?array $params=null) { |   function __construct(string $class, bool $nullable, ?array $params=null) { | ||||||
| @ -20,8 +18,8 @@ class tgeneric extends _tsimple { | |||||||
|     return $this->class; |     return $this->class; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function isAvailable(Input $input, $valueKey): bool { |   function getNullValue() { | ||||||
|     return $input->isAvailable($valueKey); |     return null; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   public function isNull($value): bool { |   public function isNull($value): bool { | ||||||
| @ -38,7 +36,6 @@ class tgeneric extends _tsimple { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
|    * @var ScalarResult $result |  | ||||||
|    * @var ScalarSchema $schema |    * @var ScalarSchema $schema | ||||||
|    */ |    */ | ||||||
|   function normalize(&$value, Result $result, Schema $schema): bool { |   function normalize(&$value, Result $result, Schema $schema): bool { | ||||||
|  | |||||||
| @ -1,11 +1,10 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
|  | use nulib\schema\_scalar\ScalarSchema; | ||||||
|  | use nulib\schema\Result; | ||||||
|  | use nulib\schema\Schema; | ||||||
| use nulib\ValueException; | use nulib\ValueException; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarResult; |  | ||||||
| use nur\sery\wip\schema\_scalar\ScalarSchema; |  | ||||||
| use nur\sery\wip\schema\Result; |  | ||||||
| use nur\sery\wip\schema\Schema; |  | ||||||
| 
 | 
 | ||||||
| class tint extends _tformatable { | class tint extends _tformatable { | ||||||
|   const NAME = "int"; |   const NAME = "int"; | ||||||
| @ -26,6 +25,10 @@ class tint extends _tformatable { | |||||||
|     return "int"; |     return "int"; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   function getNullValue() { | ||||||
|  |     return $this->nullable? null: 0; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   function isValid($value, ?bool &$normalized=null): bool { |   function isValid($value, ?bool &$normalized=null): bool { | ||||||
|     $normalized = is_int($value); |     $normalized = is_int($value); | ||||||
|     return is_scalar($value); |     return is_scalar($value); | ||||||
| @ -42,7 +45,6 @@ class tint extends _tformatable { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
|    * @var ScalarResult $result |  | ||||||
|    * @var ScalarSchema $schema |    * @var ScalarSchema $schema | ||||||
|    */ |    */ | ||||||
|   function normalize(&$value, Result $result, Schema $schema): bool { |   function normalize(&$value, Result $result, Schema $schema): bool { | ||||||
|  | |||||||
| @ -1,10 +1,9 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| use nur\sery\wip\schema\_scalar\ScalarResult; | use nulib\schema\_scalar\ScalarSchema; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarSchema; | use nulib\schema\Result; | ||||||
| use nur\sery\wip\schema\Result; | use nulib\schema\Schema; | ||||||
| use nur\sery\wip\schema\Schema; |  | ||||||
| 
 | 
 | ||||||
| class tkey extends _tunion { | class tkey extends _tunion { | ||||||
|   const NAME = "key"; |   const NAME = "key"; | ||||||
| @ -23,6 +22,10 @@ class tkey extends _tunion { | |||||||
|     return "string|int"; |     return "string|int"; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   function getNullValue() { | ||||||
|  |     return $this->nullable? null: ""; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   function isValid($value, ?bool &$normalized=null): bool { |   function isValid($value, ?bool &$normalized=null): bool { | ||||||
|     $normalized = is_string($value) || is_int($value); |     $normalized = is_string($value) || is_int($value); | ||||||
|     return $normalized || is_scalar($value); |     return $normalized || is_scalar($value); | ||||||
| @ -33,7 +36,6 @@ class tkey extends _tunion { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
|    * @var ScalarResult $result |  | ||||||
|    * @var ScalarSchema $schema |    * @var ScalarSchema $schema | ||||||
|    */ |    */ | ||||||
|   function normalize(&$value, Result $result, Schema $schema): bool { |   function normalize(&$value, Result $result, Schema $schema): bool { | ||||||
|  | |||||||
| @ -1,11 +1,10 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| use nur\sery\wip\schema\_scalar\ScalarResult; | use nulib\schema\_scalar\ScalarSchema; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarSchema; | use nulib\schema\input\Input; | ||||||
| use nur\sery\wip\schema\input\Input; | use nulib\schema\Result; | ||||||
| use nur\sery\wip\schema\Result; | use nulib\schema\Schema; | ||||||
| use nur\sery\wip\schema\Schema; |  | ||||||
| 
 | 
 | ||||||
| class tmixed extends _tsimple { | class tmixed extends _tsimple { | ||||||
|   const NAME = "mixed"; |   const NAME = "mixed"; | ||||||
| @ -14,6 +13,15 @@ class tmixed extends _tsimple { | |||||||
|     return "mixed"; |     return "mixed"; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   function getMissingValue(?bool &$valid=null) { | ||||||
|  |     $valid = false; | ||||||
|  |     return null; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function getNullValue() { | ||||||
|  |     return null; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   function isAvailable(Input $input, $valueKey): bool { |   function isAvailable(Input $input, $valueKey): bool { | ||||||
|     return $input->isAvailable($valueKey); |     return $input->isAvailable($valueKey); | ||||||
|   } |   } | ||||||
| @ -32,7 +40,6 @@ class tmixed extends _tsimple { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
|    * @var ScalarResult $result |  | ||||||
|    * @var ScalarSchema $schema |    * @var ScalarSchema $schema | ||||||
|    */ |    */ | ||||||
|   function normalize(&$value, Result $result, Schema $schema): bool { |   function normalize(&$value, Result $result, Schema $schema): bool { | ||||||
|  | |||||||
| @ -1,10 +1,9 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| use nur\sery\wip\schema\_scalar\ScalarResult; | use nulib\schema\_scalar\ScalarSchema; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarSchema; | use nulib\schema\Result; | ||||||
| use nur\sery\wip\schema\Result; | use nulib\schema\Schema; | ||||||
| use nur\sery\wip\schema\Schema; |  | ||||||
| 
 | 
 | ||||||
| class tpkey extends _tunion { | class tpkey extends _tunion { | ||||||
|   const NAME = "pkey"; |   const NAME = "pkey"; | ||||||
| @ -28,6 +27,10 @@ class tpkey extends _tunion { | |||||||
|     return "string|int|array"; |     return "string|int|array"; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   function getNullValue() { | ||||||
|  |     return $this->nullable? null: []; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   function isValid($value, ?bool &$normalized=null): bool { |   function isValid($value, ?bool &$normalized=null): bool { | ||||||
|     $normalized = is_string($value) || is_int($value) || is_array($value); |     $normalized = is_string($value) || is_int($value) || is_array($value); | ||||||
|     return $normalized || is_scalar($value); |     return $normalized || is_scalar($value); | ||||||
| @ -38,7 +41,6 @@ class tpkey extends _tunion { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
|    * @var ScalarResult $result |  | ||||||
|    * @var ScalarSchema $schema |    * @var ScalarSchema $schema | ||||||
|    */ |    */ | ||||||
|   function normalize(&$value, Result $result, Schema $schema): bool { |   function normalize(&$value, Result $result, Schema $schema): bool { | ||||||
|  | |||||||
| @ -1,7 +1,7 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| use nur\sery\wip\schema\input\Input; | use nulib\schema\input\Input; | ||||||
| 
 | 
 | ||||||
| class traw extends tmixed { | class traw extends tmixed { | ||||||
|   const NAME = "raw"; |   const NAME = "raw"; | ||||||
|  | |||||||
| @ -1,11 +1,10 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
|  | use nulib\schema\_scalar\ScalarSchema; | ||||||
|  | use nulib\schema\Result; | ||||||
|  | use nulib\schema\Schema; | ||||||
| use nulib\str; | use nulib\str; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarResult; |  | ||||||
| use nur\sery\wip\schema\_scalar\ScalarSchema; |  | ||||||
| use nur\sery\wip\schema\Result; |  | ||||||
| use nur\sery\wip\schema\Schema; |  | ||||||
| 
 | 
 | ||||||
| class trawstring extends _tstring { | class trawstring extends _tstring { | ||||||
|   const NAME = "rawstring"; |   const NAME = "rawstring"; | ||||||
| @ -24,6 +23,10 @@ class trawstring extends _tstring { | |||||||
|     return "string"; |     return "string"; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   function getNullValue() { | ||||||
|  |     return $this->nullable? null: ""; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   function isNull($value): bool { |   function isNull($value): bool { | ||||||
|     return $value === null; |     return $value === null; | ||||||
|   } |   } | ||||||
| @ -41,7 +44,6 @@ class trawstring extends _tstring { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
|    * @var ScalarResult $result |  | ||||||
|    * @var ScalarSchema $schema |    * @var ScalarSchema $schema | ||||||
|    */ |    */ | ||||||
|   function normalize(&$value, Result $result, Schema $schema): bool { |   function normalize(&$value, Result $result, Schema $schema): bool { | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| class tstring extends trawstring { | class tstring extends trawstring { | ||||||
|   const NAME = "string"; |   const NAME = "string"; | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| class ttext extends trawstring { | class ttext extends trawstring { | ||||||
|   const NAME = "text"; |   const NAME = "text"; | ||||||
|  | |||||||
							
								
								
									
										23
									
								
								src/tools/Json2yamlApp.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/tools/Json2yamlApp.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,23 @@ | |||||||
|  | <?php | ||||||
|  | namespace nulib\tools; | ||||||
|  | 
 | ||||||
|  | use nulib\app\cli\Application; | ||||||
|  | use nulib\ext\json; | ||||||
|  | use nulib\ext\yaml; | ||||||
|  | use nulib\os\path; | ||||||
|  | 
 | ||||||
|  | class Json2yamlApp extends Application { | ||||||
|  |   private $args; | ||||||
|  | 
 | ||||||
|  |   function main() { | ||||||
|  |     $input = $this->args[0] ?? null; | ||||||
|  |     if ($input === null || $input === "-") { | ||||||
|  |       $output = null; | ||||||
|  |     } else { | ||||||
|  |       $output = path::ensure_ext($input, ".yml", ".json"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     $data = json::load($input); | ||||||
|  |     yaml::dump($data, $output); | ||||||
|  |   } | ||||||
|  | } | ||||||
							
								
								
									
										23
									
								
								src/tools/Yaml2jsonApp.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/tools/Yaml2jsonApp.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,23 @@ | |||||||
|  | <?php | ||||||
|  | namespace nulib\tools; | ||||||
|  | 
 | ||||||
|  | use nulib\app\cli\Application; | ||||||
|  | use nulib\ext\json; | ||||||
|  | use nulib\ext\yaml; | ||||||
|  | use nulib\os\path; | ||||||
|  | 
 | ||||||
|  | class Yaml2jsonApp extends Application { | ||||||
|  |   private $args; | ||||||
|  | 
 | ||||||
|  |   function main() { | ||||||
|  |     $input = $this->args[0] ?? null; | ||||||
|  |     if ($input === null || $input === "-") { | ||||||
|  |       $output = null; | ||||||
|  |     } else { | ||||||
|  |       $output = path::ensure_ext($input, ".json", [".yml", ".yaml"]); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     $data = yaml::load($input); | ||||||
|  |     json::dump($data, $output); | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\web\content; | namespace nulib\web\content; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| use nulib\A; | use nulib\A; | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\web\content; | namespace nulib\web\content; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class v: classe outil pour gérer du contenu pour le web |  * Class v: classe outil pour gérer du contenu pour le web | ||||||
|  | |||||||
| @ -1,5 +0,0 @@ | |||||||
| <?php |  | ||||||
| namespace nulib; |  | ||||||
| 
 |  | ||||||
| class app extends \nur\sery\app { |  | ||||||
| } |  | ||||||
| @ -1,5 +0,0 @@ | |||||||
| <?php |  | ||||||
| namespace nulib\app\cli; |  | ||||||
| 
 |  | ||||||
| abstract class Application extends \nur\sery\app\cli\Application { |  | ||||||
| } |  | ||||||
							
								
								
									
										132
									
								
								tests/appTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										132
									
								
								tests/appTest.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,132 @@ | |||||||
|  | <?php | ||||||
|  | namespace nulib { | ||||||
|  |   use nulib\tests\TestCase; | ||||||
|  |   use nulib\impl\config; | ||||||
|  |   use nulib\impl\myapp; | ||||||
|  |   use nulib\impl\MyApplication1; | ||||||
|  |   use nulib\impl\MyApplication2; | ||||||
|  | 
 | ||||||
|  |   class appTest extends TestCase { | ||||||
|  |     function testWith() { | ||||||
|  |       $projdir = config::get_projdir(); | ||||||
|  |       $cwd = getcwd(); | ||||||
|  | 
 | ||||||
|  |       myapp::reset(); | ||||||
|  |       $app1 = myapp::with(MyApplication1::class); | ||||||
|  |       self::assertSame([ | ||||||
|  |         "projdir" => $projdir, | ||||||
|  |         "vendor" => [ | ||||||
|  |           "bindir" => "$projdir/vendor/bin", | ||||||
|  |           "autoload" => "$projdir/vendor/autoload.php", | ||||||
|  |         ], | ||||||
|  |         "appcode" => "nur-sery", | ||||||
|  |         "cwd" => $cwd, | ||||||
|  |         "datadir" => "$projdir/devel", | ||||||
|  |         "etcdir" => "$projdir/devel/etc", | ||||||
|  |         "vardir" => "$projdir/devel/var", | ||||||
|  |         "logdir" => "$projdir/devel/log", | ||||||
|  |         "profile" => "devel", | ||||||
|  |         "appgroup" => null, | ||||||
|  |         "name" => "my-application1", | ||||||
|  |         "title" => null, | ||||||
|  |       ], $app1->getParams()); | ||||||
|  | 
 | ||||||
|  |       $app2 = myapp::with(MyApplication2::class, $app1); | ||||||
|  |       self::assertSame([ | ||||||
|  |         "projdir" => $projdir, | ||||||
|  |         "vendor" => [ | ||||||
|  |           "bindir" => "$projdir/vendor/bin", | ||||||
|  |           "autoload" => "$projdir/vendor/autoload.php", | ||||||
|  |         ], | ||||||
|  |         "appcode" => "nur-sery", | ||||||
|  |         "cwd" => $cwd, | ||||||
|  |         "datadir" => "$projdir/devel", | ||||||
|  |         "etcdir" => "$projdir/devel/etc", | ||||||
|  |         "vardir" => "$projdir/devel/var", | ||||||
|  |         "logdir" => "$projdir/devel/log", | ||||||
|  |         "profile" => "devel", | ||||||
|  |         "appgroup" => null, | ||||||
|  |         "name" => "my-application2", | ||||||
|  |         "title" => null, | ||||||
|  |       ], $app2->getParams()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     function testInit() { | ||||||
|  |       $projdir = config::get_projdir(); | ||||||
|  |       $cwd = getcwd(); | ||||||
|  | 
 | ||||||
|  |       myapp::reset(); | ||||||
|  |       myapp::init(MyApplication1::class); | ||||||
|  |       self::assertSame([ | ||||||
|  |         "projdir" => $projdir, | ||||||
|  |         "vendor" => [ | ||||||
|  |           "bindir" => "$projdir/vendor/bin", | ||||||
|  |           "autoload" => "$projdir/vendor/autoload.php", | ||||||
|  |         ], | ||||||
|  |         "appcode" => "nur-sery", | ||||||
|  |         "cwd" => $cwd, | ||||||
|  |         "datadir" => "$projdir/devel", | ||||||
|  |         "etcdir" => "$projdir/devel/etc", | ||||||
|  |         "vardir" => "$projdir/devel/var", | ||||||
|  |         "logdir" => "$projdir/devel/log", | ||||||
|  |         "profile" => "devel", | ||||||
|  |         "appgroup" => null, | ||||||
|  |         "name" => "my-application1", | ||||||
|  |         "title" => null, | ||||||
|  |       ], myapp::get()->getParams()); | ||||||
|  | 
 | ||||||
|  |       myapp::init(MyApplication2::class); | ||||||
|  |       self::assertSame([ | ||||||
|  |         "projdir" => $projdir, | ||||||
|  |         "vendor" => [ | ||||||
|  |           "bindir" => "$projdir/vendor/bin", | ||||||
|  |           "autoload" => "$projdir/vendor/autoload.php", | ||||||
|  |         ], | ||||||
|  |         "appcode" => "nur-sery", | ||||||
|  |         "cwd" => $cwd, | ||||||
|  |         "datadir" => "$projdir/devel", | ||||||
|  |         "etcdir" => "$projdir/devel/etc", | ||||||
|  |         "vardir" => "$projdir/devel/var", | ||||||
|  |         "logdir" => "$projdir/devel/log", | ||||||
|  |         "profile" => "devel", | ||||||
|  |         "appgroup" => null, | ||||||
|  |         "name" => "my-application2", | ||||||
|  |         "title" => null, | ||||||
|  |       ], myapp::get()->getParams()); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | namespace nulib\impl { | ||||||
|  | 
 | ||||||
|  |   use nulib\app\cli\Application; | ||||||
|  |   use nulib\os\path; | ||||||
|  |   use nulib\app; | ||||||
|  | 
 | ||||||
|  |   class config { | ||||||
|  |     const PROJDIR = __DIR__.'/..'; | ||||||
|  | 
 | ||||||
|  |     static function get_projdir(): string { | ||||||
|  |       return path::abspath(self::PROJDIR); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   class myapp extends app { | ||||||
|  |     static function reset(): void { | ||||||
|  |       self::$app = null; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   class MyApplication1 extends Application { | ||||||
|  |     const PROJDIR = config::PROJDIR; | ||||||
|  | 
 | ||||||
|  |     function main() { | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |   class MyApplication2 extends Application { | ||||||
|  |     const PROJDIR = null; | ||||||
|  | 
 | ||||||
|  |     function main() { | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\access; | namespace nulib\php\access; | ||||||
| 
 | 
 | ||||||
| use nulib\tests\TestCase; | use nulib\tests\TestCase; | ||||||
| use stdClass; | use stdClass; | ||||||
| @ -152,7 +152,7 @@ class KeyAccessTest extends TestCase { | |||||||
| 
 | 
 | ||||||
|   private function _ensureKeys(?array $orig, ?array $expected, array $defaults, ?array $params=null) { |   private function _ensureKeys(?array $orig, ?array $expected, array $defaults, ?array $params=null) { | ||||||
|     $v = $orig; $a = new KeyAccess($v); |     $v = $orig; $a = new KeyAccess($v); | ||||||
|     $a->ensureKeys($defaults, $params); |     $a->ensureKeys($defaults, $missings, $params); | ||||||
|     self::assertSame($expected, $v); |     self::assertSame($expected, $v); | ||||||
|   } |   } | ||||||
|   function testEnsureKeys() { |   function testEnsureKeys() { | ||||||
| @ -187,7 +187,7 @@ class KeyAccessTest extends TestCase { | |||||||
|     $v = $orig; $a = new KeyAccess($v); |     $v = $orig; $a = new KeyAccess($v); | ||||||
|     $keys = array_keys($defaults); |     $keys = array_keys($defaults); | ||||||
|     $a->ensureAssoc($keys, $params); |     $a->ensureAssoc($keys, $params); | ||||||
|     $a->ensureKeys($defaults, $params); |     $a->ensureKeys($defaults, $missings, $params); | ||||||
|     $a->ensureOrder($keys, $params); |     $a->ensureOrder($keys, $params); | ||||||
|     self::assertSame($expected, $v); |     self::assertSame($expected, $v); | ||||||
|   } |   } | ||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\coll; | namespace nulib\php\coll; | ||||||
| 
 | 
 | ||||||
| use Exception; | use Exception; | ||||||
| use nulib\cl; | use nulib\cl; | ||||||
| @ -1,9 +1,9 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\content; | namespace nulib\php\content; | ||||||
| 
 | 
 | ||||||
| use nulib\php\content\c; | use nulib\php\content\c; | ||||||
| use nur\sery\wip\php\content\impl\html; | use nulib\php\content\impl\html; | ||||||
| use nur\sery\wip\web\content\v; | use nulib\web\content\v; | ||||||
| use PHPUnit\Framework\TestCase; | use PHPUnit\Framework\TestCase; | ||||||
| 
 | 
 | ||||||
| class cTest extends TestCase { | class cTest extends TestCase { | ||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\content\impl; | namespace nulib\php\content\impl; | ||||||
| 
 | 
 | ||||||
| use nulib\php\content\IContent; | use nulib\php\content\IContent; | ||||||
| 
 | 
 | ||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\content\impl; | namespace nulib\php\content\impl; | ||||||
| 
 | 
 | ||||||
| use nulib\php\content\IPrintable; | use nulib\php\content\IPrintable; | ||||||
| 
 | 
 | ||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\content\impl; | namespace nulib\php\content\impl; | ||||||
| 
 | 
 | ||||||
| use nulib\php\content\c; | use nulib\php\content\c; | ||||||
| use nulib\php\content\IContent; | use nulib\php\content\IContent; | ||||||
| @ -1,5 +1,5 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\php\content\impl; | namespace nulib\php\content\impl; | ||||||
| 
 | 
 | ||||||
| class html { | class html { | ||||||
|   const H1 = [self::class, "h1"]; |   const H1 = [self::class, "h1"]; | ||||||
							
								
								
									
										396
									
								
								tests/schema/_assoc/AssocSchemaTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										396
									
								
								tests/schema/_assoc/AssocSchemaTest.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,396 @@ | |||||||
|  | <?php | ||||||
|  | namespace nulib\schema\_assoc; | ||||||
|  | 
 | ||||||
|  | use nulib\ext\yaml; | ||||||
|  | use nulib\tests\TestCase; | ||||||
|  | use nulib\ValueException; | ||||||
|  | use nulib\schema\_scalar\ScalarSchemaTest; | ||||||
|  | use nulib\schema\Schema; | ||||||
|  | use PHPStan\Type\Enum\EnumCaseObjectType; | ||||||
|  | 
 | ||||||
|  | class AssocSchemaTest extends TestCase { | ||||||
|  |   const NULL_SCHEMA = [ | ||||||
|  |     "" => [ | ||||||
|  |       "assoc", | ||||||
|  |       "compute_func" => null, | ||||||
|  |       "validate_func" => null, | ||||||
|  |       "ensure_array" => null, | ||||||
|  |       "ensure_assoc" => null, | ||||||
|  |       "ensure_keys" => null, | ||||||
|  |       "ensure_order" => null, | ||||||
|  |     ], | ||||||
|  |     "schema" => null, | ||||||
|  |     "type" => [null], | ||||||
|  |     "default" => null, | ||||||
|  |     "title" => null, | ||||||
|  |     "required" => false, | ||||||
|  |     "nullable" => true, | ||||||
|  |     "desc" => null, | ||||||
|  |     "analyzer_func" => null, | ||||||
|  |     "extractor_func" => null, | ||||||
|  |     "parser_func" => null, | ||||||
|  |     "normalizer_func" => null, | ||||||
|  |     "messages" => null, | ||||||
|  |     "formatter_func" => null, | ||||||
|  |     "format" => null, | ||||||
|  |     "size" => null, | ||||||
|  |     "precision" => null, | ||||||
|  |     "name" => null, | ||||||
|  |     "pkey" => null, | ||||||
|  |     "header" => null, | ||||||
|  |     "computed" => null, | ||||||
|  |   ]; | ||||||
|  | 
 | ||||||
|  |   static function schema(array $definition, array $keyDefinitions): array { | ||||||
|  |     $definition = array_merge(self::NULL_SCHEMA, $definition, ["schema" => []]); | ||||||
|  |     foreach ($keyDefinitions as $key => $keydef) { | ||||||
|  |       $definition["schema"][$key] = array_merge(ScalarSchemaTest::NULL_SCHEMA, $keydef); | ||||||
|  |     }; unset($subdef); | ||||||
|  |     return $definition; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function testNormalize() { | ||||||
|  |     self::assertSame(self::schema([ | ||||||
|  |       "type" => ["array"], "nullable" => true, | ||||||
|  |     ], [ | ||||||
|  |       "s" => [ | ||||||
|  |         "type" => ["string"], "nullable" => false, | ||||||
|  |         "name" => "s", "pkey" => "s", "header" => "s", | ||||||
|  |       ], | ||||||
|  |     ]), AssocSchema::normalize_definition(["s" => "string"])); | ||||||
|  | 
 | ||||||
|  |     self::assertSame(self::schema([ | ||||||
|  |       "type" => ["array"], "nullable" => true, | ||||||
|  |     ], [ | ||||||
|  |       "s" => [ | ||||||
|  |         "type" => ["string"], "nullable" => false, | ||||||
|  |         "name" => "s", "pkey" => "s", "header" => "s", | ||||||
|  |       ], | ||||||
|  |       "i" => [ | ||||||
|  |         "type" => ["int"], "nullable" => false, | ||||||
|  |         "name" => "i", "pkey" => "i", "header" => "i", | ||||||
|  |       ], | ||||||
|  |       "b" => [ | ||||||
|  |         "type" => ["bool"], "nullable" => false, | ||||||
|  |         "name" => "b", "pkey" => "b", "header" => "b", | ||||||
|  |       ], | ||||||
|  |     ]), AssocSchema::normalize_definition([ | ||||||
|  |       "s" => "string", | ||||||
|  |       "i" => "int", | ||||||
|  |       "b" => "bool", | ||||||
|  |     ])); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function testConstructor() { | ||||||
|  |     $schema = new AssocSchema([ | ||||||
|  |       "s" => "string", | ||||||
|  |       "i" => "int", | ||||||
|  |       "b" => "bool", | ||||||
|  |     ]); | ||||||
|  |     self::assertSame(self::schema([ | ||||||
|  |       "type" => ["array"], "nullable" => true, | ||||||
|  |     ], [ | ||||||
|  |       "s" => [ | ||||||
|  |         "type" => ["string"], "nullable" => false, | ||||||
|  |         "name" => "s", "pkey" => "s", "header" => "s", | ||||||
|  |       ], | ||||||
|  |       "i" => [ | ||||||
|  |         "type" => ["int"], "nullable" => false, | ||||||
|  |         "name" => "i", "pkey" => "i", "header" => "i", | ||||||
|  |       ], | ||||||
|  |       "b" => [ | ||||||
|  |         "type" => ["bool"], "nullable" => false, | ||||||
|  |         "name" => "b", "pkey" => "b", "header" => "b", | ||||||
|  |       ], | ||||||
|  |     ]), $schema->getDefinition()); | ||||||
|  |     //yaml::dump($schema->getDefinition());
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function testWrapper() { | ||||||
|  |     $schema = new AssocSchema([ | ||||||
|  |       "s" => "?string", | ||||||
|  |       "i" => "?int", | ||||||
|  |       "b" => "?bool", | ||||||
|  |     ]); | ||||||
|  |     $array = ["s" => "  string  ", "i" => "  42 ", "b" => false]; | ||||||
|  |     $schema->getWrapper($array); | ||||||
|  |     self::assertSame([ | ||||||
|  |       "s" => "string", | ||||||
|  |       "i" => 42, | ||||||
|  |       "b" => false, | ||||||
|  |     ], $array); | ||||||
|  | 
 | ||||||
|  |     ###########################################################################
 | ||||||
|  |     $schema = new AssocSchema([ | ||||||
|  |       "s" => "string", | ||||||
|  |       "i" => "int", | ||||||
|  |       "b" => "bool", | ||||||
|  |     ]); | ||||||
|  | 
 | ||||||
|  |     $array = ["s" => "  string  "]; | ||||||
|  |     $schema->getWrapper($array); | ||||||
|  |     self::assertSame([ | ||||||
|  |       "s" => "string", | ||||||
|  |       "i" => 0, | ||||||
|  |       "b" => false, | ||||||
|  |     ], $array); | ||||||
|  | 
 | ||||||
|  |     $array = ["b" => false, "s" => "  string  "]; | ||||||
|  |     $schema->getWrapper($array); | ||||||
|  |     self::assertSame([ | ||||||
|  |       "s" => "string", | ||||||
|  |       "i" => 0, | ||||||
|  |       "b" => false, | ||||||
|  |     ], $array); | ||||||
|  | 
 | ||||||
|  |     $array = ["s" => "  string  "]; | ||||||
|  |     $schema->getWrapper($array, null, ["ensure_order" => false]); | ||||||
|  |     self::assertSame([ | ||||||
|  |       "s" => "string", | ||||||
|  |       "i" => 0, | ||||||
|  |       "b" => false, | ||||||
|  |     ], $array); | ||||||
|  | 
 | ||||||
|  |     $array = ["b" => false, "s" => "  string  "]; | ||||||
|  |     $schema->getWrapper($array, null, ["ensure_order" => false]); | ||||||
|  |     self::assertSame([ | ||||||
|  |       "b" => false, | ||||||
|  |       "s" => "string", | ||||||
|  |       "i" => 0, | ||||||
|  |     ], $array); | ||||||
|  | 
 | ||||||
|  |     $array = ["s" => "  string  "]; | ||||||
|  |     $schema->getWrapper($array, null, ["ensure_keys" => false]); | ||||||
|  |     self::assertSame([ | ||||||
|  |       "s" => "string", | ||||||
|  |     ], $array); | ||||||
|  | 
 | ||||||
|  |     $array = ["b" => false, "s" => "  string  "]; | ||||||
|  |     $schema->getWrapper($array, null, ["ensure_keys" => false]); | ||||||
|  |     self::assertSame([ | ||||||
|  |       "s" => "string", | ||||||
|  |       "b" => false, | ||||||
|  |     ], $array); | ||||||
|  | 
 | ||||||
|  |     // false équivaut à absent, sauf pour "b" qui est de type bool
 | ||||||
|  |     $array = ["s" => false, "i" => false, "b" => false]; | ||||||
|  |     $schema->getWrapper($array, null, ["ensure_keys" => true]); | ||||||
|  |     self::assertSame([ | ||||||
|  |       "s" => "", | ||||||
|  |       "i" => 0, | ||||||
|  |       "b" => false, | ||||||
|  |     ], $array); | ||||||
|  | 
 | ||||||
|  |     $array = ["s" => false, "i" => false, "b" => false]; | ||||||
|  |     $schema->getWrapper($array, null, ["ensure_keys" => false]); | ||||||
|  |     self::assertSame([ | ||||||
|  |       "b" => false, | ||||||
|  |     ], $array); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   const STRING_SCHEMA = [ | ||||||
|  |     "s" => "string", | ||||||
|  |     "f" => "string", | ||||||
|  |     "m" => "string", | ||||||
|  |   ]; | ||||||
|  | 
 | ||||||
|  |   const NSTRING_SCHEMA = [ | ||||||
|  |     "s" => "?string", | ||||||
|  |     "f" => "?string", | ||||||
|  |     "m" => "?string", | ||||||
|  |   ]; | ||||||
|  | 
 | ||||||
|  |   const RSTRING_SCHEMA = [ | ||||||
|  |     "s" => ["string", "required" => true], | ||||||
|  |     "f" => ["string", "required" => true], | ||||||
|  |     "m" => ["string", "required" => true], | ||||||
|  |   ]; | ||||||
|  | 
 | ||||||
|  |   const RNSTRING_SCHEMA = [ | ||||||
|  |     "s" => ["?string", "required" => true], | ||||||
|  |     "f" => ["?string", "required" => true], | ||||||
|  |     "m" => ["?string", "required" => true], | ||||||
|  |   ]; | ||||||
|  | 
 | ||||||
|  |   const STRINGS = ["s" => "string", "f" => false]; | ||||||
|  |   const NSTRINGS = ["s" => null, "f" => null]; | ||||||
|  | 
 | ||||||
|  |   function testString() { | ||||||
|  |     /** @var AssocSchema $schema */ | ||||||
|  |     $schema = Schema::ns(self::STRING_SCHEMA); | ||||||
|  | 
 | ||||||
|  |     $array = self::STRINGS; | ||||||
|  |     $wrapper = $schema->getWrapper($array, null, ["throw" => false]); | ||||||
|  |     self::assertSame(["s" => "string", "f" => "", "m" => ""], $array); | ||||||
|  |     $result = $wrapper->getResult("s"); | ||||||
|  |     self::assertTrue($result->normalized); | ||||||
|  |     $result = $wrapper->getResult("f"); | ||||||
|  |     self::assertTrue($result->present); | ||||||
|  |     self::assertFalse($result->available); | ||||||
|  |     $result = $wrapper->getResult("m"); | ||||||
|  |     self::assertFalse($result->present); | ||||||
|  | 
 | ||||||
|  |     self::assertNotException(function() use ($schema) { | ||||||
|  |       $array = self::STRINGS; | ||||||
|  |       $schema->getWrapper($array); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     $array = self::NSTRINGS; | ||||||
|  |     $wrapper = $schema->getWrapper($array, null, ["throw" => false]); | ||||||
|  |     self::assertSame(["s" => null, "f" => null, "m" => ""], $array); | ||||||
|  |     $result = $wrapper->getResult("s"); | ||||||
|  |     self::assertFalse($result->valid); | ||||||
|  |     self::assertSame("null", $result->messageKey); | ||||||
|  |     $result = $wrapper->getResult("f"); | ||||||
|  |     self::assertFalse($result->valid); | ||||||
|  |     self::assertSame("null", $result->messageKey); | ||||||
|  |     $result = $wrapper->getResult("m"); | ||||||
|  |     self::assertFalse($result->present); | ||||||
|  | 
 | ||||||
|  |     self::assertException(ValueException::class, function() use ($schema) { | ||||||
|  |       $array = self::NSTRINGS; | ||||||
|  |       $schema->getWrapper($array); | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function testNstring() { | ||||||
|  |     /** @var AssocSchema $schema */ | ||||||
|  |     $schema = Schema::ns(self::NSTRING_SCHEMA); | ||||||
|  | 
 | ||||||
|  |     $array = self::STRINGS; | ||||||
|  |     $wrapper = $schema->getWrapper($array, null, ["throw" => false]); | ||||||
|  |     self::assertSame(["s" => "string", "f" => null, "m" => null], $array); | ||||||
|  |     $result = $wrapper->getResult("s"); | ||||||
|  |     self::assertTrue($result->normalized); | ||||||
|  |     $result = $wrapper->getResult("f"); | ||||||
|  |     self::assertTrue($result->present); | ||||||
|  |     self::assertFalse($result->available); | ||||||
|  |     $result = $wrapper->getResult("m"); | ||||||
|  |     self::assertFalse($result->present); | ||||||
|  | 
 | ||||||
|  |     self::assertNotException(function() use ($schema) { | ||||||
|  |       $array = self::STRINGS; | ||||||
|  |       $schema->getWrapper($array); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     $array = self::NSTRINGS; | ||||||
|  |     $wrapper = $schema->getWrapper($array, null, ["throw" => false]); | ||||||
|  |     self::assertSame(["s" => null, "f" => null, "m" => null], $array); | ||||||
|  |     $result = $wrapper->getResult("s"); | ||||||
|  |     self::assertTrue($result->normalized); | ||||||
|  |     $result = $wrapper->getResult("f"); | ||||||
|  |     self::assertTrue($result->normalized); | ||||||
|  |     $result = $wrapper->getResult("m"); | ||||||
|  |     self::assertFalse($result->present); | ||||||
|  | 
 | ||||||
|  |     self::assertNotException(function() use ($schema) { | ||||||
|  |       $array = self::NSTRINGS; | ||||||
|  |       $schema->getWrapper($array); | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function testRstring() { | ||||||
|  |     /** @var AssocSchema $schema */ | ||||||
|  |     $schema = Schema::ns(self::RSTRING_SCHEMA); | ||||||
|  | 
 | ||||||
|  |     $array = self::STRINGS; | ||||||
|  |     $wrapper = $schema->getWrapper($array, null, ["throw" => false]); | ||||||
|  |     self::assertSame(["s" => "string", "f" => "", "m" => ""], $array); | ||||||
|  |     $result = $wrapper->getResult("s"); | ||||||
|  |     self::assertTrue($result->normalized); | ||||||
|  |     $result = $wrapper->getResult("f"); | ||||||
|  |     self::assertTrue($result->present); | ||||||
|  |     self::assertFalse($result->available); | ||||||
|  |     self::assertSame("unavailable", $result->messageKey); | ||||||
|  |     $result = $wrapper->getResult("m"); | ||||||
|  |     self::assertFalse($result->present); | ||||||
|  |     self::assertSame("missing", $result->messageKey); | ||||||
|  | 
 | ||||||
|  |     self::assertException(ValueException::class, function() use ($schema) { | ||||||
|  |       $array = self::STRINGS; | ||||||
|  |       $schema->getWrapper($array); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     $array = self::NSTRINGS; | ||||||
|  |     $wrapper = $schema->getWrapper($array, null, ["throw" => false]); | ||||||
|  |     self::assertSame(["s" => null, "f" => null, "m" => ""], $array); | ||||||
|  |     $result = $wrapper->getResult("s"); | ||||||
|  |     self::assertFalse($result->valid); | ||||||
|  |     self::assertSame("null", $result->messageKey); | ||||||
|  |     $result = $wrapper->getResult("f"); | ||||||
|  |     self::assertFalse($result->valid); | ||||||
|  |     self::assertSame("null", $result->messageKey); | ||||||
|  |     $result = $wrapper->getResult("m"); | ||||||
|  |     self::assertFalse($result->present); | ||||||
|  |     self::assertSame("missing", $result->messageKey); | ||||||
|  | 
 | ||||||
|  |     self::assertException(ValueException::class, function() use ($schema) { | ||||||
|  |       $array = self::NSTRINGS; | ||||||
|  |       $schema->getWrapper($array); | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function testRnstring() { | ||||||
|  |     /** @var AssocSchema $schema */ | ||||||
|  |     $schema = Schema::ns(self::RNSTRING_SCHEMA); | ||||||
|  | 
 | ||||||
|  |     $array = self::STRINGS; | ||||||
|  |     $wrapper = $schema->getWrapper($array, null, ["throw" => false]); | ||||||
|  |     self::assertSame(["s" => "string", "f" => null, "m" => null], $array); | ||||||
|  |     $result = $wrapper->getResult("s"); | ||||||
|  |     self::assertTrue($result->normalized); | ||||||
|  |     $result = $wrapper->getResult("f"); | ||||||
|  |     self::assertTrue($result->present); | ||||||
|  |     self::assertFalse($result->available); | ||||||
|  |     self::assertSame("unavailable", $result->messageKey); | ||||||
|  |     $result = $wrapper->getResult("m"); | ||||||
|  |     self::assertFalse($result->present); | ||||||
|  |     self::assertSame("missing", $result->messageKey); | ||||||
|  | 
 | ||||||
|  |     self::assertException(ValueException::class, function() use ($schema) { | ||||||
|  |       $array = self::STRINGS; | ||||||
|  |       $schema->getWrapper($array); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     $array = self::NSTRINGS; | ||||||
|  |     $wrapper = $schema->getWrapper($array, null, ["throw" => false]); | ||||||
|  |     self::assertSame(["s" => null, "f" => null, "m" => null], $array); | ||||||
|  |     $result = $wrapper->getResult("s"); | ||||||
|  |     self::assertTrue($result->normalized); | ||||||
|  |     $result = $wrapper->getResult("f"); | ||||||
|  |     self::assertTrue($result->normalized); | ||||||
|  |     $result = $wrapper->getResult("m"); | ||||||
|  |     self::assertFalse($result->present); | ||||||
|  |     self::assertSame("missing", $result->messageKey); | ||||||
|  | 
 | ||||||
|  |     self::assertException(ValueException::class, function() use ($schema) { | ||||||
|  |       $array = self::NSTRINGS; | ||||||
|  |       $schema->getWrapper($array); | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function testMessage() { | ||||||
|  |     $schema = new AssocSchema([ | ||||||
|  |       "rs" => ["string", "required" => true], | ||||||
|  |       "i" => ["int"], | ||||||
|  |     ]); | ||||||
|  | 
 | ||||||
|  |     $value = []; | ||||||
|  |     $result = $schema->getWrapper($value, null, ["throw" => false])->getResult(null); | ||||||
|  |     $expectedMessage = <<<EOT | ||||||
|  | rs: vous devez spécifier cette valeur | ||||||
|  | EOT; | ||||||
|  |     self::assertSame($expectedMessage, $result->message); | ||||||
|  | 
 | ||||||
|  |     $value = [ | ||||||
|  |       "rs" => null, | ||||||
|  |       "i" => "abc", | ||||||
|  |     ]; | ||||||
|  |     $result = $schema->getWrapper($value, null, ["throw" => false])->getResult(null); | ||||||
|  |     $expectedMessage = <<<EOT | ||||||
|  | rs: cette valeur ne doit pas être nulle | ||||||
|  | i: une valeur numérique entière est attendue | ||||||
|  | EOT; | ||||||
|  |     self::assertSame($expectedMessage, $result->message); | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -1,8 +1,8 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\_scalar; | namespace nulib\schema\_scalar; | ||||||
| 
 | 
 | ||||||
| use nulib\tests\TestCase; | use nulib\tests\TestCase; | ||||||
| use nur\sery\wip\schema\SchemaException; | use nulib\schema\SchemaException; | ||||||
| 
 | 
 | ||||||
| class ScalarSchemaTest extends TestCase { | class ScalarSchemaTest extends TestCase { | ||||||
|   const NULL_SCHEMA = [ |   const NULL_SCHEMA = [ | ||||||
| @ -19,6 +19,8 @@ class ScalarSchemaTest extends TestCase { | |||||||
|     "messages" => null, |     "messages" => null, | ||||||
|     "formatter_func" => null, |     "formatter_func" => null, | ||||||
|     "format" => null, |     "format" => null, | ||||||
|  |     "size" => null, | ||||||
|  |     "precision" => null, | ||||||
|     "" => [ |     "" => [ | ||||||
|       "scalar", |       "scalar", | ||||||
|       "compute_func" => null, |       "compute_func" => null, | ||||||
							
								
								
									
										296
									
								
								tests/schema/_scalar/ScalarWrapperTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										296
									
								
								tests/schema/_scalar/ScalarWrapperTest.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,296 @@ | |||||||
|  | <?php | ||||||
|  | namespace nulib\schema\_scalar; | ||||||
|  | 
 | ||||||
|  | use nulib\tests\TestCase; | ||||||
|  | use nulib\ValueException; | ||||||
|  | use nulib\schema\input\Input; | ||||||
|  | use stdClass; | ||||||
|  | 
 | ||||||
|  | class ScalarWrapperTest extends TestCase { | ||||||
|  |   function checkValue(ScalarWrapper $wrapper, $value, bool $present, bool $available, bool $valid, bool $normalized): void { | ||||||
|  |     self::assertSame($value, $wrapper->get(), "value"); | ||||||
|  |     self::assertSame($present, $wrapper->isPresent(), "present"); | ||||||
|  |     self::assertSame($available, $wrapper->isAvailable(), "available"); | ||||||
|  |     self::assertSame($valid, $wrapper->isValid(), "valid"); | ||||||
|  |     self::assertSame($normalized, $wrapper->isNormalized(), "normalized"); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function checkNormalize(ScalarSchema $schema, $orig, bool $normalize, $value, bool $present, bool $available, bool $valid, bool $normalized, ?array $inputParams=null): void { | ||||||
|  |     $wrapper = $schema->getWrapper(); | ||||||
|  |     $wrapper->resetParams(["normalize" => $normalize]); | ||||||
|  |     if ($inputParams !== null) $input = new Input($orig, $inputParams); | ||||||
|  |     else $input = $orig; | ||||||
|  |     $wrapper->reset($input); | ||||||
|  |     $this->checkValue($wrapper, $value, $present, $available, $valid, $normalized); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function checkException(ScalarSchema $schema, $orig, bool $normalize, string $exceptionClass, ?array $inputParams=null) { | ||||||
|  |     $wrapper = $schema->getWrapper(); | ||||||
|  |     if ($inputParams !== null) $orig = new Input($orig, $inputParams); | ||||||
|  |     self::assertException($exceptionClass, function() use ($wrapper, &$orig, $normalize) { | ||||||
|  |       $wrapper->resetParams(["normalize" => $normalize]); | ||||||
|  |       $wrapper->reset($orig); | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function testRaw() { | ||||||
|  |     $schema = new ScalarSchema(); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, false, false, false, true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, false, true, false, true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, null, false, null, true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, null, true, null, true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     $obj = new stdClass(); | ||||||
|  |     $this->checkNormalize($schema, $obj, false, $obj, true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, $obj, true, $obj, true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     $schema = new ScalarSchema("raw"); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, false, false, false, true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, false, true, false, true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, null, false, null, true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, null, true, null, true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     $obj = new stdClass(); | ||||||
|  |     $this->checkNormalize($schema, $obj, false, $obj, true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, $obj, true, $obj, true, true, true, true); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function testMixed() { | ||||||
|  |     $schema = new ScalarSchema("mixed"); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, false, false, false, true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, false, true, false, true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, null, false, null, true, true, false, false); | ||||||
|  |     $this->checkException($schema, null, true, ValueException::class); | ||||||
|  | 
 | ||||||
|  |     $obj = new stdClass(); | ||||||
|  |     $this->checkNormalize($schema, $obj, false, $obj, true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, $obj, true, $obj, true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     $schema = new ScalarSchema("?mixed"); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, false, false, false, true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, false, true, false, true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, null, false, null, true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, null, true, null, true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     $obj = new stdClass(); | ||||||
|  |     $this->checkNormalize($schema, $obj, false, $obj, true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, $obj, true, $obj, true, true, true, true); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function testRawstring() { | ||||||
|  |     $schema = new ScalarSchema("rawstring"); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, false, false, null, true, false, true, true); | ||||||
|  |     $this->checkNormalize($schema, false, true, null, true, false, true, true); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, null, false, null, true, true, false, false); | ||||||
|  |     $this->checkException($schema, null, true, ValueException::class); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "", false, "", true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, "", true, "", true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "   ", false, "   ", true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, "   ", true, "   ", true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "text", false, "text", true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, "text", true, "text", true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "  text  ", false, "  text  ", true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, "  text  ", true, "  text  ", true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, true, false, true, true, true, true, false); | ||||||
|  |     $this->checkNormalize($schema, true, true, "1", true, true, true, false); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, 42, false, 42, true, true, true, false); | ||||||
|  |     $this->checkNormalize($schema, 42, true, "42", true, true, true, false); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, [], false, [], true, true, false, false); | ||||||
|  |     $this->checkException($schema, [], true, ValueException::class); | ||||||
|  | 
 | ||||||
|  |     ## Tester valeur par défaut
 | ||||||
|  |     $schema = new ScalarSchema(["rawstring", null]); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, false, false, null, true, false, true, true); | ||||||
|  |     $this->checkNormalize($schema, false, true, null, true, false, true, true); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, null, false, null, true, true, false, false); | ||||||
|  |     $this->checkException($schema, null, true, ValueException::class); | ||||||
|  | 
 | ||||||
|  |     $schema = new ScalarSchema(["rawstring", "default"]); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, false, false, "default", true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, false, true, "default", true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, null, false, null, true, true, false, false); | ||||||
|  |     $this->checkException($schema, null, true, ValueException::class); | ||||||
|  | 
 | ||||||
|  |     ## Tester nullable
 | ||||||
|  |     $schema = new ScalarSchema("?rawstring"); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, null, false, null, true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, null, true, null, true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     ## Tester required
 | ||||||
|  |     $schema = new ScalarSchema(["rawstring", "required" => true]); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, false, false, null, true, false, false, false); | ||||||
|  |     $this->checkException($schema, false, true, ValueException::class); | ||||||
|  | 
 | ||||||
|  |     ## Tester allow_empty === false
 | ||||||
|  |     $inputParams = ["allow_empty" => false]; | ||||||
|  |     $schema = new ScalarSchema("rawstring"); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, null, false, null, true, true, false, false, $inputParams); | ||||||
|  |     $this->checkException($schema, null, true, ValueException::class, $inputParams); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "", false, null, true, false, true, true, $inputParams); | ||||||
|  |     $this->checkNormalize($schema, "", true, null, true, false, true, true, $inputParams); | ||||||
|  | 
 | ||||||
|  |     $schema = new ScalarSchema("?rawstring"); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, null, false, null, true, true, true, true, $inputParams); | ||||||
|  |     $this->checkNormalize($schema, null, true, null, true, true, true, true, $inputParams); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "", false, null, true, false, true, true, $inputParams); | ||||||
|  |     $this->checkNormalize($schema, "", true, null, true, false, true, true, $inputParams); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function testString() { | ||||||
|  |     $schema = new ScalarSchema("string"); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, false, false, null, true, false, true, true); | ||||||
|  |     $this->checkNormalize($schema, false, true, null, true, false, true, true); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, null, false, null, true, true, false, false); | ||||||
|  |     $this->checkException($schema, null, true, ValueException::class); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "", false, "", true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, "", true, "", true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "   ", false, "", true, true, true, false); | ||||||
|  |     $this->checkNormalize($schema, "   ", true, "", true, true, true, false); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "text", false, "text", true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, "text", true, "text", true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "  text  ", false, "text", true, true, true, false); | ||||||
|  |     $this->checkNormalize($schema, "  text  ", true, "text", true, true, true, false); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, true, false, true, true, true, true, false); | ||||||
|  |     $this->checkNormalize($schema, true, true, "1", true, true, true, false); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, 42, false, 42, true, true, true, false); | ||||||
|  |     $this->checkNormalize($schema, 42, true, "42", true, true, true, false); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, [], false, [], true, true, false, false); | ||||||
|  |     $this->checkException($schema, [], true, ValueException::class); | ||||||
|  | 
 | ||||||
|  |     ## Tester nullable
 | ||||||
|  |     $schema = new ScalarSchema("?string"); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, null, false, null, true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, null, true, null, true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     ## Tester required
 | ||||||
|  |     $schema = new ScalarSchema(["string", "required" => true]); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, false, false, null, true, false, false, false); | ||||||
|  |     $this->checkException($schema, false, true, ValueException::class); | ||||||
|  | 
 | ||||||
|  |     ## Tester allow_empty === false
 | ||||||
|  |     $inputParams = ["allow_empty" => false]; | ||||||
|  |     $schema = new ScalarSchema("string"); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, null, false, null, true, true, false, false, $inputParams); | ||||||
|  |     $this->checkException($schema, null, true, ValueException::class, $inputParams); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "", false, null, true, false, true, true, $inputParams); | ||||||
|  |     $this->checkNormalize($schema, "", true, null, true, false, true, true, $inputParams); | ||||||
|  | 
 | ||||||
|  |     $schema = new ScalarSchema("?string"); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, null, false, null, true, true, true, true, $inputParams); | ||||||
|  |     $this->checkNormalize($schema, null, true, null, true, true, true, true, $inputParams); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "", false, null, true, false, true, true, $inputParams); | ||||||
|  |     $this->checkNormalize($schema, "", true, null, true, false, true, true, $inputParams); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function testInt() { | ||||||
|  |     $schema = new ScalarSchema("int"); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, false, false, null, true, false, true, true); | ||||||
|  |     $this->checkNormalize($schema, false, true, null, true, false, true, true); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, null, false, null, true, true, false, false); | ||||||
|  |     $this->checkException($schema, null, true, ValueException::class); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, 42, false, 42, true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, 42, true, 42, true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "42", false, "42", true, true, true, false); | ||||||
|  |     $this->checkNormalize($schema, "42", true, 42, true, true, true, false); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "42.5", false, "42.5", true, true, true, false); | ||||||
|  |     $this->checkNormalize($schema, "42.5", true, 42, true, true, true, false); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "42,5", false, "42,5", true, true, true, false); | ||||||
|  |     $this->checkNormalize($schema, "42,5", true, 42, true, true, true, false); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "  42  ", false, "42", true, true, true, false); | ||||||
|  |     $this->checkNormalize($schema, "  42  ", true, 42, true, true, true, false); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "", false, "", true, true, false, false); | ||||||
|  |     $this->checkException($schema, "", true, ValueException::class); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "   ", false, "   ", true, true, false, false); | ||||||
|  |     $this->checkException($schema, "   ", true, ValueException::class); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "text", false, "text", true, true, false, false); | ||||||
|  |     $this->checkException($schema, "text", true, ValueException::class); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, true, false, true, true, true, true, false); | ||||||
|  |     $this->checkNormalize($schema, true, true, 1, true, true, true, false); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, [], false, [], true, true, false, false); | ||||||
|  |     $this->checkException($schema, [], true, ValueException::class); | ||||||
|  | 
 | ||||||
|  |     ## Tester nullable
 | ||||||
|  |     $schema = new ScalarSchema("?int"); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, null, false, null, true, true, true, true); | ||||||
|  |     $this->checkNormalize($schema, null, true, null, true, true, true, true); | ||||||
|  | 
 | ||||||
|  |     ## Tester required
 | ||||||
|  |     $schema = new ScalarSchema(["int", "required" => true]); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, false, false, null, true, false, false, false); | ||||||
|  |     $this->checkException($schema, false, true, ValueException::class); | ||||||
|  | 
 | ||||||
|  |     ## Tester allow_empty === false
 | ||||||
|  |     $inputParams = ["allow_empty" => false]; | ||||||
|  |     $schema = new ScalarSchema("int"); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, null, false, null, true, true, false, false, $inputParams); | ||||||
|  |     $this->checkException($schema, null, true, ValueException::class, $inputParams); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "", false, null, true, false, true, true, $inputParams); | ||||||
|  |     $this->checkNormalize($schema, "", true, null, true, false, true, true, $inputParams); | ||||||
|  | 
 | ||||||
|  |     $schema = new ScalarSchema("?int"); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, null, false, null, true, true, true, true, $inputParams); | ||||||
|  |     $this->checkNormalize($schema, null, true, null, true, true, true, true, $inputParams); | ||||||
|  | 
 | ||||||
|  |     $this->checkNormalize($schema, "", false, null, true, false, true, true, $inputParams); | ||||||
|  |     $this->checkNormalize($schema, "", true, null, true, false, true, true, $inputParams); | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -1,10 +1,10 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| use Exception; | use Exception; | ||||||
| use nulib\tests\TestCase; | use nulib\tests\TestCase; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarWrapper; | use nulib\schema\_scalar\ScalarWrapper; | ||||||
| use nur\sery\wip\schema\Schema; | use nulib\schema\Schema; | ||||||
| 
 | 
 | ||||||
| class boolTest extends TestCase { | class boolTest extends TestCase { | ||||||
|   function commonTests($wrapper, &$value, callable $wrapperSetter): void { |   function commonTests($wrapper, &$value, callable $wrapperSetter): void { | ||||||
| @ -59,7 +59,7 @@ class boolTest extends TestCase { | |||||||
| 
 | 
 | ||||||
|   function testBool() { |   function testBool() { | ||||||
|     /** @var ScalarWrapper $wrapper */ |     /** @var ScalarWrapper $wrapper */ | ||||||
|     Schema::nw($value, null, $schema, "bool", $wrapper); |     Schema::nw($value, null, "bool", $schema, $wrapper); | ||||||
|     $wrapperSetter = function($value) use($wrapper) { |     $wrapperSetter = function($value) use($wrapper) { | ||||||
|       return function() use($wrapper, $value) { |       return function() use($wrapper, $value) { | ||||||
|         $wrapper->set($value); |         $wrapper->set($value); | ||||||
| @ -75,7 +75,7 @@ class boolTest extends TestCase { | |||||||
| 
 | 
 | ||||||
|   function testNbool() { |   function testNbool() { | ||||||
|     /** @var ScalarWrapper $wrapper */ |     /** @var ScalarWrapper $wrapper */ | ||||||
|     Schema::nw($value, null, $schema, "?bool", $wrapper); |     Schema::nw($value, null, "?bool", $schema, $wrapper); | ||||||
|     $wrapperSetter = function($value) use($wrapper) { |     $wrapperSetter = function($value) use($wrapper) { | ||||||
|       return function() use($wrapper, $value) { |       return function() use($wrapper, $value) { | ||||||
|         $wrapper->set($value); |         $wrapper->set($value); | ||||||
| @ -1,10 +1,10 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| use Exception; | use Exception; | ||||||
| use nulib\tests\TestCase; | use nulib\tests\TestCase; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarWrapper; | use nulib\schema\_scalar\ScalarWrapper; | ||||||
| use nur\sery\wip\schema\Schema; | use nulib\schema\Schema; | ||||||
| 
 | 
 | ||||||
| class floatTest extends TestCase { | class floatTest extends TestCase { | ||||||
|   function commonTests($wrapper, &$value, callable $wrapperSetter): void { |   function commonTests($wrapper, &$value, callable $wrapperSetter): void { | ||||||
| @ -33,7 +33,7 @@ class floatTest extends TestCase { | |||||||
| 
 | 
 | ||||||
|   function testFloat() { |   function testFloat() { | ||||||
|     /** @var ScalarWrapper $wrapper */ |     /** @var ScalarWrapper $wrapper */ | ||||||
|     Schema::nw($value, null, $schema, "float", $wrapper); |     Schema::nw($value, null, "float", $schema, $wrapper); | ||||||
|     $wrapperSetter = function($value) use($wrapper) { |     $wrapperSetter = function($value) use($wrapper) { | ||||||
|       return function() use($wrapper, $value) { |       return function() use($wrapper, $value) { | ||||||
|         $wrapper->set($value); |         $wrapper->set($value); | ||||||
| @ -53,10 +53,10 @@ class floatTest extends TestCase { | |||||||
| 
 | 
 | ||||||
|   function testRequiredFloat() { |   function testRequiredFloat() { | ||||||
|     /** @var ScalarWrapper $wrapper */ |     /** @var ScalarWrapper $wrapper */ | ||||||
|     Schema::nw($value, null, $schema, [ |     Schema::nw($value, null, [ | ||||||
|       "float", null, |       "float", null, | ||||||
|       "required" => true, |       "required" => true, | ||||||
|     ], $wrapper); |     ], $schema, $wrapper); | ||||||
|     $wrapperSetter = function($value) use($wrapper) { |     $wrapperSetter = function($value) use($wrapper) { | ||||||
|       return function() use($wrapper, $value) { |       return function() use($wrapper, $value) { | ||||||
|         $wrapper->set($value); |         $wrapper->set($value); | ||||||
| @ -75,7 +75,7 @@ class floatTest extends TestCase { | |||||||
| 
 | 
 | ||||||
|   function testNfloat() { |   function testNfloat() { | ||||||
|     /** @var ScalarWrapper $wrapper */ |     /** @var ScalarWrapper $wrapper */ | ||||||
|     Schema::nw($value, null, $schema, "?float", $wrapper); |     Schema::nw($value, null, "?float", $schema, $wrapper); | ||||||
|     $wrapperSetter = function($value) use($wrapper) { |     $wrapperSetter = function($value) use($wrapper) { | ||||||
|       return function() use($wrapper, $value) { |       return function() use($wrapper, $value) { | ||||||
|         $wrapper->set($value); |         $wrapper->set($value); | ||||||
| @ -106,10 +106,10 @@ class floatTest extends TestCase { | |||||||
| 
 | 
 | ||||||
|   function testRequiredNfloat() { |   function testRequiredNfloat() { | ||||||
|     /** @var ScalarWrapper $wrapper */ |     /** @var ScalarWrapper $wrapper */ | ||||||
|     Schema::nw($value, null, $schema, [ |     Schema::nw($value, null, [ | ||||||
|       "?float", null, |       "?float", null, | ||||||
|       "required" => true, |       "required" => true, | ||||||
|     ], $wrapper); |     ], $schema, $wrapper); | ||||||
|     $wrapperSetter = function($value) use($wrapper) { |     $wrapperSetter = function($value) use($wrapper) { | ||||||
|       return function() use($wrapper, $value) { |       return function() use($wrapper, $value) { | ||||||
|         $wrapper->set($value); |         $wrapper->set($value); | ||||||
| @ -1,10 +1,10 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| use Exception; | use Exception; | ||||||
| use nulib\tests\TestCase; | use nulib\tests\TestCase; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarWrapper; | use nulib\schema\_scalar\ScalarWrapper; | ||||||
| use nur\sery\wip\schema\Schema; | use nulib\schema\Schema; | ||||||
| 
 | 
 | ||||||
| class intTest extends TestCase { | class intTest extends TestCase { | ||||||
|   function commonTests($wrapper, &$value, callable $wrapperSetter): void { |   function commonTests($wrapper, &$value, callable $wrapperSetter): void { | ||||||
| @ -33,7 +33,7 @@ class intTest extends TestCase { | |||||||
| 
 | 
 | ||||||
|   function testInt() { |   function testInt() { | ||||||
|     /** @var ScalarWrapper $wrapper */ |     /** @var ScalarWrapper $wrapper */ | ||||||
|     Schema::nw($value, null, $schema, "int", $wrapper); |     Schema::nw($value, null, "int", $schema, $wrapper); | ||||||
|     $wrapperSetter = function($value) use($wrapper) { |     $wrapperSetter = function($value) use($wrapper) { | ||||||
|       return function() use($wrapper, $value) { |       return function() use($wrapper, $value) { | ||||||
|         $wrapper->set($value); |         $wrapper->set($value); | ||||||
| @ -53,10 +53,10 @@ class intTest extends TestCase { | |||||||
| 
 | 
 | ||||||
|   function testRequiredInt() { |   function testRequiredInt() { | ||||||
|     /** @var ScalarWrapper $wrapper */ |     /** @var ScalarWrapper $wrapper */ | ||||||
|     Schema::nw($value, null, $schema, [ |     Schema::nw($value, null, [ | ||||||
|       "int", null, |       "int", null, | ||||||
|       "required" => true, |       "required" => true, | ||||||
|     ], $wrapper); |     ], $schema, $wrapper); | ||||||
|     $wrapperSetter = function($value) use($wrapper) { |     $wrapperSetter = function($value) use($wrapper) { | ||||||
|       return function() use($wrapper, $value) { |       return function() use($wrapper, $value) { | ||||||
|         $wrapper->set($value); |         $wrapper->set($value); | ||||||
| @ -75,7 +75,7 @@ class intTest extends TestCase { | |||||||
| 
 | 
 | ||||||
|   function testNint() { |   function testNint() { | ||||||
|     /** @var ScalarWrapper $wrapper */ |     /** @var ScalarWrapper $wrapper */ | ||||||
|     Schema::nw($value, null, $schema, "?int", $wrapper); |     Schema::nw($value, null, "?int", $schema, $wrapper); | ||||||
|     $wrapperSetter = function($value) use($wrapper) { |     $wrapperSetter = function($value) use($wrapper) { | ||||||
|       return function() use($wrapper, $value) { |       return function() use($wrapper, $value) { | ||||||
|         $wrapper->set($value); |         $wrapper->set($value); | ||||||
| @ -106,10 +106,10 @@ class intTest extends TestCase { | |||||||
| 
 | 
 | ||||||
|   function testRequiredNint() { |   function testRequiredNint() { | ||||||
|     /** @var ScalarWrapper $wrapper */ |     /** @var ScalarWrapper $wrapper */ | ||||||
|     Schema::nw($value, null, $schema, [ |     Schema::nw($value, null, [ | ||||||
|       "?int", null, |       "?int", null, | ||||||
|       "required" => true, |       "required" => true, | ||||||
|     ], $wrapper); |     ], $schema, $wrapper); | ||||||
|     $wrapperSetter = function($value) use($wrapper) { |     $wrapperSetter = function($value) use($wrapper) { | ||||||
|       return function() use($wrapper, $value) { |       return function() use($wrapper, $value) { | ||||||
|         $wrapper->set($value); |         $wrapper->set($value); | ||||||
| @ -1,10 +1,10 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| use Exception; | use Exception; | ||||||
| use nulib\tests\TestCase; | use nulib\tests\TestCase; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarWrapper; | use nulib\schema\_scalar\ScalarWrapper; | ||||||
| use nur\sery\wip\schema\Schema; | use nulib\schema\Schema; | ||||||
| 
 | 
 | ||||||
| class strTest extends TestCase { | class strTest extends TestCase { | ||||||
|   function commonTests($wrapper, &$value, callable $wrapperSetter): void { |   function commonTests($wrapper, &$value, callable $wrapperSetter): void { | ||||||
| @ -41,7 +41,7 @@ class strTest extends TestCase { | |||||||
| 
 | 
 | ||||||
|   function testStr() { |   function testStr() { | ||||||
|     /** @var ScalarWrapper $wrapper */ |     /** @var ScalarWrapper $wrapper */ | ||||||
|     Schema::nw($value, null, $schema, "string", $wrapper); |     Schema::nw($value, null, "string", $schema, $wrapper); | ||||||
|     $wrapperSetter = function($value) use($wrapper) { |     $wrapperSetter = function($value) use($wrapper) { | ||||||
|       return function() use($wrapper, $value) { |       return function() use($wrapper, $value) { | ||||||
|         $wrapper->set($value); |         $wrapper->set($value); | ||||||
| @ -59,10 +59,10 @@ class strTest extends TestCase { | |||||||
| 
 | 
 | ||||||
|   function testRequiredStr() { |   function testRequiredStr() { | ||||||
|     /** @var ScalarWrapper $wrapper */ |     /** @var ScalarWrapper $wrapper */ | ||||||
|     Schema::nw($value, null, $schema, [ |     Schema::nw($value, null, [ | ||||||
|       "string", null, |       "string", null, | ||||||
|       "required" => true, |       "required" => true, | ||||||
|     ], $wrapper); |     ], $schema, $wrapper); | ||||||
|     $wrapperSetter = function($value) use($wrapper) { |     $wrapperSetter = function($value) use($wrapper) { | ||||||
|       return function() use($wrapper, $value) { |       return function() use($wrapper, $value) { | ||||||
|         $wrapper->set($value); |         $wrapper->set($value); | ||||||
| @ -79,7 +79,7 @@ class strTest extends TestCase { | |||||||
| 
 | 
 | ||||||
|   function testNstr() { |   function testNstr() { | ||||||
|     /** @var ScalarWrapper $wrapper */ |     /** @var ScalarWrapper $wrapper */ | ||||||
|     Schema::nw($value, null, $schema, "?string", $wrapper); |     Schema::nw($value, null, "?string", $schema, $wrapper); | ||||||
|     $wrapperSetter = function($value) use($wrapper) { |     $wrapperSetter = function($value) use($wrapper) { | ||||||
|       return function() use($wrapper, $value) { |       return function() use($wrapper, $value) { | ||||||
|         $wrapper->set($value); |         $wrapper->set($value); | ||||||
| @ -100,10 +100,10 @@ class strTest extends TestCase { | |||||||
| 
 | 
 | ||||||
|   function testRequiredNstr() { |   function testRequiredNstr() { | ||||||
|     /** @var ScalarWrapper $wrapper */ |     /** @var ScalarWrapper $wrapper */ | ||||||
|     Schema::nw($value, null, $schema, [ |     Schema::nw($value, null, [ | ||||||
|       "?string", null, |       "?string", null, | ||||||
|       "required" => true, |       "required" => true, | ||||||
|     ], $wrapper); |     ], $schema, $wrapper); | ||||||
|     $wrapperSetter = function($value) use($wrapper) { |     $wrapperSetter = function($value) use($wrapper) { | ||||||
|       return function() use($wrapper, $value) { |       return function() use($wrapper, $value) { | ||||||
|         $wrapper->set($value); |         $wrapper->set($value); | ||||||
| @ -1,9 +1,9 @@ | |||||||
| <?php | <?php | ||||||
| namespace nur\sery\wip\schema\types; | namespace nulib\schema\types; | ||||||
| 
 | 
 | ||||||
| use nulib\tests\TestCase; | use nulib\tests\TestCase; | ||||||
| use nur\sery\wip\schema\_scalar\ScalarWrapper; | use nulib\schema\_scalar\ScalarWrapper; | ||||||
| use nur\sery\wip\schema\Schema; | use nulib\schema\Schema; | ||||||
| 
 | 
 | ||||||
| class unionTest extends TestCase { | class unionTest extends TestCase { | ||||||
|   function testUnionTypes() { |   function testUnionTypes() { | ||||||
| @ -11,7 +11,7 @@ class unionTest extends TestCase { | |||||||
|      |      | ||||||
|     # string puis int
 |     # string puis int
 | ||||||
|     /** @var ScalarWrapper $siw */ |     /** @var ScalarWrapper $siw */ | ||||||
|     Schema::nw($si, null, $sis, "string|int", $siw); |     Schema::nw($si, null, "string|int", $sis, $siw); | ||||||
| 
 | 
 | ||||||
|     $siw->set("12"); |     $siw->set("12"); | ||||||
|     self::assertSame("12", $si); |     self::assertSame("12", $si); | ||||||
| @ -20,7 +20,7 @@ class unionTest extends TestCase { | |||||||
| 
 | 
 | ||||||
|     # int puis string
 |     # int puis string
 | ||||||
|     /** @var ScalarWrapper $isw */ |     /** @var ScalarWrapper $isw */ | ||||||
|     Schema::nw($is, null, $iss, "int|string", $isw); |     Schema::nw($is, null, "int|string", $iss, $isw); | ||||||
| 
 | 
 | ||||||
|     $isw->set("12"); |     $isw->set("12"); | ||||||
|     self::assertSame("12", $is); |     self::assertSame("12", $is); | ||||||
| @ -1,168 +0,0 @@ | |||||||
| <?php |  | ||||||
| namespace nur\sery\wip\schema\_assoc; |  | ||||||
| 
 |  | ||||||
| use nulib\ext\yaml; |  | ||||||
| use nulib\tests\TestCase; |  | ||||||
| use nur\sery\wip\schema\_scalar\ScalarSchemaTest; |  | ||||||
| 
 |  | ||||||
| class AssocSchemaTest extends TestCase { |  | ||||||
|   const NULL_SCHEMA = [ |  | ||||||
|     "" => [ |  | ||||||
|       "assoc", |  | ||||||
|       "compute_func" => null, |  | ||||||
|       "validate_func" => null, |  | ||||||
|       "ensure_array" => false, |  | ||||||
|       "ensure_keys" => true, |  | ||||||
|       "ensure_order" => true, |  | ||||||
|     ], |  | ||||||
|     "schema" => null, |  | ||||||
|     "type" => [null], |  | ||||||
|     "default" => null, |  | ||||||
|     "title" => null, |  | ||||||
|     "required" => false, |  | ||||||
|     "nullable" => true, |  | ||||||
|     "desc" => null, |  | ||||||
|     "analyzer_func" => null, |  | ||||||
|     "extractor_func" => null, |  | ||||||
|     "parser_func" => null, |  | ||||||
|     "normalizer_func" => null, |  | ||||||
|     "messages" => null, |  | ||||||
|     "formatter_func" => null, |  | ||||||
|     "format" => null, |  | ||||||
|     "name" => null, |  | ||||||
|     "pkey" => null, |  | ||||||
|     "header" => null, |  | ||||||
|     "computed" => null, |  | ||||||
|   ]; |  | ||||||
| 
 |  | ||||||
|   static function schema(array $definition, array $keyDefinitions): array { |  | ||||||
|     $definition = array_merge(self::NULL_SCHEMA, $definition, ["schema" => []]); |  | ||||||
|     foreach ($keyDefinitions as $key => $keydef) { |  | ||||||
|       $definition["schema"][$key] = array_merge(ScalarSchemaTest::NULL_SCHEMA, $keydef); |  | ||||||
|     }; unset($subdef); |  | ||||||
|     return $definition; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function testNormalize() { |  | ||||||
|     self::assertSame(self::schema([ |  | ||||||
|       "type" => ["array"], "nullable" => true, |  | ||||||
|     ], [ |  | ||||||
|       "a" => [ |  | ||||||
|         "type" => ["string"], "nullable" => false, |  | ||||||
|         "name" => "a", "pkey" => "a", "header" => "a", |  | ||||||
|       ], |  | ||||||
|     ]), AssocSchema::normalize_definition(["a" => "string"])); |  | ||||||
| 
 |  | ||||||
|     self::assertSame(self::schema([ |  | ||||||
|       "type" => ["array"], "nullable" => true, |  | ||||||
|     ], [ |  | ||||||
|       "a" => [ |  | ||||||
|         "type" => ["string"], "nullable" => false, |  | ||||||
|         "name" => "a", "pkey" => "a", "header" => "a", |  | ||||||
|       ], |  | ||||||
|       "b" => [ |  | ||||||
|         "type" => ["int"], "nullable" => false, |  | ||||||
|         "name" => "b", "pkey" => "b", "header" => "b", |  | ||||||
|       ], |  | ||||||
|       "c" => [ |  | ||||||
|         "type" => ["bool"], "nullable" => false, |  | ||||||
|         "name" => "c", "pkey" => "c", "header" => "c", |  | ||||||
|       ], |  | ||||||
|     ]), AssocSchema::normalize_definition([ |  | ||||||
|       "a" => "string", |  | ||||||
|       "b" => "int", |  | ||||||
|       "c" => "bool", |  | ||||||
|     ])); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function testConstructor() { |  | ||||||
|     $schema = new AssocSchema([ |  | ||||||
|       "a" => "string", |  | ||||||
|       "b" => "int", |  | ||||||
|       "c" => "bool", |  | ||||||
|     ]); |  | ||||||
|     self::assertSame(self::schema([ |  | ||||||
|       "type" => ["array"], "nullable" => true, |  | ||||||
|     ], [ |  | ||||||
|       "a" => [ |  | ||||||
|         "type" => ["string"], "nullable" => false, |  | ||||||
|         "name" => "a", "pkey" => "a", "header" => "a", |  | ||||||
|       ], |  | ||||||
|       "b" => [ |  | ||||||
|         "type" => ["int"], "nullable" => false, |  | ||||||
|         "name" => "b", "pkey" => "b", "header" => "b", |  | ||||||
|       ], |  | ||||||
|       "c" => [ |  | ||||||
|         "type" => ["bool"], "nullable" => false, |  | ||||||
|         "name" => "c", "pkey" => "c", "header" => "c", |  | ||||||
|       ], |  | ||||||
|     ]), $schema->getDefinition()); |  | ||||||
|     //yaml::dump($schema->getDefinition());
 |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function testWrapper() { |  | ||||||
|     $schema = new AssocSchema([ |  | ||||||
|       "a" => "?string", |  | ||||||
|       "b" => "?int", |  | ||||||
|       "c" => "?bool", |  | ||||||
|     ]); |  | ||||||
|     $array = ["a" => "  string  ", "b" => "  42 ", "c" => false]; |  | ||||||
|     $schema->getWrapper($array); |  | ||||||
|     self::assertSame([ |  | ||||||
|       "a" => "string", |  | ||||||
|       "b" => 42, |  | ||||||
|       "c" => false, |  | ||||||
|     ], $array); |  | ||||||
| 
 |  | ||||||
|     $schema = new AssocSchema([ |  | ||||||
|       "a" => "string", |  | ||||||
|       "b" => "int", |  | ||||||
|       "c" => "bool", |  | ||||||
|     ]); |  | ||||||
| 
 |  | ||||||
|     $array = ["a" => "  string  "]; |  | ||||||
|     $schema->getWrapper($array); |  | ||||||
|     self::assertSame([ |  | ||||||
|       "a" => "string", |  | ||||||
|       "b" => false, |  | ||||||
|       "c" => null, |  | ||||||
|     ], $array); |  | ||||||
| 
 |  | ||||||
|     $array = ["c" => false, "a" => "  string  "]; |  | ||||||
|     $schema->getWrapper($array); |  | ||||||
|     self::assertSame([ |  | ||||||
|       "a" => "string", |  | ||||||
|       "b" => false, |  | ||||||
|       "c" => false, |  | ||||||
|     ], $array); |  | ||||||
| 
 |  | ||||||
|     $array = ["a" => "  string  "]; |  | ||||||
|     $schema->getWrapper($array, null, ["ensure_order" => false]); |  | ||||||
|     self::assertSame([ |  | ||||||
|       "a" => "string", |  | ||||||
|       "b" => false, |  | ||||||
|       "c" => null, |  | ||||||
|     ], $array); |  | ||||||
| 
 |  | ||||||
|     $array = ["c" => false, "a" => "  string  "]; |  | ||||||
|     $schema->getWrapper($array, null, ["ensure_order" => false]); |  | ||||||
|     self::assertSame([ |  | ||||||
|       "c" => false, |  | ||||||
|       "a" => "string", |  | ||||||
|       "b" => false, |  | ||||||
|     ], $array); |  | ||||||
| 
 |  | ||||||
|     $array = ["a" => "  string  "]; |  | ||||||
|     $schema->getWrapper($array, null, ["ensure_keys" => false]); |  | ||||||
|     self::assertSame([ |  | ||||||
|       "a" => "string", |  | ||||||
|     ], $array); |  | ||||||
| 
 |  | ||||||
|     $array = ["c" => false, "a" => "  string  "]; |  | ||||||
|     $schema->getWrapper($array, null, ["ensure_keys" => false]); |  | ||||||
|     self::assertSame([ |  | ||||||
|       "a" => "string", |  | ||||||
|       "c" => false, |  | ||||||
|     ], $array); |  | ||||||
|   } |  | ||||||
| } |  | ||||||
Some files were not shown because too many files have changed in this diff Show More
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user