Автор: Bernhard Schussek (bschussek@gmail.com)
Наследование: extends Symfony\Component\Validator\ConstraintValidator
Пример #1
0
 /**
  * @dataProvider validateDataProvider
  * @param mixed $data
  * @param boolean $correct
  */
 public function testValidate($data, $correct)
 {
     if ($correct) {
         $this->context->expects($this->never())->method('addViolation');
     } else {
         $this->context->expects($this->once())->method('addViolation')->with($this->constraint->message);
     }
     $this->validator->validate($data, $this->constraint);
 }
Пример #2
0
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     // first is this an url ?
     parent::validate($value, $constraint);
     // now parse it :
     $parts = parse_url($value);
     switch ($parts['host']) {
         case 'www.youtube.com':
         case 'youtube.com':
             if (!empty($parts['query'])) {
                 $param = [];
                 parse_str($parts['query'], $param);
                 if (!isset($param['v'])) {
                     $this->context->addViolation("This youtube page does not contain a video");
                 }
             }
             break;
         case 'youtu.be':
             if (!preg_match('#^/[a-z0-9A-Z_-]+$#', $parts['path'])) {
                 $this->context->addViolation("This youtube page does not contain a video");
             }
             break;
         default:
             $this->context->addViolation("This is not a youtube page");
     }
 }
Пример #3
0
 /**
  * {@inheritDoc}
  */
 public function validate($value, Validator\Constraint $constraint)
 {
     // If value has not been submitting, it should skip validation. However, we cannot simply check for
     // falsiness because the value could be '0' (spoiler alert: that will fail validation)
     if (null === $value || false === $value || '' === $value) {
         return;
     }
     parent::validate($value, $constraint);
     if (!$this->_isValid($value)) {
         $this->context->addViolation('\'%value%\' is not a valid YouTube video URL', ['%value%' => $value]);
     }
 }
Пример #4
0
 /**
  * {@inheritDoc}
  */
 public function validate($value, Validator\Constraint $constraint)
 {
     // If value has not been submitting, it should skip validation. However, we cannot simply check for
     // falsiness because the value could be '0' (spoiler alert: that will fail validation)
     if (null === $value || false === $value || '' === $value) {
         return;
     }
     parent::validate($value, $constraint);
     if (!$this->_isValid($value)) {
         $this->context->addViolation(self::ERROR_MESSAGE, [self::VALUE_KEY => $value]);
     }
 }
Пример #5
0
 /**
  * {@inheritDoc}
  *
  * @param string     $value
  * @param Constraint $constraint
  */
 public function validate($value, Constraint $constraint)
 {
     $startWithProtocol = preg_match('/^[a-z]{3,9}\\:\\/\\//i', $value);
     $fullyValidUri = $startWithProtocol && preg_match('/
         (?:[a-z0-9\\-\\.]+\\.[a-z0-9]+)
         (?:\\/[a-z0-9\\.\\-\\/\\$\\_=\\?]*)?$
     /ix', $value);
     $validLocalPath = preg_match('/^\\/[a-z0-9\\.\\-\\/\\$\\_=\\?\\&]+$/i', $value);
     if ($constraint->global && $fullyValidUri) {
         list($uriValue) = explode('?', $value);
         return parent::validate($uriValue, $constraint);
     }
     if (!$constraint->local || !$validLocalPath) {
         $this->context->addViolation($constraint->message, array('{{ value }}' => $value));
     }
 }
 public function validate($value, Constraint $constraint)
 {
     if (empty($value)) {
         return;
     }
     $previousViolationsCount = $this->context->getViolations()->count();
     parent::validate($value, $constraint);
     if ($previousViolationsCount < $this->context->getViolations()->count()) {
         return;
     }
     $client = new Client();
     try {
         $request = $client->head($value);
         $response = $request->send();
         if (!$response->isSuccessful()) {
             $this->context->addViolation($constraint->clientError, array('%errorCode%' => $response->getStatusCode()));
         }
     } catch (CurlException $e) {
         $this->context->addViolation($constraint->websiteDoesntExist, array('%url' => $value));
     } catch (ClientErrorResponseException $e) {
         $errorCode = $e->getResponse()->getStatusCode();
         if ($errorCode == 403) {
             $this->context->addViolation($constraint->accessDenied);
         } elseif ($errorCode == 404) {
             $this->context->addViolation($constraint->resNotFound);
         } elseif ($errorCode == 405) {
             $allow = $e->getResponse()->getHeaders()['allow'];
             if (!preg_match('#GET#', $allow)) {
                 $this->context->addViolation($constraint->methodNotAllowed);
             }
         } else {
             $this->context->addViolation($constraint->clientError, array('%errorCode%' => $errorCode));
         }
     } catch (ServerErrorResponseException $e) {
         $this->context->addViolation($constraint->serverError, array('%errorCode%' => $e->getResponse()->getStatusCode()));
     }
 }