/** * {@inheritDoc} */ public function validate($value, Constraint $constraint) { if (null === $value || '' === $value) { return; } $vatNumber = new VatNumber(); try { $value = (string) $vatNumber->fromString($value); } catch (\InvalidArgumentException $e) { throw new UnexpectedTypeException($value, 'string'); } catch (Vat\InvalidCountryCodeException $e) { $this->context->addViolation($constraint->incorrectCountryCodeMessage, array('{{ countryCode }}' => $vatNumber->getCountryCode())); } catch (Vat\InvalidNumberException $e) { $this->context->addViolation($constraint->invalidMessage); } $pattern = $vatNumber->getPattern($vatNumber->getCountryCode()); if (null !== $pattern && !preg_match($pattern, $vatNumber->getNumber())) { $this->context->addViolation($constraint->invalidMessage); } //$validateForCountryCB = 'validateForCountry'.$vatNumber->getCountryCode(); //if (method_exists($this, $validateForCountryCB)) { //if (!$this->$validateForCountryCB($vatNumber)) { //// //$this->context->addViolation(); //} //} }
/** * {@inheritDoc} */ public function validate($value, Constraint $constraint) { if (null === $value || '' === $value) { return; } $vatNumber = new VatNumber(); try { $value = (string) $vatNumber->fromString($value); } catch (\InvalidArgumentException $e) { throw new UnexpectedTypeException($value, 'string'); } try { $result = $this->getSoapClient()->checkVat(array('countryCode' => $vatNumber->getCountryCode(), 'vatNumber' => $vatNumber->getNumber())); if ($result->valid != 1) { $this->context->addViolation($constraint->message, array('{{ countryCode }}' => $vatNumber->getCountryCode())); } } catch (\SoapException $e) { $this->context->addViolation($constraint->remoteError); throw $e; } }
public function testFromString($input, $expectedOutput) { $this->if($vatNumber = new TestedVatNumber())->string((string) $vatNumber->fromString($input))->isIdenticalTo($expectedOutput); }