3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Length { public $violations = []; /** * Minimum length. * * @var int */ private $min; /** * Maximum length. * * @var int */ private $max; /** * Charset. * * @var string */ private $charset; /** * Length validation rule constructor. * * @param int $min * @param int $max * @param string $charset * @throws InvalidArgumentException * @throws LogicException */ public function __construct( $min = null, $max = null, $charset = 'UTF-8' ) { if ($min === null && $max === null) { throw new \InvalidArgumentException('Either option "min" or "max" must be given.'); } if ($max < $min) { throw new \LogicException('"Max" option cannot be less that "min".'); } $this->min = $min; $this->max = $max; $this->charset = $charset; } /** * Validates input. * * @param mixed $input * * @return bool */ public function isValid($input = null) { if ($input === null || $input === '') { return false; } $input = (string) $input; if (!$invalidCharset = !@mb_check_encoding($input, $this->charset)) { $length = mb_strlen($input, $this->charset); } if ($invalidCharset) { $this->violations[] = 'charset'; return false; } if ($this->max !== null && $length > $this->max) { $this->violations[] = 'max'; return false; } if ($this->min !== null && $length < $this->min) { $this->violations[] = 'min'; return false; } return true; } } $length = new Length(-100, ['a', 'b', 'c', 185, null]); $length->isValid(stream_context_create()); echo sprintf('Count fail violations: %d', count($length->violations));

preferences:
28.23 ms | 402 KiB | 5 Q