You are here

protected function SearchApiDate::calculateRangeAbsolute in Facets 8

Returns a start and end date based on a unix timestamp.

This method returns a start and end date with an absolute interval, based on the granularity set in the widget.

Parameters

int $value: Unix timestamp.

Return value

array An array with a start and end date as unix timestamps.

Throws

\Exception Thrown when creating a date fails.

1 call to SearchApiDate::calculateRangeAbsolute()
SearchApiDate::calculateRange in src/Plugin/facets/query_type/SearchApiDate.php
Calculate the range for a given facet filter value.

File

src/Plugin/facets/query_type/SearchApiDate.php, line 99

Class

SearchApiDate
Support for date facets within the Search API scope.

Namespace

Drupal\facets\Plugin\facets\query_type

Code

protected function calculateRangeAbsolute($value) {
  $dateTime = new DrupalDateTime();
  switch ($this
    ->getGranularity()) {
    case static::FACETAPI_DATE_YEAR:
      $startDate = $dateTime::createFromFormat('Y-m-d\\TH:i:s', $value . '-01-01T00:00:00');
      $stopDate = $dateTime::createFromFormat('Y-m-d\\TH:i:s', $value . '-12-31T23:59:59');
      break;
    case static::FACETAPI_DATE_MONTH:
      $startDate = $dateTime::createFromFormat('Y-m-d\\TH:i:s', $value . '-01T00:00:00');
      $stopDate = $dateTime::createFromFormat('Y-m-d\\TH:i:s', $value . '-' . $startDate
        ->format('t') . 'T23:59:59');
      break;
    case static::FACETAPI_DATE_DAY:
      $startDate = $dateTime::createFromFormat('Y-m-d\\TH:i:s', $value . 'T00:00:00');
      $stopDate = $dateTime::createFromFormat('Y-m-d\\TH:i:s', $value . 'T23:59:59');
      break;
    case static::FACETAPI_DATE_HOUR:
      $startDate = $dateTime::createFromFormat('Y-m-d\\TH:i:s', $value . ':00:00');
      $stopDate = $dateTime::createFromFormat('Y-m-d\\TH:i:s', $value . ':59:59');
      break;
    case static::FACETAPI_DATE_MINUTE:
      $startDate = $dateTime::createFromFormat('Y-m-d\\TH:i:s', $value . ':00');
      $stopDate = $dateTime::createFromFormat('Y-m-d\\TH:i:s', $value . ':59');
      break;
    default:
      $startDate = $dateTime::createFromFormat('Y-m-d\\TH:i:s', $value);
      $stopDate = $dateTime::createFromFormat('Y-m-d\\TH:i:s', $value);
      break;
  }
  return [
    'start' => $startDate
      ->format('U'),
    'stop' => $stopDate
      ->format('U'),
  ];
}