Validators for Form Fields

This commit is contained in:
2024-07-05 13:39:02 +02:00
parent 7043d56329
commit 6bb19ad6b0
9 changed files with 262 additions and 2 deletions
+3
View File
@@ -0,0 +1,3 @@
Order deny,allow
Deny from all
Allow from 127.0.0.1
@@ -0,0 +1,112 @@
<?php
class FieldValidator
{
/*
* Private members
*/
private string $name;
private string $value;
private string $type;
private bool $required;
private int $minLen;
private int $maxLen;
private string $error;
/*
* Constructor
*/
public function __construct(string $name, string $value, string $type, bool $required = false, int $minLen = -1, int $maxLen = -1, string $error = "")
{
$this->name = $name;
$this->value = $value;
$this->type = $type;
$this->required = $required;
$this->minLen = $minLen;
$this->maxLen = $maxLen;
$this->error = $error;
return $this;
}
/*
* Fluent Style
*/
public static function create(string $name, string $value, string $type)
{
return new FieldValidator($name, $value, $type);
}
public function isRequired()
{
$this->required = true;
return $this;
}
public function withMinLen(int $minLen)
{
$this->minLen = $minLen;
return $this;
}
public function withMaxLen(int $maxLen)
{
$this->maxLen = $maxLen;
return $this;
}
/*
* Getters and Setters
*/
public function getError()
{
return $this->error;
}
public function getName()
{
return $this->name;
}
/*
* Methods
*/
public function isValid()
{
if ($this->required && empty($this->value)) {
$this->error = "$this->name is required";
return false;
}
if ($this->minLen >= 0 && !empty($this->value) && strlen($this->value) < $this->minLen) {
$this->error = "$this->name requires $this->minLen characters, got " . strlen($this->value);
return false;
}
if ($this->maxLen >= 0 && !empty($this->value) && strlen($this->value) > $this->maxLen) {
$this->error = "$this->name allows $this->maxLen characters, got " . strlen($this->value);
return false;
}
switch ($this->type) {
case "email":
if (!filter_var($this->value, FILTER_VALIDATE_EMAIL)) {
$this->error = "Not a valid E-Mail";
return false;
}
break;
case "number":
if (!filter_var($this->value, FILTER_VALIDATE_FLOAT) || !filter_var($this->value, FILTER_VALIDATE_INT)) {
$this->error = "Not a valid number";
return false;
}
break;
}
return true;
}
}
@@ -0,0 +1,61 @@
<?php
class FormValidator
{
/*
* Private members
*/
private array $fields;
/*
* Constructor
*/
public function __construct(FieldValidator ...$fields)
{
$this->fields = $fields;
}
/*
* Fluent Style
*/
public static function create(FieldValidator ...$fields)
{
return new FormValidator(...$fields);
}
/*
* Methods
*/
public function isValid()
{
foreach ($this->fields as $field) {
if (!$field->isValid) return false;
}
return true;
}
public function getErrors()
{
$errors = [];
foreach ($this->fields as $field) {
if (!$field->isValid()) array_push($errors, $field->getError());
}
return $errors;
}
public function getField(string $name)
{
$field = current(array_filter($this->fields, fn ($field) => strcmp($field->getName(), $name) === 0));
if ($field !== false) {
return $field;
}
return "No field named $name";
}
}
+58
View File
@@ -0,0 +1,58 @@
<?php
class Hyperlink
{
/*
* Private members
*/
private string $uri;
private string $title;
private ?string $colour;
/*
* Constructor
*/
public function __construct(string $uri, string $title, string $colour = null)
{
$this->uri = $uri;
$this->title = $title;
$this->setColour($colour);
}
/*
* Getters and Setters
*/
public function setColour(string $colour = null)
{
if ($colour === null) {
$this->colour = null;
return;
}
if (!preg_match("/^#([0-9A-Fa-f]{3,4}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$/", $colour)) {
trigger_error("Colour invalid", E_USER_WARNING);
return;
}
$this->colour = $colour;
}
public function getColour()
{
return $this->colour ?? "No colour specified";
}
/*
* Meta
*/
public function __toString()
{
$style = $this->colour ? "style=\"color:$this->colour;\"" : "";
return <<<END
<a href="$this->uri" $style>$this->title</a>
END;
}
}
+33
View File
@@ -0,0 +1,33 @@
<?php
class Person
{
/*
* Private members
*/
private string $name;
private float $weight;
private DateTime $birthdate;
/*
* Constructor
*/
public function __construct(string $name, float $weight, DateTime $birthdate)
{
$this->name = $name;
$this->weight = $weight;
$this->birthdate = $birthdate;
}
/*
* Meta
*/
public function __toString()
{
$formatter = new IntlDateFormatter('en_UK', IntlDateFormatter::SHORT, IntlDateFormatter::SHORT);
$formatter->setPattern('E dd.MM.yyyy');
return "$this->name, currently {$this->weight}lbs, was born on {$formatter->format($this->birthdate)}";
}
}