iCal PHP Package
The iCal PHP package offers an abstraction layer for creating iCalendars files.
By using this PHP package, you can create *.ics
files without the knowledge of the underling format.
The output itself will follow RFC 5545 as good as possible.
Usage
The classes within this package are grouped into two namespaces:
- The
Domain
contains the information about the events. - The
Presentation
contains the transformation fromDomain
into a*.ics
file.
To create a calendar, the first step will be to create the corresponding domain objects. Then these objects can be transformed into a iCalendar PHP representation, which can be cast to string.
Empty event
In this very basic example, that renders an empty event. You will learn how to create an event domain object, how to add it to a calendar and how to transform it to a iCalendar component.
1. Create an event domain entity
2. Create a calendar domain entity
3. Transform calendar domain object into a presentation object
$iCalendarComponent = (new \OpenCal\iCal\Presentation\Factory\CalendarFactory())->createCalendar($calendar);
4. a) Save to file
4. b) Send via HTTP
header('Content-Type: text/calendar; charset=utf-8');
header('Content-Disposition: attachment; filename="cal.ics"');
echo $iCalendarComponent;
Full example
The following example will create a single day event with a summary and a description. More examples can be found in the examples/ folder.
<?php
require_once __DIR__ . '/../vendor/autoload.php';
// 1. Create Event domain entity
$event = (new OpenCal\iCal\Domain\Entity\Event())
->setSummary('Christmas Eve')
->setDescription('Lorem Ipsum Dolor...')
->setOccurrence(
new OpenCal\iCal\Domain\ValueObject\SingleDay(
new OpenCal\iCal\Domain\ValueObject\Date(
\DateTimeImmutable::createFromFormat('Y-m-d', '2030-12-24')
)
)
);
// 2. Create Calendar domain entity
$calendar = new OpenCal\iCal\Domain\Entity\Calendar([$event]);
// 3. Transform domain entity into an iCalendar component
$componentFactory = new OpenCal\iCal\Presentation\Factory\CalendarFactory();
$calendarComponent = $componentFactory->createCalendar($calendar);
// 4. Set headers
header('Content-Type: text/calendar; charset=utf-8');
header('Content-Disposition: attachment; filename="cal.ics"');
// 5. Output
echo $calendarComponent;
License
This package is released under the MIT license.