Пример #1
0
 public function testCanAcceptContextWithOptions()
 {
     $value = 'bar';
     $context = array('foo' => 'bar', 'bar' => 'baz');
     $options = array('baz' => 'bat');
     $validator = new Callback(function ($v, $c, $baz) use($value, $context, $options) {
         return $value == $v && $context == $c && $options['baz'] == $baz;
     });
     $validator->setCallbackOptions($options);
     $this->assertTrue($validator->isValid($value, $context));
 }
Пример #2
0
 public function testAddingValueOptions()
 {
     $valid = new Validator\Callback(array('callback' => array($this, 'optionsCallback'), 'callbackOptions' => 'options'));
     $this->assertEquals(array('options'), $valid->getCallbackOptions());
     $this->assertTrue($valid->isValid('test', 'something'));
 }
Пример #3
0
 /**
  * Returns true if and only if $value follows the Luhn algorithm (mod-10 checksum)
  *
  * @param  string $value
  * @return boolean
  */
 public function isValid($value)
 {
     $this->setValue($value);
     if (!is_string($value)) {
         $this->error(self::INVALID, $value);
         return false;
     }
     if (!ctype_digit($value)) {
         $this->error(self::CONTENT, $value);
         return false;
     }
     $length = strlen($value);
     $types = $this->getType();
     $foundp = false;
     $foundl = false;
     foreach ($types as $type) {
         foreach ($this->cardType[$type] as $prefix) {
             if (substr($value, 0, strlen($prefix)) == $prefix) {
                 $foundp = true;
                 if (in_array($length, $this->cardLength[$type])) {
                     $foundl = true;
                     break 2;
                 }
             }
         }
     }
     if ($foundp == false) {
         $this->error(self::PREFIX, $value);
         return false;
     }
     if ($foundl == false) {
         $this->error(self::LENGTH, $value);
         return false;
     }
     $sum = 0;
     $weight = 2;
     for ($i = $length - 2; $i >= 0; $i--) {
         $digit = $weight * $value[$i];
         $sum += floor($digit / 10) + $digit % 10;
         $weight = $weight % 2 + 1;
     }
     if ((10 - $sum % 10) % 10 != $value[$length - 1]) {
         $this->error(self::CHECKSUM, $value);
         return false;
     }
     $service = $this->getService();
     if (!empty($service)) {
         try {
             $callback = new Callback($service);
             $callback->setOptions($this->getType());
             if (!$callback->isValid($value)) {
                 $this->error(self::SERVICE, $value);
                 return false;
             }
         } catch (\Exception $e) {
             $this->error(self::SERVICEFAILURE, $value);
             return false;
         }
     }
     return true;
 }
Пример #4
0
 /**
  * Returns true if and only if $value is a valid postalcode
  *
  * @param  string $value
  * @return bool
  * @throws Exception\InvalidArgumentException
  */
 public function isValid($value)
 {
     if (!is_string($value) && !is_int($value)) {
         $this->error(self::INVALID);
         return false;
     }
     $this->setValue($value);
     $service = $this->getService();
     $locale = $this->getLocale();
     $format = $this->getFormat();
     if ((null === $format || '' === $format) && !empty($locale)) {
         $region = Locale::getRegion($locale);
         if ('' === $region) {
             throw new Exception\InvalidArgumentException("Locale must contain a region");
         }
         if (isset(static::$postCodeRegex[$region])) {
             $format = static::$postCodeRegex[$region];
         }
     }
     if (null === $format || '' === $format) {
         throw new Exception\InvalidArgumentException("A postcode-format string has to be given for validation");
     }
     if ($format[0] !== '/') {
         $format = '/^' . $format;
     }
     if ($format[strlen($format) - 1] !== '/') {
         $format .= '$/';
     }
     if (!empty($service)) {
         if (!is_callable($service)) {
             throw new Exception\InvalidArgumentException('Invalid callback given');
         }
         try {
             $callback = new Callback($service);
             $callback->setOptions(array('format' => $format, 'locale' => $locale));
             if (!$callback->isValid($value)) {
                 $this->error(self::SERVICE, $value);
                 return false;
             }
         } catch (\Exception $e) {
             $this->error(self::SERVICEFAILURE, $value);
             return false;
         }
     }
     if (!preg_match($format, $value)) {
         $this->error(self::NO_MATCH);
         return false;
     }
     return true;
 }
Пример #5
0
 /**
  * Returns true if and only if $value is a valid postalcode
  *
  * @param  string $value
  * @return boolean
  */
 public function isValid($value)
 {
     $this->setValue($value);
     if (!is_string($value) && !is_int($value)) {
         $this->error(self::INVALID);
         return false;
     }
     $service = $this->getService();
     if (!empty($service)) {
         try {
             $callback = new Callback($service);
             $callback->setOptions(array('format' => $this->options['format'], 'locale' => $this->options['locale']));
             if (!$callback->isValid($value)) {
                 $this->error(self::SERVICE, $value);
                 return false;
             }
         } catch (\Exception $e) {
             $this->error(self::SERVICEFAILURE, $value);
             return false;
         }
     }
     $format = $this->getFormat();
     if (!preg_match($format, $value)) {
         $this->error(self::NO_MATCH);
         return false;
     }
     return true;
 }
 /**
  * Is the data set valid?
  *
  * @throws RuntimeException
  * @return bool
  */
 public function isValid()
 {
     if (true === ($valid = parent::isValid())) {
         $validator = new Callback(array('callback' => function ($values) {
             if (isset($values['query']) || isset($values['latitude']) && isset($values['longitude']) || isset($values['id'])) {
                 return true;
             }
         }, 'messages' => array(Callback::INVALID_VALUE => 'Requires a query, id, or latitude and longitude')));
         if (true === ($valid = $validator->isValid($this->getValues()))) {
             $this->validInputs['atLeastOne'] = $validator;
         } else {
             $this->invalidInputs['atLeastOne'] = $validator;
         }
     }
     return $valid;
 }