Skip to content

Reading input

Choose a method based on where the .ics data comes from. Every successful method returns the same Calendar object.

String contents

php
$calendar = ICalendar::read($contents);
$calendar = ICalendar::tryRead($contents);

$contents is the complete iCalendar byte string, not a path or URL. read() returns Calendar; tryRead() returns null when the content is not a valid iCalendar document. Both still report size and configuration errors as exceptions.

Local path

php
$calendar = ICalendar::fromPath($path);
$calendar = ICalendar::tryFromPath($path);

$path must identify an existing, readable, regular local file. URL wrappers are rejected. The methods may throw CalendarFileNotFound, CalendarFileUnreadable, CalendarTooLarge, or InvalidConfiguration; the try variant does not hide them.

Stream

php
$stream = fopen($path, 'rb');

try {
    $calendar = ICalendar::fromStream($stream);
} finally {
    fclose($stream);
}

$stream accepts a readable PHP stream resource. Reading starts from its current position. The package does not rewind or close the stream for you. Passing a non-stream or write-only stream throws InvalidCalendarSource; an I/O failure throws CalendarFileUnreadable. tryFromStream() only converts InvalidCalendar to null.

UploadedFile

php
$calendar = ICalendar::fromUploadedFile($request->file('calendar'));
$calendar = ICalendar::tryFromUploadedFile($request->file('calendar'));

$file is an Illuminate\Http\UploadedFile. It provides the .ics content uploaded with a Laravel request. Client MIME type and filename do not prove that its content is valid. Validate that the request contains a file before calling this method.

Exceptions or null

Failureread*()try*()
Invalid iCalendar contentthrows InvalidCalendarreturns null
Missing/unreadable filethrows source exceptionsame exception
Invalid stream/uploadthrows InvalidCalendarSourcesame exception
Exceeds max_bytesthrows CalendarTooLargesame exception
Invalid max_bytesthrows InvalidConfigurationsame exception

Use read*() when you want details about invalid content. Use try*() when null is enough to tell your application that the user supplied an invalid calendar.

Last updated:

Released under the MIT License.