Exemple #1
0
 /**
  * Construct validator rule.
  *
  * @param array $rules
  *            Associative array of validation rules for field.
  */
 public function __construct($rules = array())
 {
     $this->rules = $rules;
     if (isset($this->rules['message'])) {
         $this->message = \Jivoo\I18n\I18n::get($this->rules['message']);
         unset($this->rules['message']);
     }
 }
Exemple #2
0
 public function handleException(\Exception $exception)
 {
     $this->lastError = $exception;
     if (isset($this->options['trace'])) {
         $this->error(I18n::get('Uncaught exception'));
         self::dumpException($exception);
     } else {
         $this->error(I18n::get('Uncaught %1: %2', get_class($exception), $exception->getMessage()));
         $this->put();
         $this->put(I18n::get('Call "trace" or run script with the "--trace" option to show stack trace'));
     }
 }
Exemple #3
0
 /**
  * Localized date function.
  *
  * Works like {@see date()} but translates month names and weekday names using
  * the current {@see getLocale()}.
  *
  * @param string $format
  *            The format of the outputted date string. See
  *            {@link http://php.net/manual/en/function.date.php date()}
  * @param int $timestamp
  *            Optional Unix timestamp to use. Default is value of
  *            {@see time()}
  * @return string Formatted date string.
  */
 public static function date($format, $timestamp = null)
 {
     if (is_null($timestamp)) {
         $timestamp = time();
     }
     $month = date('n', $timestamp);
     if ($month == 1) {
         $F = I18n::get('January');
     } elseif ($month == 2) {
         $F = I18n::get('February');
     } elseif ($month == 3) {
         $F = I18n::get('March');
     } elseif ($month == 4) {
         $F = I18n::get('April');
     } elseif ($month == 5) {
         $F = I18n::get('May');
     } elseif ($month == 6) {
         $F = I18n::get('June');
     } elseif ($month == 7) {
         $F = I18n::get('July');
     } elseif ($month == 8) {
         $F = I18n::get('August');
     } elseif ($month == 9) {
         $F = I18n::get('September');
     } elseif ($month == 10) {
         $F = I18n::get('October');
     } elseif ($month == 11) {
         $F = I18n::get('November');
     } elseif ($month == 12) {
         $F = I18n::get('December');
     }
     $M = Unicode::slice($F, 0, 3);
     $weekday = date('w', $timestamp);
     if ($weekday == 0) {
         $l = I18n::get('Sunday');
     } elseif ($weekday == 1) {
         $l = I18n::get('Monday');
     } elseif ($weekday == 2) {
         $l = I18n::get('Tuesday');
     } elseif ($weekday == 3) {
         $l = I18n::get('Wednesday');
     } elseif ($weekday == 4) {
         $l = I18n::get('Thursday');
     } elseif ($weekday == 5) {
         $l = I18n::get('Friday');
     } elseif ($weekday == 6) {
         $l = I18n::get('Saturday');
     }
     $D = Unicode::slice($l, 0, 3);
     $date = date($format, $timestamp);
     $date = str_replace(date('F', $timestamp), $F, $date);
     $date = str_replace(date('M', $timestamp), $M, $date);
     $date = str_replace(date('l', $timestamp), $l, $date);
     $date = str_replace(date('D', $timestamp), $D, $date);
     return $date;
 }
