Skip to content

Commit 0da337d

Browse files
committed
Fix for supporting line-folding
1 parent c69411c commit 0da337d

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/ICalendarOrg/ZCiCal.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,17 @@ public function __construct(string $data = '', int $maxevents = 1_000_000, int $
5252
$eventcount = 0;
5353
$eventpos = 0;
5454

55+
// We need to handle data folding, so let's detect when we need
56+
// to unfold and keep track of what we're unfolding onto.
57+
$lastdatakey = null;
58+
5559
foreach ($lines as $line)
5660
{
5761
if ('BEGIN:' == \substr($line, 0, 6))
5862
{
5963
// start new object
6064
$name = \substr($line, 6);
65+
$lastdatakey = null;
6166

6267
if ('VEVENT' == $name)
6368
{
@@ -84,6 +89,7 @@ public function __construct(string $data = '', int $maxevents = 1_000_000, int $
8489
elseif ('END:' == \substr($line, 0, 4))
8590
{
8691
$name = \substr($line, 4);
92+
$lastdatakey = null;
8793

8894
if ('VEVENT' == $name)
8995
{
@@ -116,9 +122,19 @@ public function __construct(string $data = '', int $maxevents = 1_000_000, int $
116122
}
117123
}
118124
}
125+
elseif (' ' == \substr($line, 0, 1))
126+
{
127+
// This appends to the previous line.
128+
if ($lastdatakey !== null) {
129+
$this->curnode->data[$lastdatakey]->values[
130+
count($this->curnode->data[$lastdatakey]->values)-1
131+
] .= ltrim($line);
132+
}
133+
}
119134
else
120135
{
121136
$datanode = new \ICalendarOrg\ZCiCalDataNode($line);
137+
$lastdatakey = $datanode->getName();
122138

123139
if ('VEVENT' == $this->curnode->getName())
124140
{

tests/LinefoldingTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* This file is part of the ICalendarOrg package
4+
*
5+
* (c) Bruce Wells
6+
*
7+
* For the full copyright and license information, please view
8+
* the LICENSE file that was distributed with this source
9+
* code
10+
*
11+
* Contributed by Tanabi @ GitHub
12+
*/
13+
class LinefoldingTest extends \PHPUnit\Framework\TestCase
14+
{
15+
/**
16+
* Test iCAL format with line folding (long lines)
17+
*/
18+
public function testLineFolding() : void
19+
{
20+
$sample = <<<EOS
21+
BEGIN:VCALENDAR
22+
PRODID;X-RICAL-TZSOURCE=TZINFO:-//Airbnb Inc//Hosting Calendar 0.8.8//EN
23+
CALSCALE:GREGORIAN
24+
VERSION:2.0
25+
BEGIN:VEVENT
26+
DTEND;VALUE=DATE:20230611
27+
DTSTART;VALUE=DATE:20230514
28+
29+
DESCRIPTION:Reservation URL: https://www.airbnb.com/hosting/reservations/
30+
details/xxx\\nPhone Number (Last 4 Digits): 0000
31+
SUMMARY:Reserved
32+
END:VEVENT
33+
END:VCALENDAR
34+
EOS
35+
;
36+
37+
$test = new \ICalendarOrg\ZCiCal($sample);
38+
39+
// Make sure description 'reformed' correctly.
40+
$this->assertCount(1, $test->tree->child);
41+
42+
$node = $test->tree->child[0];
43+
44+
// Make sure all expected fields are there
45+
foreach (array('DTEND', 'DTSTART', 'UID', 'DESCRIPTION', 'SUMMARY')
46+
as $field) {
47+
$this->assertArrayHasKey($field, $node->data);
48+
}
49+
50+
// Make sure description matches
51+
$this->assertEquals(
52+
trim($node->data['DESCRIPTION']->values[0]),
53+
'Reservation URL: https://www.airbnb.com/hosting/reservations/details/xxx\nPhone Number (Last 4 Digits): 0000'
54+
);
55+
}
56+
}

0 commit comments

Comments
 (0)