Webmozart\Assert\Assert::regex PHP Method

regex() public static method

public static regex ( $value, $pattern, $message = '' )
    public static function regex($value, $pattern, $message = '')
    {
        if (!preg_match($pattern, $value)) {
            static::reportInvalidArgument(sprintf($message ?: 'The value %s does not match the expected pattern.', static::valueToString($value)));
        }
    }

Usage Example

示例#1
0
 /**
  * @param string $value
  */
 public static function create($value)
 {
     try {
         Assert::string($value, 'Value must be a string');
         Assert::regex($value, '{^(?:.*\\d.*){6}$}', 'Value must contain 6 digits');
     } catch (\InvalidArgumentException $e) {
         throw E::wrap($e);
     }
     return new self(preg_replace('{[^0-9]}', '', $value));
 }
All Usage Examples Of Webmozart\Assert\Assert::regex