Assert\Assertion::betweenLength PHP Méthode

betweenLength() public static méthode

Assert that string length is between min,max lengths.
public static betweenLength ( mixed $value, integer $minLength, integer $maxLength, string | null $message = null, string | null $propertyPath = null, string $encoding = 'utf8' ) : boolean
$value mixed
$minLength integer
$maxLength integer
$message string | null
$propertyPath string | null
$encoding string
Résultat boolean
    public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
    {
        static::string($value, $message, $propertyPath);
        if (mb_strlen($value, $encoding) < $minLength) {
            $message = sprintf($message ?: 'Value "%s" is too short, it should have at least %d characters, but only has %d characters.', static::stringify($value), $minLength, mb_strlen($value, $encoding));
            $constraints = array('min_length' => $minLength, 'encoding' => $encoding);
            throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
        }
        if (mb_strlen($value, $encoding) > $maxLength) {
            $message = sprintf($message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.', static::stringify($value), $maxLength, mb_strlen($value, $encoding));
            $constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
            throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
        }
        return true;
    }

Usage Example

Exemple #1
0
 public function testValidBetweenLength()
 {
     Assertion::betweenLength("foo", 0, 3);
     Assertion::betweenLength("址址", 2, 2);
 }
All Usage Examples Of Assert\Assertion::betweenLength