for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Fiv\Form\Validator;
/**
* Check value length
*
* @author Ivan Shcherbak <[email protected]>
* @package Fiv\Form\Validator
*/
class Len extends \Fiv\Form\Validator\Base {
* @var int|null
protected $exactLen;
* @var string|null
protected $exactLenError;
protected $minLen;
protected $minLenError;
protected $maxLen;
protected $maxLenError;
* Maximum len of value
* @param $len
* @param $error
* @return $this
public function max($len, $error) {
$this->maxLen = $len;
$this->maxLenError = $error;
return $this;
}
* Minimum len of value
public function min($len, $error) {
$this->minLen = $len;
$this->minLenError = $error;
* Expect exact len of value
public function exact($len, $error) {
$this->exactLen = $len;
$this->exactLenError = $error;
* Validate value
* @param string $value
* @return bool
public function isValid($value) {
if ($this->exactLen !== null and mb_strlen($value, 'UTF-8') != $this->exactLen) {
$this->addError(vsprintf($this->exactLenError, [$this->exactLen]));
if ($this->maxLen !== null and mb_strlen($value, 'UTF-8') > $this->maxLen) {
$this->addError(vsprintf($this->maxLenError, [$this->maxLen]));
if ($this->minLen !== null and mb_strlen($value, 'UTF-8') < $this->minLen) {
$this->addError(vsprintf($this->minLenError, [$this->minLen]));
return !$this->hasErrors();