Assert\Assertion::length PHP Method

length() public static method

Assert that string has a given length.
public static length ( mixed $value, integer $length, string | null $message = null, string | null $propertyPath = null, string $encoding = 'utf8' ) : boolean
$value mixed
$length integer
$message string | null
$propertyPath string | null
$encoding string
return boolean
    public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8')
    {
        static::string($value, $message, $propertyPath);
        if (mb_strlen($value, $encoding) !== $length) {
            $message = sprintf($message ?: 'Value "%s" has to be %d exactly characters long, but length is %d.', static::stringify($value), $length, mb_strlen($value, $encoding));
            $constraints = array('length' => $length, 'encoding' => $encoding);
            throw static::createException($value, $message, static::INVALID_LENGTH, $propertyPath, $constraints);
        }
        return true;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * isEmpty.
  *
  * @return bool
  */
 public function isEmpty()
 {
     try {
         Assertion::length($this->value, 0);
         return true;
     } catch (AssertionInvalidArgumentException $exception) {
         return false;
     }
 }
All Usage Examples Of Assert\Assertion::length