Skip to content

Instantly share code, notes, and snippets.

@Koopzington
Created July 9, 2018 13:00
Show Gist options
  • Save Koopzington/e4e7a3218d6d90bc37f0ee88346f9594 to your computer and use it in GitHub Desktop.
Save Koopzington/e4e7a3218d6d90bc37f0ee88346f9594 to your computer and use it in GitHub Desktop.
<?php
namespace Foo\Validator;
use Zend\Validator\ValidatorInterface;
final class LicensePlateValidator implements ValidatorInterface
{
const FORMATS = [
// Germany (Total Length: 8 characters excluding spaces, No leading zeros, can end in E or H)
'/^(?=.{5,10}$|.{5,10}(?:H|E)$)[A-ZÖÜÄ]{1,3} [A-Z]{1,2} [1-9][0-9]{0,3}(?: H| E){0,1}$/',
// Austria (Total Length: 7 characters excluding spaces, No leading zeros, no Q in 2nd letter block
'/^(?=.{5,9}$)[A-Z]{1,2} (?:([1-9][0-9]{0,4}) ([A-PR-Z]{1,5})|(?2) (?1))$/',
];
/** @var array */
private $messages;
public function isValid($value): bool
{
foreach (self::FORMATS as $format) {
if (preg_match($format, $value) === 1) {
return true;
}
}
$this->messages[] = 'Invalid license plate format. Only numbers, letters and spaces are allowed';
return false;
}
public function getMessages(): array
{
return $this->messages;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment