You are here

protected function AvailabilityCalendarICalFeedsParser::parseVevent in Availability Calendars 7.5

Parses a VEVENT in a VCALENDAR feed.

Parameters

array $lines:

int $i:

Return value

bool|\stdClass

Throws

\Exception

1 call to AvailabilityCalendarICalFeedsParser::parseVevent()
AvailabilityCalendarICalFeedsParser::parseVcalendar in ./AvailabilityCalendarICalFeedsParser.inc
Parses a VCALENDAR feed into VEVENTS.

File

./AvailabilityCalendarICalFeedsParser.inc, line 82

Class

AvailabilityCalendarICalFeedsParser
@class ICalendar parser for availability calendars.

Code

protected function parseVevent(array $lines, &$i) {
  $vevent = new stdClass();
  $c = count($lines);
  while ($i < $c) {
    $parts = explode(':', $lines[$i++]);
    if (count($parts) !== 2) {
      return FALSE;
    }
    $property = strtoupper(trim($parts[0]));
    $value = $parts[1];
    $propertyParts = explode(';', $property);
    if (count($propertyParts) > 1) {
      $property = $propertyParts[0];
      $propertyType = strtoupper(trim($propertyParts[1]));
    }
    else {
      $propertyType = '';
    }
    switch ($property) {
      case 'UID':
        if ($propertyType !== '') {
          return FALSE;
        }
        $vevent->uid = $value;
        break;
      case 'SUMMARY':
        if ($propertyType !== '') {
          return FALSE;
        }
        $vevent->summary = $value;
        break;
      case 'DTSTART':
        if ($propertyType !== 'VALUE=DATE') {
          return FALSE;
        }
        $vevent->start = DateTime::createFromFormat('Ymd', $value)
          ->setTime(0, 0, 0, 0);
        break;
      case 'DTEND':
        if ($propertyType !== 'VALUE=DATE') {
          return FALSE;
        }

        // A DTEND indicates the departure day, we store a not-available block
        // from the arrival date just to the last full day, ie. the day before
        // the departure.
        $vevent->end = DateTime::createFromFormat('Ymd', $value)
          ->setTime(0, 0, 0, 0)
          ->sub(new DateInterval('P1D'));
        break;
      case 'END':
        if ($propertyType !== '') {
          return FALSE;
        }
        if (strtoupper(trim($value)) !== 'VEVENT') {
          return FALSE;
        }
        return !empty($vevent->start) && !empty($vevent->end) ? $vevent : FALSE;
        break;
      default:

        // We ignore other properties.
        break;
    }
  }
  return FALSE;
}