Skip to content

Commit b8e855e

Browse files
Merge pull request #104 from CalebDW/file_attachments
Add embedded attachments to Events
2 parents 8c6be64 + a9db5b2 commit b8e855e

File tree

6 files changed

+173
-4
lines changed

6 files changed

+173
-4
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ Event::create()
222222
...
223223
```
224224

225-
You can add an attachment as such:
225+
You can add a url attachment as such:
226226

227227
```php
228228
Event::create()
@@ -231,6 +231,16 @@ Event::create()
231231
...
232232
```
233233

234+
You can add an embedded attachment (base64) as such:
235+
236+
```php
237+
Event::create()
238+
->embeddedAttachment($file->toString())
239+
->embeddedAttachment($fileString, 'application/json')
240+
->embeddedAttachment($base64String, 'application/json', needsEncoding: false)
241+
...
242+
```
243+
234244
You can add an image as such:
235245

236246
``` php

src/Components/Event.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Spatie\IcalendarGenerator\Enums\EventStatus;
1313
use Spatie\IcalendarGenerator\Enums\ParticipationStatus;
1414
use Spatie\IcalendarGenerator\Properties\AppleLocationCoordinatesProperty;
15+
use Spatie\IcalendarGenerator\Properties\BinaryProperty;
1516
use Spatie\IcalendarGenerator\Properties\CalendarAddressProperty;
1617
use Spatie\IcalendarGenerator\Properties\CoordinatesProperty;
1718
use Spatie\IcalendarGenerator\Properties\DateTimeProperty;
@@ -21,6 +22,7 @@
2122
use Spatie\IcalendarGenerator\Properties\UriProperty;
2223
use Spatie\IcalendarGenerator\Timezones\HasTimezones;
2324
use Spatie\IcalendarGenerator\Timezones\TimezoneRangeCollection;
25+
use Spatie\IcalendarGenerator\ValueObjects\BinaryValue;
2426
use Spatie\IcalendarGenerator\ValueObjects\CalendarAddress;
2527
use Spatie\IcalendarGenerator\ValueObjects\DateTimeValue;
2628
use Spatie\IcalendarGenerator\ValueObjects\RRule;
@@ -346,6 +348,16 @@ public function attachment(string $url, ?string $mediaType = null): Event
346348
return $this;
347349
}
348350

351+
public function embeddedAttachment(
352+
string $data,
353+
?string $mediaType = null,
354+
bool $needsEncoding = true,
355+
): Event {
356+
$this->attachments[] = new BinaryValue($data, $mediaType, $needsEncoding);
357+
358+
return $this;
359+
}
360+
349361
public function image(string $url, ?string $mime = null, ?Display $display = null): Event
350362
{
351363
$this->images[] = [
@@ -449,9 +461,15 @@ private function resolveProperties(ComponentPayload $payload): self
449461
)
450462
->multiple(
451463
$this->attachments,
452-
fn (array $attachment) => $attachment['type'] !== null
453-
? UriProperty::create('ATTACH', $attachment['url'])->addParameter(Parameter::create('FMTTYPE', $attachment['type']))
454-
: UriProperty::create('ATTACH', $attachment['url'])
464+
function (array|BinaryValue $attachment) {
465+
if ($attachment instanceof BinaryValue) {
466+
return BinaryProperty::create('ATTACH', $attachment);
467+
}
468+
469+
return $attachment['type'] !== null
470+
? UriProperty::create('ATTACH', $attachment['url'])->addParameter(Parameter::create('FMTTYPE', $attachment['type']))
471+
: UriProperty::create('ATTACH', $attachment['url']);
472+
}
455473
)
456474
->multiple(
457475
$this->images,

src/Properties/BinaryProperty.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Spatie\IcalendarGenerator\Properties;
4+
5+
use Spatie\IcalendarGenerator\ValueObjects\BinaryValue;
6+
7+
final class BinaryProperty extends Property
8+
{
9+
private BinaryValue $binaryValue;
10+
11+
public static function create(string $name, BinaryValue $binaryValue): BinaryProperty
12+
{
13+
return new self($name, $binaryValue);
14+
}
15+
16+
public function __construct(string $name, BinaryValue $binaryValue)
17+
{
18+
$this->name = $name;
19+
$this->binaryValue = $binaryValue;
20+
21+
if ($this->binaryValue->fmttype) {
22+
$this->addParameter(Parameter::create('FMTTYPE', $this->binaryValue->fmttype));
23+
}
24+
25+
$this->addParameter(Parameter::create('ENCODING', 'BASE64'));
26+
$this->addParameter(Parameter::create('VALUE', 'BINARY'));
27+
}
28+
29+
public function getValue(): string
30+
{
31+
return $this->binaryValue->data;
32+
}
33+
34+
public function getOriginalValue(): BinaryValue
35+
{
36+
return $this->binaryValue;
37+
}
38+
}

src/ValueObjects/BinaryValue.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Spatie\IcalendarGenerator\ValueObjects;
4+
5+
class BinaryValue
6+
{
7+
public string $data;
8+
9+
public ?string $fmttype;
10+
11+
public function __construct(
12+
string $data,
13+
?string $fmttype = null,
14+
bool $needsEncoding = true,
15+
) {
16+
$this->data = $needsEncoding
17+
? base64_encode($data)
18+
: $data;
19+
20+
$this->fmttype = $fmttype;
21+
}
22+
}

tests/Components/EventTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,32 @@ function (PropertyExpectation $expectation) use ($dateD, $dateA) {
457457
);
458458
});
459459

