Download the PHP package clubmaster/vobject without Composer
On this page you can find all versions of the php package clubmaster/vobject. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download clubmaster/vobject
More information about clubmaster/vobject
Files in clubmaster/vobject
Package vobject
Short Description The VObject library for PHP allows you to easily parse and manipulate iCalendar and vCard objects
License BSD-3-Clause
Homepage https://github.com/evert/sabre-vobject
Informations about the package vobject
SabreTooth VObject library
The VObject library allows you to easily parse and manipulate iCalendar and vCard objects using PHP. The goal of the VObject library is to create a very complete library, with an easy to use API.
This project is a spin-off from SabreDAV, where it has been used for several years. The VObject library has 100% unittest coverage.
Installation
VObject requires PHP 5.3, and should be installed using composer. The general composer instructions can be found on the [composer website](http://getcomposer.org/doc/00-intro.md composer website).
After that, just declare the vobject dependency as follows:
Then, run composer.phar update
and you should be good. As soon as the first release is out, you should switch dev-master
to 2.0.*
though.
Usage
Parsing
For our example, we will be using the following vcard:
If we want to just print out Max' full name, you can just use property access:
Changing properties
Creating properties is pretty similar. If we like to add his birthday, we just set the property:
Note that in the previous example, we're actually updating any existing BDAY that
may already exist. If we want to add a new property, without overwriting the previous
we can do this with the add
method.
Parameters
If we want to also specify that this is max' home email addresses, we can do this with a third parameter:
If we want to read out all the email addresses and their type, this would look something like this:
Groups
In our example, you can see that the TEL properties are prefixed. These are 'groups' and allow you to group multiple related properties together. The group can be any user-defined name.
This particular example as generated by the OS X addressbook, which uses the X-ABLabel
to allow the user to specify custom labels for properties. OS X addressbook uses groups
to tie the label to the property.
The VObject library simply ignores the group if you don't specify it, so this will work:
But if you would like to target a specific group + property, this is possible too:
So if you would like to show all the phone numbers, along with their custom label, the following syntax is used:
Serializing / Saving
If you want to generate your updated VObject, you can simply call the serialize() method.
Components
iCalendar, unlike vCards always have sub-components. Where vCards are often just a flat list, iCalendar objects tend to have a tree-like structure. For the following paragraphs, we will use the following object as the example:
Since events, tasks and journals are always in a sub component, this is also how we access them.
Adding components to a calendar is done with a factory method:
By the way.. cloning also works as expected, as the entire structure is cloned along with it:
Date and time handling
If you ever had to deal with iCalendar timezones, you know it can be complicated. The way timezones are specified is flawed, which is something I may write an essay about some day. VObject does its best to determine the correct timezone. Many standard formats have been tested and verified, and special code has been implemented for special-casing microsoft generated timezone information, and others.
To get a real php DateTime
object, you can request this as follows:
To set the property with a DateTime object, you can use the following syntax:
The second argument specifies the type of date you're setting. The following three options exist:
LOCAL
This is a floating time, with no timezone information. This basically specifies that the event happens in whatever the timezone's currently in. This would be encoded asDTSTART;VALUE=DATE-TIME:20120807235300
UTC
This specifies that the time should be encoded as a UTC time. This is encoded asDTSTART;VALUE=DATE-TIME:20120807205300Z
. Note the extra Z and the fact that it's two hours 'earlier'.LOCALTZ
specifies that it's supposed to be encoded in its local timezone. For exampleDTSTART;VALUE=DATE-TIME;TZID=Europe/Amsterdam:20120807235300
.DATE
This is a date-only, and does not contain the time. In this case this would be encoded asDTSTART;VALUE=DATE:20120807
.
A few important notes:
- When a
TZID
is specified, there should also be a matchingVTIMEZONE
object with all the timezone information. VObject cannot currently automatically embed this. However, in reality other clients seem to do fine without this information. Yet, for completeness, this will be added in the future. - As mentioned, the timezone-determination process may sometimes fail. Report any issues you find, and I'll be quick to add workarounds!
Recurrence rules
Recurrence rules allow events to recur, for example for a weekly meeting, or an anniversary.
This is done with the RRULE
property. The RRULE
property allows for a LOT of different
rules. VObject only implements the ones that actually appear in calendar software.
To read more about RRULE
and all the options, check out RFC5545.
VObject supports the following options:
UNTIL
for an end date.INTERVAL
for for example "every 2 days".COUNT
to stop recurring after x items.FREQ=DAILY
to recur every day, andBYDAY
to limit it to certain days.FREQ=WEEKLY
to recur every week,BYDAY
to expand this to multiple weekdays in every week andWKST
to specify on which day the week starts.FREQ=MONTHLY
to recur every month,BYMONTHDAY
to expand this to certain days in a month,BYDAY
to expand it to certain weekdays occuring in a month, andBYSETPOS
to limit the last two expansions.FREQ=YEARLY
to recur every year,BYMONTH
to expand that to certain months in a year, andBYDAY
andBYWEEKDAY
to expand theBYMONTH
rule even further.
VObject supports the EXDATE
property for exclusions, but not yet the RDATE
and EXRULE
properties. If you're interested in this, please file a github issue, as this will put it
on my radar.
This is a bit of a complex subject to go in excruciating detail. The RFC has a lot of examples though.
The hard part is not to write the RRULE, it is to expand them. The most complex and hard-to-read code is hidden in this component. Dragons be here.
So, if we have a meeting every 2nd monday of the month, this would be specified as such:
Note that normally it's not allowed to indent the object like this, but it does make it easier to read. This is also the first time I added in a UID, which is required for all VEVENT, VTODO and VJOURNAL objects!
To figure out all the meetings for this year, we can use the following syntax:
What the expand method does, is look at its inner events, and expand the recurring
rule. Our calendar now contains 12 events. The first will have its RRULE stripped,
and every subsequent VEVENT has the correct meeting date and a RECURRENCE-ID
set.
This results in something like this:
To show the list of dates, we would do this as such:
In a recurring event, single instances can also be overriden. VObject also takes these into consideration. The reason we needed to specify a start and end-date, is because some recurrence rules can be 'never ending'.
You should make sure you pick a sane date-range. Because if you pick a 50 year time-range, for a daily recurring event; this would result in over 18K objects.
Free-busy report generation
Some calendaring software can make use of FREEBUSY reports to show when people are available.
You can automatically generate these reports from calendars using the FreeBusyGenerator
.
Example based on our last event:
The output of this script will look like this: