xp-forge/ical

v4.2.0 2024-03-24 10:45 UTC

README

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.0+ Supports PHP 8.0+ Latest Stable Version

I/O

可以使用 ICalendar 类读取和写入日历

use text\ical\ICalendar;
use util\cmd\Console;
use io\File;

$ical= new ICalendar();

$calendar= $ical->read('BEGIN:VCALENDAR...');
$calendar= $ical->read(Console::$in->stream());
$calendar= $ical->read(new File('meeting.ics'));

$ical->write($calendar, Console::$out->stream());
$ical->write($calendar, new File('meeting.ics'));

事件

通常一个日历包含一个事件,尽管格式允许任何数量,包括没有。

使用第一个事件,典型用例

$event= $calendar->events()->first();

当没有事件时,为了防止出现 lang.ElementNotFoundException,首先进行检查

$events= $calendar->events(); 
if ($events->present()) {
  $event= $events->first();
} else {
  // Handle situation when no events are inside calendar
}

处理所有事件

foreach ($calendar->events() as $event) {
  // ...
}

创建

可以使用流畅的接口创建日历实例

use text\ical\{
  Calendar,
  Event,
  Organizer,
  Attendee,
  IDate,
  Text,
  Method,
  Role,
  PartStat
};

$calendar= Calendar::with()
  ->method(Method::REQUEST)
  ->prodid('Microsoft Exchange Server 2010')
  ->version('2.0')
  ->events([Event::with()
    ->organizer(new Organizer('The Organizer', 'MAILTO:organizer@example.com'))
    ->attendees([
      Attendee::with()
        ->role(Role::CHAIR)
        ->partstat(PartStat::NEEDS_ACTION)
        ->rsvp('TRUE')
        ->cn('The Attendee 1')
        ->value('MAILTO:attendee2@example.com')
        ->create()
      ,
      Attendee::with()
        ->role(Role::REQ_PARTICIPANT)
        ->partstat(PartStat::NEEDS_ACTION)
        ->rsvp('TRUE')
        ->cn('The Attendee 2')
        ->value('MAILTO:attendee3@example.com')
        ->create()
    ])
    ->dtstart(new IDate(null, '20160524T183000Z'))
    ->dtend(new IDate(null, '20160524T190000Z'))
    ->location(new Text('de-DE', 'BS 50 EG 0102'))
    ->summary(new Text('de-DE', 'Treffen'))
    ->create()
  ])
  ->create()
;