Skip to content

Reading input ​

Choose a method based on where the .ics data comes from. Successful reads return a Calendar.

String contents ​

php
use Mattmy\ICalendar\Facades\ICalendar;

$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.

Local path ​

php
use Mattmy\ICalendar\Facades\ICalendar;

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

$path must identify an existing, readable, regular local file. URL wrappers are rejected.

Stream ​

php
use Mattmy\ICalendar\Facades\ICalendar;

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

if ($stream === false) {
    throw new RuntimeException('Could not open the calendar file.');
}

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

Pass 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
use Mattmy\ICalendar\Facades\ICalendar;

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

Pass an Illuminate\Http\UploadedFile. 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 CalendarFileNotFound / CalendarFileUnreadablesame exception
Invalid stream/uploadthrows InvalidCalendarSourcesame exception
Exceeds max_bytesthrows CalendarTooLargesame exception
Invalid max_bytesthrows InvalidConfigurationsame exception

Use the throwing method for details about invalid content, or its try*() counterpart when null is enough.

Last updated:

Released under the MIT License.