for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace InputGuard\Guards\Bases;
trait SingleInput
{
/**
* The input to be validated.
*
* @var mixed
*/
private $input;
* The value to be returned after validation is complete.
private $value;
* A flag that indicates the validated state the object is in.
* @var bool|null
private $validated;
* A flag that if set to true indicates that a null as the input is acceptable.
* @var bool
private $allowNull = false;
* A flag that if set to true indicates that an empty string as the input is acceptable.
private $allowEmptyString = false;
* The validation algorithm that determines if the input was successfully validated.
* If PHP allowed the behavior the method would be private.
* @param mixed $input
* @param mixed &$value
* @return bool
abstract protected function validation($input, &$value): bool;
* A switch to update the object state to allow nulls as a valid value for the input.
* @return $this
public function allowNull(): self
$this->allowNull = true;
return $this;
}
* A switch to update the object state to allow empty strings as a valid value for the input.
public function allowEmptyString(): self
$this->allowEmptyString = true;
* Execute to determine the success status of the input validation.
public function success(): bool
if ($this->validated === null) {
if ($this->allowNull && $this->input === null) {
$this->validated = true;
} elseif ($this->allowEmptyString && $this->input === '') {
} else {
$this->validated = $this->validation($this->input, $this->value);
return $this->validated;