You are here

public function DateObject::arrayErrors in Date 7.3

Same name and namespace in other branches
  1. 7 date_api/date_api.module \DateObject::arrayErrors()
  2. 7.2 date_api/date_api.module \DateObject::arrayErrors()

Finds possible errors in an array of date part values.

The forceValid() function will change an invalid value to a valid one, so we just need to see if the value got altered.

Parameters

array $arr: An array of date values, keyed by date part.

Return value

array An array of error messages, keyed by date part.

2 calls to DateObject::arrayErrors()
DateObject::parse in date_api/date_api.module
Converts a date string into a date object.
DateObject::__construct in date_api/date_api.module
Constructs a date object.

File

date_api/date_api.module, line 970
This module will make the date API available to other modules.

Class

DateObject
Extend PHP DateTime class.

Code

public function arrayErrors(array $arr) {
  $errors = array();
  $now = date_now();
  $default_month = !empty($arr['month']) ? $arr['month'] : $now
    ->format('n');
  $default_year = !empty($arr['year']) ? $arr['year'] : $now
    ->format('Y');
  $this->granularity = array();
  foreach ($arr as $part => $value) {

    // Explicitly set the granularity to the values in the input array.
    if (is_numeric($value)) {
      $this
        ->addGranularity($part);
    }

    // Avoid false errors when a numeric value is input as a string by casting
    // as an integer.
    $value = intval($value);
    if (!empty($value) && $this
      ->forceValid($part, $value, 'now', $default_month, $default_year) != $value) {

      // Use a switch/case to make translation easier by providing a different
      // message for each part.
      switch ($part) {
        case 'year':
          $errors['year'] = t('The year is invalid.');
          break;
        case 'month':
          $errors['month'] = t('The month is invalid.');
          break;
        case 'day':
          $errors['day'] = t('The day is invalid.');
          break;
        case 'hour':
          $errors['hour'] = t('The hour is invalid.');
          break;
        case 'minute':
          $errors['minute'] = t('The minute is invalid.');
          break;
        case 'second':
          $errors['second'] = t('The second is invalid.');
          break;
      }
    }
  }
  if ($this
    ->hasTime()) {
    $this
      ->addGranularity('timezone');
  }
  return $errors;
}