You are here

protected function FeedsEntityProcessorPropertyDate::convertDate in Feeds entity processor 7

Converts a date string or object into a DateObject.

Parameters

DateTime|string|int $value: The date value or object.

DateTimeZone $default_tz: The default timezone.

Return value

DateObject The converted DateObject.

Throws

RuntimeException In case the DateObject class does not exist.

2 calls to FeedsEntityProcessorPropertyDate::convertDate()
FeedsEntityProcessorPropertyDate::setValue in src/Property/FeedsEntityProcessorPropertyDate.php
Implements FeedsEntityProcessorPropertyInterface::setValue().
FeedsEntityProcessorPropertyDate::validate in src/Property/FeedsEntityProcessorPropertyDate.php
Implements FeedsEntityProcessorPropertyInterface::validate().

File

src/Property/FeedsEntityProcessorPropertyDate.php, line 167
Contains FeedsEntityProcessorPropertyDate.

Class

FeedsEntityProcessorPropertyDate
Handler for date property.

Code

protected function convertDate($value, DateTimeZone $default_tz = NULL) {
  if (!class_exists('DateObject')) {
    if (!module_exist('date_api')) {
      throw new RuntimeException('Enable the date_api module to handle importing dates correctly.');
    }
    else {
      throw new RuntimeException('Class "DateObject" not found. Clear caches or rebuild the Drupal class registry and try again.');
    }
  }
  if (empty($timezone)) {
    $timezone = variable_get('date_default_timezone', 'UTC');
  }
  if ($value instanceof DateObject) {
    return $value;
  }

  // Convert DateTime and FeedsDateTime.
  if ($value instanceof DateTime) {
    if (!$value
      ->getTimezone() || !preg_match('/[a-zA-Z]/', $value
      ->getTimezone()
      ->getName())) {
      $value
        ->setTimezone($default_tz);
    }
    return new DateObject($value
      ->format(DATE_FORMAT_ISO), $value
      ->getTimezone());
  }
  if (is_string($value) || is_object($value) && method_exists($value, '__toString')) {
    $value = trim($value);
  }

  // Filter out meaningless values.
  if (empty($value) || !is_string($value) && !is_int($value)) {
    return FALSE;
  }

  // Support year values.
  if ((string) $value === (string) (int) $value) {
    if ($value >= variable_get('date_min_year', 100) && $value <= variable_get('date_max_year', 4000)) {
      return new DateObject('January ' . $value, $default_tz);
    }
  }
  return new DateObject($value, $default_tz);
}