<?php
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class CardSchemeValidator extends ConstraintValidator {
protected $schemes = array(
'AMEX' => array(
'/^3[47][0-9]{13}$/',
),
'CHINA_UNIONPAY' => array(
'/^62[0-9]{14,17}$/',
),
'DINERS' => array(
'/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/',
),
'DISCOVER' => array(
'/^6011[0-9]{12}$/',
'/^64[4-9][0-9]{13}$/',
'/^65[0-9]{14}$/',
'/^622(12[6-9]|1[3-9][0-9]|[2-8][0-9][0-9]|91[0-9]|92[0-5])[0-9]{10}$/',
),
'INSTAPAYMENT' => array(
'/^63[7-9][0-9]{13}$/',
),
'JCB' => array(
'/^(?:2131|1800|35[0-9]{3})[0-9]{11}$/',
),
'LASER' => array(
'/^(6304|670[69]|6771)[0-9]{12,15}$/',
),
'MAESTRO' => array(
'/^(5018|5020|5038|6304|6759|6761|676[23]|0604)[0-9]{8,15}$/',
),
'MASTERCARD' => array(
'/^5[1-5][0-9]{14}$/',
),
'VISA' => array(
'/^4([0-9]{12}|[0-9]{15})$/',
),
);
public function validate($value, Constraint $constraint) {
if (null === $value || '' === $value) {
return;
}
if (!is_numeric($value)) {
$this->context
->addViolation($constraint->message);
return;
}
$schemes = array_flip((array) $constraint->schemes);
$schemeRegexes = array_intersect_key($this->schemes, $schemes);
foreach ($schemeRegexes as $regexes) {
foreach ($regexes as $regex) {
if (preg_match($regex, $value)) {
return;
}
}
}
$this->context
->addViolation($constraint->message);
}
}