Exemple #4
0
 /**
  * Validate a rule on a record.
  *
  * @param Record $record
  *            A record.
  * @param string $field
  *            Field name.
  * @param string $ruleName
  *            Name of rule.
  * @param mixed $rule
  *            Value of rule.
  * @return true|string True if valid, otherwise returns an error message.
  */
 public static function validateRule(Record $record, $field, $ruleName, $rule)
 {
     if ($rule instanceof ValidatorRule) {
         return $rule->validate($record, $field);
     }
     $value = $record->{$field};
     if ($ruleName != 'presence' and $ruleName != 'null' and $ruleName != 'callback' and ($value == null or $value == '')) {
         return true;
     }
     // if (!is_scalar($value)) {
     // return I18n::get('Must be a scalar.');
     // }
     switch ($ruleName) {
         case 'type':
             return $rule->isValid($value);
         case 'presence':
             if ((!empty($value) or is_numeric($value)) == $rule) {
                 return true;
             }
             return $rule ? I18n::get('Must not be empty.') : I18n::get('Must be empty.');
         case 'null':
             if (is_null($value) == $rule) {
                 return true;
             }
             return $rule ? I18n::get('Must not be set.') : I18n::get('Must be set.');
         case 'email':
             if ((preg_match(self::EMAIL_REGEX, $value) == 1) == $rule) {
                 return true;
             }
             return $rule ? I18n::get('Not a valid email address.') : I18n::get('Must not be an email address.');
         case 'url':
             if ((preg_match("/^https?:\\/\\/[-a-z0-9@:%_\\+\\.~#\\?&\\/=\\[\\]]+\$/i", $value) == 1) == $rule) {
                 return true;
             }
             return $rule ? I18n::get('Not a valid URL.') : I18n::get('Must not be a URL.');
         case 'date':
             $timestamp = false;
             if (preg_match('/^[-+]?\\d+$/', $value) == 1) {
                 $timestamp = (int) $value;
             } else {
                 $timestamp = strtotime($value);
             }
             if (($timestamp !== false) == $rule) {
                 return true;
             }
             return $rule ? I18n::get('Not a valid date.') : I18n::get('Must not be a date.');
         case 'minLength':
             if (strlen($value) >= $rule) {
                 return true;
             }
             return I18n::nget('Must be at least %1 characters long.', 'Must be at least %1 character long.', $rule);
         case 'maxLength':
             if (strlen($value) <= $rule) {
                 return true;
             }
             return I18n::nget('Must be at most %1 characters long.', 'Must be at most %1 character long.', $rule);
         case 'numeric':
             if (is_numeric($value) == $rule) {
                 return true;
             }
             return $rule ? I18n::get('Must be numeric.') : I18n::get('Must not be numeric.');
         case 'integer':
             if ((preg_match('/^[-+]?\\d+$/', $value) == 1) == $rule) {
                 return true;
             }
             return $rule ? I18n::get('Must be an integer.') : I18n::get('Must not be an integer.');
         case 'float':
             if ((preg_match('/^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$/', $value) == 1) == $rule) {
                 return true;
             }
             return $rule ? I18n::get('Must be a decimal number.') : I18n::get('Must not be a decimal number.');
         case 'boolean':
             if ((preg_match('/^(0|1|true|false|yes|no)$/i', $value) == 1) == $rule) {
                 return true;
             }
             return $rule ? I18n::get('Must be boolean (true or false).') : I18n::get('Must not be boolean.');
         case 'minValue':
             $value = is_float($rule) ? (double) $value : (int) $value;
             if ($value >= $rule) {
                 return true;
             }
             return I18n::get('Must be greater than or equal to %1.', $rule);
         case 'maxValue':
             $value = is_float($rule) ? (double) $value : (int) $value;
             if ($value <= $rule) {
                 return true;
             }
             return I18n::get('Must be less than or equal to %1.', $rule);
         case 'in':
             if (in_array($value, $rule)) {
                 return true;
             }
             return I18n::nget('Must be either "%1{", "}{" or "}".', 'Must be "%1{", "}{" or "}".', $rule);
         case 'match':
             if (preg_match($rule, $value) == 1) {
                 return true;
             }
             return I18n::get('Invalid value.');
         case 'unique':
             $selection = $record->getModel();
             if (!$record->isNew()) {
                 $selection = $selection->selectNotRecord($record);
             }
             if (($selection->where($field . ' = ?', $value)->countSelection() == 0) == $rule) {
                 return true;
             }
             return $rule ? I18n::get('Must be unique.') : I18n::get('Must not be unique.');
         case 'callback':
             if (!is_array($rule)) {
                 $rule = array($record->getModel(), $rule);
             }
             if (!is_callable($rule)) {
                 return true;
             }
             return call_user_func($rule, $record, $field);
     }
     return true;
 }
Exemple #5
0
 public function __invoke(array $parameters, array $options)
 {
     if (count($parameters) == 0) {
         return $this->onEmpty();
     }
     $command = array_shift($parameters);
     if (!isset($this->commands[$command])) {
         $this->m->shell->put(I18n::get('Unknown command: %1', $command));
         return;
     }
     call_user_func($this->commands[$command], $parameters, $options);
 }
Exemple #6
0
 /**
  * {@inheritdoc}
  */
 public function getLabel($field)
 {
     if (!isset($this->labels[$field])) {
         $this->labels[$field] = ucfirst(strtolower(preg_replace('/([A-Z])/', ' $1', lcfirst($field))));
     }
     return \Jivoo\I18n\I18n::get($this->labels[$field]);
 }