460+
test('it can add an embedded attachment to an event', function () {
461+
$file = file_get_contents('.gitignore');
462+
$base64File = base64_encode(file_get_contents('.gitattributes'));
463+
464+
$payload = Event::create()
465+
->embeddedAttachment($file, 'text/plain')
466+
->embeddedAttachment($base64File, 'text/plain', false)
467+
->resolvePayload();
468+
469+
PayloadExpectation::create($payload)->expectProperty(
470+
'ATTACH',
471+
fn (PropertyExpectation $expectation) => $expectation
472+
->expectParameterCount(3)
473+
->expectParameterValue('FMTTYPE', 'text/plain')
474+
->expectParameterValue('ENCODING', 'BASE64')
475+
->expectParameterValue('VALUE', 'BINARY')
476+
->expectOutput(base64_encode($file)),
477+
fn (PropertyExpectation $expectation) => $expectation
478+
->expectParameterCount(3)
479+
->expectParameterValue('FMTTYPE', 'text/plain')
480+
->expectParameterValue('ENCODING', 'BASE64')
481+
->expectParameterValue('VALUE', 'BINARY')
482+
->expectOutput($base64File),
483+
);
484+
});
485+
460486
test('it can add an image to an event', function () {
461487
$payload = Event::create()
462488
->image('http://spatie.be/logo.svg')
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
use Spatie\IcalendarGenerator\Properties\BinaryProperty;
4+
use Spatie\IcalendarGenerator\Tests\PropertyExpectation;
5+
use Spatie\IcalendarGenerator\ValueObjects\BinaryValue;
6+
7+
test('it can create a binary property type', function () {
8+
$fileString = file_get_contents('.gitignore');
9+
10+
$property = new BinaryProperty(
11+
'ATTACH',
12+
new BinaryValue($fileString)
13+
);
14+
15+
PropertyExpectation::create($property)
16+
->expectName('ATTACH')
17+
->expectOutput(base64_encode($fileString))
18+
->expectParameterCount(2)
19+
->expectParameterValue('ENCODING', 'BASE64')
20+
->expectParameterValue('VALUE', 'BINARY');
21+
});
22+
23+
test('it can set a media type', function () {
24+
$fileString = file_get_contents('.gitignore');
25+
26+
$property = new BinaryProperty(
27+
'ATTACH',
28+
new BinaryValue($fileString, 'text/plain')
29+
);
30+
31+
PropertyExpectation::create($property)
32+
->expectName('ATTACH')
33+
->expectOutput(base64_encode($fileString))
34+
->expectParameterCount(3)
35+
->expectParameterValue('FMTTYPE', 'text/plain')
36+
->expectParameterValue('ENCODING', 'BASE64')
37+
->expectParameterValue('VALUE', 'BINARY');
38+
});
39+
40+
test('it can accept base64 encoded content', function () {
41+
$fileString = base64_encode(file_get_contents('.gitignore'));
42+
43+
$property = new BinaryProperty(
44+
'ATTACH',
45+
new BinaryValue($fileString, 'text/plain', false)
46+
);
47+
48+
PropertyExpectation::create($property)
49+
->expectName('ATTACH')
50+
->expectOutput($fileString)
51+
->expectParameterCount(3)
52+
->expectParameterValue('FMTTYPE', 'text/plain')
53+
->expectParameterValue('ENCODING', 'BASE64')
54+
->expectParameterValue('VALUE', 'BINARY');
55+
});

0 commit comments

Comments
 (0)