- Big WIP on forms component
This commit is contained in:
1 parent
c0b0681fee
commit
3d50cfaccb
14 files changed
+114
-20
No files matched your search
@@ -2,6 +2,11 @@
|
||||
|
||||
namespace Lean\Api\Attribute;
|
||||
|
||||
use Lean\Api\Exception\MandatoryFieldException;
|
||||
use Lean\Api\Exception\MaximumLengthException;
|
||||
use Lean\Api\Exception\MinimumLengthException;
|
||||
use Lean\Api\Exception\RegexFormatException;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_CLASS)]
|
||||
class ContextField
|
||||
{
|
||||
@@ -13,5 +18,34 @@ class ContextField
|
||||
public ?string $regexFormat = null,
|
||||
public ?string $example = null,
|
||||
public bool $hidden = false,
|
||||
public mixed $default = false,
|
||||
) {}
|
||||
|
||||
public function assertValueSpecs(mixed $value, bool $isNew) : void
|
||||
{
|
||||
if ($isNew && ( $this->mandatory && $value === null )) {
|
||||
throw new MandatoryFieldException("Mandatory field must be provided a value.");
|
||||
}
|
||||
elseif ($value !== null) {
|
||||
if ($this->minLength || $this->maxLength) {
|
||||
if (is_string($value)) {
|
||||
if (mb_strlen($value) < $this->minLength) {
|
||||
throw new MinimumLengthException("Minimum {$this->minLength} string length is required.");
|
||||
}
|
||||
elseif (mb_strlen($value) > $this->maxLength) {
|
||||
throw new MaximumLengthException("Maximum {$this->maxLength} string length has been exceeded.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->regexFormat) {
|
||||
if (! is_string($value)) {
|
||||
throw new RegexFormatException("Property type must be a string if a regexFormat is provided.");
|
||||
}
|
||||
elseif (! preg_match($this->regexFormat, $value)) {
|
||||
throw new RegexFormatException("Regex format {$this->regexFormat} is not valid for value {$value}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user