From 9a525e45c7310c4f5537b93b55268138585a2e8d Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Wed, 29 Nov 2023 06:49:47 +0100 Subject: [PATCH] feat: add default() rule --- src/Rules.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Rules.php b/src/Rules.php index 56d1d74..b12e0e2 100644 --- a/src/Rules.php +++ b/src/Rules.php @@ -24,6 +24,9 @@ public bool $required = false; public ?Type $type = null; + private bool $default_enabled = false; + public mixed $default; + public ?int $min = null; public ?int $max = null; @@ -76,6 +79,14 @@ return $this; } + // Set a default value if property is not provided + public function default(mixed $value): self { + $this->default_enabled = true; + $this->default = $value; + + return $this; + } + /* # Eval methods These methods are used to check conformity against set rules. @@ -124,8 +135,11 @@ return true; } - // Property does not exist in scope, create nulled superglobal for it - $scope_data[$this->property] = null; + // Property does not exist in superglobal, create one with default value if enabled + if ($this->default_enabled) { + $scope_data[$this->property] = $this->default; + } + return false; }