Skip to content

Commit

Permalink
Closes #123 - allow the parser to silently ignore events with an inva…
Browse files Browse the repository at this point in the history
…lid start date

Update README
  • Loading branch information
u01jmg3 committed Apr 6, 2017
1 parent 3b100ef commit 33bf92f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
}
```

### How to instantiate the Parser

- Using the example script as a guide, [use this code](https://github.com/u01jmg3/ics-parser/blob/master/examples/index.php#L1-L12)

---

## API
Expand Down Expand Up @@ -82,6 +86,7 @@
| `hasEvents` | - | `public` | Returns a boolean value whether the current calendar has events or not |
| `iCalDateToUnixTimestamp` | `$icalDate` | `public` | Return Unix timestamp from iCal date time format |
| `iCalDateWithTimeZone` | `$event`, `$key`, `$forceTimeZone` | `public` | Return a date adapted to the calendar timezone depending on the event TZID |
| `isValidDate` | `$value` | `public` | Check if a date string is a valid date |
| `parseExdates` | `$event` | `public` | Parse a list of excluded dates to be applied to an Event |
| `sortEventsWithOrder` | `$events`, `$sortOrder = SORT_ASC` | `public` | Sort events based on a given sort order |

Expand Down
10 changes: 10 additions & 0 deletions examples/iCal.ics
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ DTEND:20171046T120000Z
DTSTAMP:20170901T083000Z
END:VFREEBUSY
BEGIN:VEVENT
DTSTART:20171032T000000
DTEND:20171101T2300
DESCRIPTION:Invalid date - parser will skip the event
SUMMARY:Invalid date - parser will skip the event
DTSTAMP:20170406T063924
LOCATION:
UID:f81b0b41a2e138ae0903daee0a966e1e
SEQUENCE:0
END:VEVENT
BEGIN:VEVENT
DTSTART;VALUE=DATE:19410512
DTEND;VALUE=DATE:19410512
DTSTAMP;TZID="GMT Standard Time":19410512T195741Z
Expand Down
28 changes: 28 additions & 0 deletions src/ICal/ICal.php
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,13 @@ protected function processDateConversions()
}

foreach ($events as $key => $anEvent) {
if (!$this->isValidDate($anEvent['DTSTART'])) {
unset($events[$key]);
$this->eventCount--;

continue;
}

if ($this->useTimeZoneWithRRules && isset($anEvent['RRULE_array'][2]) && $anEvent['RRULE_array'][2] === self::RECURRENCE_EVENT) {
$events[$key]['DTSTART_tz'] = $anEvent['DTSTART'];
$events[$key]['DTEND_tz'] = $anEvent['DTEND'];
Expand Down Expand Up @@ -1803,4 +1810,25 @@ public function parseExdates(array $event)

return $output;
}

/**
* Check if a date string is a valid date
*
* @param string $value
* @return boolean
*/
public function isValidDate($value)
{
if (!$value) {
return false;
}

try {
new \DateTime($value);

return true;
} catch (\Exception $e) {
return false;
}
}
}

0 comments on commit 33bf92f

Please sign in to comment.