Skip to content

Commit

Permalink
Merge pull request #10 from Astrotomic/address
Browse files Browse the repository at this point in the history
Add support for Address / Adr
  • Loading branch information
Gummibeer authored Feb 21, 2023
2 parents 06945f7 + 87346e0 commit 0ad87fe
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Vcard::make()
->url('https://johnsmith.com')
->url('https://company.com')
->bday(Carbon::parse('1990-06-24'))
->adr('','','1600 Pennsylvania Ave NW', 'Washington', 'DC', '20500-0003', 'USA')
->photo('data:image/jpeg;base64,'.base64_encode(file_get_contents(__DIR__.'/stubs/photo.jpg')))
->title('V. P. Research and Development')
->role('Excecutive')
Expand All @@ -65,6 +66,7 @@ TEL;TYPE=CELL;TYPE=VOICE:+0123456789
URL:https://johnsmith.com
URL:https://company.com
BDAY:1990-06-24
ADR;TYPE=WORK:;;1600 Pennsylvania Ave NW;Washington;DC;20500-0003;USA
PHOTO:data:image/jpeg;base64,...
TITLE:V. P. Research and Development
ROLE:Excecutive
Expand Down
42 changes: 42 additions & 0 deletions src/Properties/Adr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Astrotomic\Vcard\Properties;

class Adr extends Property
{
public const HOME = 'HOME';
public const WORK = 'WORK';
public const PREF = 'PREF';

public function __construct(
protected string $poBox,
protected string $extendedAddress,
protected string $streetAddress,
protected string $locality,
protected string $region,
protected string $postalCode,
protected string $countryName,
protected array $types
) {
}

public function __toString(): string
{
$types = implode(';', array_map(
fn (string $type): string => "TYPE={$type}",
$this->types
));

$parameters = implode(';', [
$this->poBox,
$this->extendedAddress,
$this->streetAddress,
$this->locality,
$this->region,
$this->postalCode,
$this->countryName,
]);

return "ADR;{$types}:{$parameters}";
}
}
25 changes: 25 additions & 0 deletions src/Vcard.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Astrotomic\Vcard;

use Astrotomic\ConditionalProxy\HasConditionalCalls;
use Astrotomic\Vcard\Properties\Adr;
use Astrotomic\Vcard\Properties\Bday;
use Astrotomic\Vcard\Properties\Email;
use Astrotomic\Vcard\Properties\Gender;
Expand Down Expand Up @@ -140,6 +141,30 @@ public function member(?string $mail = null, ?string $uuid = null): self
return $this;
}

public function adr(
?string $poBox = null,
?string $extendedAddress = null,
?string $streetAddress = null,
?string $locality = null,
?string $region = null,
?string $postalCode = null,
?string $countryName = null,
array $types = [Adr::WORK]
): self {
$this->properties[] = new Adr(
$poBox,
$extendedAddress,
$streetAddress,
$locality,
$region,
$postalCode,
$countryName,
$types
);

return $this;
}

public function __toString(): string
{
return collect([
Expand Down
24 changes: 24 additions & 0 deletions tests/VcardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Astrotomic\Vcard\Tests;

use Astrotomic\Vcard\Properties\Adr;
use Astrotomic\Vcard\Properties\Email;
use Astrotomic\Vcard\Properties\Gender;
use Astrotomic\Vcard\Properties\Kind;
Expand Down Expand Up @@ -29,6 +30,7 @@ public function vcard_full(): void
->url('https://company.com')
->bday(Carbon::parse('1990-06-24'))
->photo('data:image/jpeg;base64,'.base64_encode(file_get_contents(__DIR__.'/stubs/photo.jpg')))
->adr('', '', '1600 Pennsylvania Ave NW', 'Washington', 'DC', '20500-0003', 'USA')
);
}

Expand Down Expand Up @@ -203,4 +205,26 @@ public function vcard_with_fn_member_uuid_and_mail(): void
->member('[email protected]', '550e8400-e29b-11d4-a716-446655440000')
);
}

/** @test */
public function vcard_with_work_address(): void
{
$this->assertMatchesVcardSnapshot(
Vcard::make()
->fullName('John Adam Smith')
->adr('', '', '1600 Pennsylvania Ave NW', 'Washington', 'DC', '20500-0003', 'USA', [Adr::WORK])
);
}

/** @test */
public function vcard_with_work_and_home_address(): void
{
$this->assertMatchesVcardSnapshot(
Vcard::make()
->fullName('John Adam Smith')
->adr('', '', '1600 Pennsylvania Ave NW', 'Washington', 'DC', '20500-0003', 'USA',
[Adr::WORK, Adr::PREF])
->adr('', '', '1640 Riverside Drive', ' Hill Valley', 'CA', '', 'USA', [Adr::HOME])
);
}
}
1 change: 1 addition & 0 deletions tests/__snapshots__/VcardTest__vcard_full__1.vcf

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions tests/__snapshots__/VcardTest__vcard_with_work_address__1.vcf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
BEGIN:VCARD
VERSION:4.0
FN;CHARSET=UTF-8:John Adam Smith
ADR;TYPE=WORK:;;1600 Pennsylvania Ave NW;Washington;DC;20500-0003;USA
REV:2021-02-25T10:30:45.000000Z
PRODID:-//Astrotomic vCard
END:VCARD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BEGIN:VCARD
VERSION:4.0
FN;CHARSET=UTF-8:John Adam Smith
ADR;TYPE=WORK;TYPE=PREF:;;1600 Pennsylvania Ave NW;Washington;DC;20500-0003;USA
ADR;TYPE=HOME:;;1640 Riverside Drive; Hill Valley;CA;;USA
REV:2021-02-25T10:30:45.000000Z
PRODID:-//Astrotomic vCard
END:VCARD

0 comments on commit 0ad87fe

Please sign in to comment.