Skip to content

Commit 708e086

Browse files
committed
ZCiCal::addDataNode
1 parent d2d86eb commit 708e086

File tree

4 files changed

+96
-13
lines changed

4 files changed

+96
-13
lines changed

examples/simpleEvent.ics

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
BEGIN:VCALENDAR
22
VERSION:2.0
3+
NAME:My Calendar Name
34
PRODID:-//ZContent.net//ZapCalLib 1.0//EN
45
CALSCALE:GREGORIAN
56
METHOD:PUBLISH
67
BEGIN:VEVENT
78
SUMMARY:Simple Event
89
DTSTART:20200101T120000
910
DTEND:20200101T130000
10-
UID:2023-02-14-02-16-35@demo.icalendar.org
11-
DTSTAMP:20230214T021635
12-
Description: This is a simple event, using the Zap Calendar PHP lib
13-
rary. Visit http://icalendar.org to validate icalendar files.
11+
UID:2024-12-26-15-58-12@demo.icalendar.org
12+
DTSTAMP:20241226T155812
13+
Description: This is a simple event, using the Zap Calendar PHP lib
14+
rary. Visit http://icalendar.org to validate icalendar files.
1415
END:VEVENT
1516
END:VCALENDAR

examples/simpleevent.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* simpleevent.php
45
*
@@ -25,6 +26,9 @@
2526
// create the ical object
2627
$icalobj = new \ICalendarOrg\ZCiCal();
2728

29+
// add name
30+
$icalobj->addDataNode(new \ICalendarOrg\ZCiCalDataNode('NAME:My Calendar Name'), new \ICalendarOrg\ZCiCalDataNode('PRODID:-//ZContent.net//ZapCalLib 1.0//EN'));
31+
2832
// create the event within the ical object
2933
$eventobj = new \ICalendarOrg\ZCiCalNode('VEVENT', $icalobj->curnode);
3034

src/ICalendarOrg/ZCiCal.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Create iCalendar data structure
45
*
@@ -210,16 +211,33 @@ public function __construct(string $data = '', int $maxevents = 1_000_000, int $
210211
$name = 'VCALENDAR';
211212
$this->curnode = new \ICalendarOrg\ZCiCalNode($name, $this->curnode);
212213
$this->tree = $this->curnode;
213-
$datanode = new \ICalendarOrg\ZCiCalDataNode('VERSION:2.0');
214-
$this->curnode->data[$datanode->getName()] = $datanode;
215-
216-
$datanode = new \ICalendarOrg\ZCiCalDataNode('PRODID:-//ZContent.net//ZapCalLib 1.0//EN');
217-
$this->curnode->data[$datanode->getName()] = $datanode;
218-
$datanode = new \ICalendarOrg\ZCiCalDataNode('CALSCALE:GREGORIAN');
219-
$this->curnode->data[$datanode->getName()] = $datanode;
220-
$datanode = new \ICalendarOrg\ZCiCalDataNode('METHOD:PUBLISH');
221-
$this->curnode->data[$datanode->getName()] = $datanode;
214+
$this->addDataNode(new \ICalendarOrg\ZCiCalDataNode('VERSION:2.0'));
215+
$this->addDataNode(new \ICalendarOrg\ZCiCalDataNode('PRODID:-//ZContent.net//ZapCalLib 1.0//EN'));
216+
$this->addDataNode(new \ICalendarOrg\ZCiCalDataNode('CALSCALE:GREGORIAN'));
217+
$this->addDataNode(new \ICalendarOrg\ZCiCalDataNode('METHOD:PUBLISH'));
218+
}
219+
}
220+
221+
public function addDataNode(\ICalendarOrg\ZCiCalDataNode $dataNode, ?\ICalendarOrg\ZCiCalDataNode $beforeNode = null) : ZCiCal
222+
{
223+
if (! $beforeNode)
224+
{
225+
$this->tree->data[$dataNode->getName()] = $dataNode;
226+
227+
return $this;
228+
}
229+
230+
// add name
231+
$insertPosition = \array_search($beforeNode->getName(), \array_keys($this->tree->data));
232+
233+
if (false === $insertPosition)
234+
{
235+
throw new \UnexpectedValueException('Node ' . $beforeNode->getName() . ' not found in ' . __CLASS__);
222236
}
237+
// Create a new array with the new element inserted
238+
$this->tree->data = \array_slice($this->tree->data, 0, $insertPosition) + [$dataNode->getName() => $dataNode] + \array_slice($this->tree->data, $insertPosition);
239+
240+
return $this;
223241
}
224242

225243
/**

tests/SimpleEventTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the ICalendarOrg package
5+
*
6+
* (c) Bruce Wells
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source
10+
* code
11+
*/
12+
class SimpleEventTest extends \PHPUnit\Framework\TestCase
13+
{
14+
public function testAddInvalidNode() : void
15+
{
16+
$this->expectException(\UnexpectedValueException::class);
17+
$icalobj = new \ICalendarOrg\ZCiCal();
18+
19+
$icalobj->addDataNode(new \ICalendarOrg\ZCiCalDataNode('NAME:My Calendar Name'), new \ICalendarOrg\ZCiCalDataNode('FRED:Flintstone'));
20+
}
21+
22+
public function testSimpleEvent() : void
23+
{
24+
$title = 'Simple Event';
25+
// date/time is in SQL datetime format
26+
$event_start = '2020-01-01 12:00:00';
27+
$event_end = '2020-01-01 13:00:00';
28+
29+
// create the ical object
30+
$icalobj = new \ICalendarOrg\ZCiCal();
31+
32+
// add name
33+
$icalobj->addDataNode(new \ICalendarOrg\ZCiCalDataNode('NAME:My Calendar Name'), new \ICalendarOrg\ZCiCalDataNode('PRODID:-//ZContent.net//ZapCalLib 1.0//EN'));
34+
35+
// create the event within the ical object
36+
$eventobj = new \ICalendarOrg\ZCiCalNode('VEVENT', $icalobj->curnode);
37+
38+
$eventobj->addNode(new \ICalendarOrg\ZCiCalDataNode('SUMMARY:' . $title));
39+
40+
// add start date
41+
$eventobj->addNode(new \ICalendarOrg\ZCiCalDataNode('DTSTART:' . \ICalendarOrg\ZDateHelper::fromSqlDateTime($event_start)));
42+
43+
// add end date
44+
$eventobj->addNode(new \ICalendarOrg\ZCiCalDataNode('DTEND:' . \ICalendarOrg\ZDateHelper::fromSqlDateTime($event_end)));
45+
46+
// UID is a required item in VEVENT, create unique string for this event
47+
// Adding your domain to the end is a good way of creating uniqueness
48+
$uid = \date('Y-m-d-H-i-s') . '@demo.icalendar.org';
49+
$eventobj->addNode(new \ICalendarOrg\ZCiCalDataNode('UID:' . $uid));
50+
51+
// DTSTAMP is a required item in VEVENT
52+
$eventobj->addNode(new \ICalendarOrg\ZCiCalDataNode('DTSTAMP:' . \ICalendarOrg\ZDateHelper::fromSqlDateTime()));
53+
54+
// Add description
55+
$eventobj->addNode(new \ICalendarOrg\ZCiCalDataNode('Description: This is a simple event, using the Zap Calendar PHP library. Visit http://icalendar.org to validate icalendar files.'));
56+
57+
$icalString = $icalobj->export();
58+
$this->assertStringContainsString('My Calendar Name', $icalString);
59+
}
60+
}

0 commit comments

Comments
 (0)