You are here

protected function SearchApiDate::calculateRangeRelative in Facets 8

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

This method returns a start and end date with an relative 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::calculateRangeRelative()
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 155

Class

SearchApiDate
Support for date facets within the Search API scope.

Namespace

Drupal\facets\Plugin\facets\query_type

Code

protected function calculateRangeRelative($value) {
  $dateTime = new DrupalDateTime();
  switch ($this
    ->getGranularity()) {
    case static::FACETAPI_DATE_YEAR:
      $startDate = $dateTime::createFromFormat('Y-m-d\\TH:i:s', $value . '-01T00:00:00');
      $stopDate = clone $startDate;
      $stopDate
        ->add(new \DateInterval('P1Y'));
      $stopDate
        ->sub(new \DateInterval('PT1S'));
      break;
    case static::FACETAPI_DATE_MONTH:
      $startDate = $dateTime::createFromFormat('Y-m-d\\TH:i:s', $value . 'T00:00:00');
      $stopDate = clone $startDate;
      $stopDate
        ->add(new \DateInterval('P1M'));
      $stopDate
        ->sub(new \DateInterval('PT1S'));
      break;
    case static::FACETAPI_DATE_DAY:
      $startDate = $dateTime::createFromFormat('Y-m-d\\TH:i:s', $value . ':00:00');
      $stopDate = clone $startDate;
      $stopDate
        ->add(new \DateInterval('P1D'));
      $stopDate
        ->sub(new \DateInterval('PT1S'));
      break;
    case static::FACETAPI_DATE_HOUR:
      $startDate = $dateTime::createFromFormat('Y-m-d\\TH:i:s', $value . ':00');
      $stopDate = clone $startDate;
      $stopDate
        ->add(new \DateInterval('PT1H'));
      $stopDate
        ->sub(new \DateInterval('PT1S'));
      break;
    case static::FACETAPI_DATE_MINUTE:
      $startDate = $dateTime::createFromFormat('Y-m-d\\TH:i:s', $value);
      $stopDate = clone $startDate;
      $stopDate
        ->add(new \DateInterval('PT1M'));
      $stopDate
        ->sub(new \DateInterval('PT1S'));
      break;
    default:
      $startDate = $dateTime::createFromFormat('Y-m-d\\TH:i:s', $value);
      $stopDate = clone $startDate;
      break;
  }
  return [
    'start' => $startDate
      ->format('U'),
    'stop' => $stopDate
      ->format('U'),
  ];
}