/**
  * {@inheritdoc}
  *
  * @param  \Phalcon\Mvc\ModelInterface $record
  * @return boolean
  */
 public function validate($record)
 {
     $field = $this->getOption('field');
     $fieldConfirmation = $this->getOption('field_confirmation');
     $fieldValue = $record->readAttribute($field);
     $fieldConfirmationValue = $record->readAttribute($fieldConfirmation);
     $message = $this->getOption('message') ? $this->getOption('message') : 'Both fields should contain equal values';
     if ($fieldConfirmationValue) {
         if ($fieldValue !== $fieldConfirmationValue) {
             $this->appendMessage($message, $fieldConfirmation, 'ConfirmationOf');
             return false;
         }
     }
     return true;
 }
Ejemplo n.º 2
0
 /**
  *  {@inheritdoc}
  *
  * @param string                      $eventType
  * @param \Phalcon\Mvc\ModelInterface $model
  */
 public function notify($eventType, ModelInterface $model)
 {
     if ($eventType == 'beforeDelete') {
         $options = $this->getOptions();
         $field = $options['field'];
         $value = $options['value'];
         $model->skipOperation(true);
         if ($model->readAttribute($field) === $value) {
             $model->appendMessage(new Message('Model was already deleted'));
             return false;
         }
         $this->fireEvent($model, 'beforeSoftDelete');
         $updateModel = clone $model;
         $updateModel->writeAttribute($field, $value);
         if (!$updateModel->update()) {
             foreach ($updateModel->getMessages() as $message) {
                 $model->appendMessage($message);
             }
             return false;
         }
         $model->writeAttribute($field, $value);
         if (isset($options['cascade']) && $options['cascade'] === true) {
             $this->cascadeDelete($model);
         }
         $this->fireEvent($model, 'afterSoftDelete');
     }
 }
Ejemplo n.º 3
0
 public function validate(ModelInterface $record)
 {
     $field = $this->getOption('field');
     if (false === is_string($field)) {
         throw new Exception('Field name must be a string');
     }
     $fieldValue = $record->readAttribute($field);
     $value = preg_replace('/[^\\d]/', '', $fieldValue);
     if ($this->isSetOption('type')) {
         $type = $this->getOption('type');
         $result = true;
         switch ($type) {
             case CardNumber::AMERICAN_EXPRESS:
                 $issuer = substr($value, 0, 2);
                 $result = true === in_array($issuer, array(34, 37));
                 break;
             case CardNumber::MASTERCARD:
                 $issuer = substr($value, 0, 2);
                 $result = true === in_array($issuer, array(51, 52, 53, 54, 55));
                 break;
             case CardNumber::VISA:
                 $issuer = $value[0];
                 $result = $issuer == 4;
                 break;
             default:
                 throw new Exception('Incorrect type specifier');
         }
         if (false === $result) {
             $message = $this->getOption('message') ?: 'Credit card number is invalid';
             $this->appendMessage($message, $field, "CardNumber");
             return false;
         }
     }
     $value = strrev($value);
     $checkSum = 0;
     for ($i = 0; $i < strlen($value); $i++) {
         if ($i % 2 == 0) {
             $temp = $value[$i];
         } else {
             $temp = $value[$i] * 2;
             if ($temp > 9) {
                 $temp -= 9;
             }
         }
         $checkSum += $temp;
     }
     if ($checkSum % 10 != 0) {
         $message = $this->getOption('message') ?: 'Credit card number is invalid';
         $this->appendMessage($message, $field, "CardNumber");
         return false;
     }
     return true;
 }
Ejemplo n.º 4
0
 public function validate(ModelInterface $record)
 {
     $field = $this->getOption('field');
     if (false === is_string($field)) {
         throw new Exception('Field name must be a string');
     }
     $value = $record->readAttribute($field);
     $version = $this->getOption('version') ?: FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6;
     $allowPrivate = $this->getOption('allowPrivate') ? 0 : FILTER_FLAG_NO_PRIV_RANGE;
     $allowReserved = $this->getOption('allowReserved') ? 0 : FILTER_FLAG_NO_RES_RANGE;
     $options = array('options' => array('default' => false), 'flags' => $version | $allowPrivate | $allowReserved);
     $result = filter_var($value, FILTER_VALIDATE_IP, $options);
     if (false === $result) {
         $message = $this->getOption('message') ?: 'IP address is incorrect';
         $this->appendMessage($message, $field, "IP");
     }
     return (bool) $result;
 }
Ejemplo n.º 5
0
 /**
  * {@inheritdoc}
  *
  * @param $record
  *
  * @return boolean
  * @throws Exception
  */
 public function validate(ModelInterface $record)
 {
     $field = $this->getOption('field');
     if (false === is_string($field)) {
         throw new Exception('Field name must be a string');
     }
     $value = $record->readAttribute($field);
     if (true === $this->isSetOption('allowEmpty') && empty($value)) {
         return true;
     }
     if (false === $this->isSetOption('min') || false === $this->isSetOption('max')) {
         throw new Exception('A minimum and maximum must be set');
     }
     $maximum = $this->getOption('max');
     $minimum = $this->getOption('min');
     if ($value < $minimum || $value > $maximum) {
         // Check if the developer has defined a custom message
         $message = $this->getOption('message') ?: sprintf('%s is not between a valid range', $field);
         $this->appendMessage($message, $field, 'Between');
         return false;
     }
     return true;
 }
Ejemplo n.º 6
0
 /**
  * {@inheritdoc}
  *
  * @param $record
  * @return boolean
  * @throws Exception
  */
 public function validate(ModelInterface $record)
 {
     $field = $this->getOption('field');
     if (false === is_string($field)) {
         throw new Exception('Field name must be a string');
     }
     $value = $record->readAttribute($field);
     if (true === $this->isSetOption('allowEmpty') && empty($value)) {
         return true;
     }
     if (false === $this->isSetOption('places')) {
         throw new Exception('A number of decimal places must be set');
     }
     $places = $this->getOption('places');
     if ($this->isSetOption('digits')) {
         // Specific number of digits
         $digits = '{' . (int) $this->getOption('digits') . '}';
     } else {
         // Any number of digits
         $digits = '+';
     }
     if ($this->isSetOption('point')) {
         $decimal = $this->getOption('point');
     } else {
         // Get the decimal point for the current locale
         list($decimal) = array_values(localeconv());
     }
     $regexp = (bool) preg_match('#^[+-]?[0-9]' . $digits . preg_quote($decimal) . '[0-9]{' . (int) $places . '}$#', $value);
     if (!$regexp) {
         // Check if the developer has defined a custom message
         $message = $this->getOption('message') ?: sprintf('%s must contain valid decimal value', $field);
         $this->appendMessage($message, $field, 'Decimal');
         return false;
     }
     return true;
 }
Ejemplo n.º 7
0
 /**
  * @param \Phalcon\Mvc\ModelInterface $user
  * 
  * @return int
  */
 public function getUserIdFromUser(\Phalcon\Mvc\ModelInterface $user)
 {
     return $user->readAttribute($this->userIdField);
 }