Skip to content

Commit

Permalink
Add X-APPLE-STRUCTURED-LOCATION information (fixes #209) (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gared committed Mar 29, 2021
1 parent 35d371b commit 444637c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
7 changes: 5 additions & 2 deletions docs/components/event.md
Expand Up @@ -179,8 +179,8 @@ use Eluceo\iCal\Domain\ValueObject\TimeSpan;
use Eluceo\iCal\Domain\ValueObject\DateTime;
use Eluceo\iCal\Domain\Entity\Event;

$start = new DateTime(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-03 13:00:00'));
$end = new DateTime(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-03 14:00:00'));
$start = new DateTime(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-03 13:00:00'), false);
$end = new DateTime(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-03 14:00:00'), false);
$occurrence = new TimeSpan($start, $end);

$event = new Event();
Expand All @@ -202,6 +202,9 @@ use Eluceo\iCal\Domain\ValueObject\GeographicPosition;

$location = new Location('North Pole');

// optionally you can create a location with a title for X-APPLE-STRUCTURED-LOCATION attribute
$location = new Location('North Pole', 'Middle of nowhere');

// optionally a location with a geographical position can be created
$location = $location->withGeographicPosition(new GeographicPosition(64.751111, 147.349444));

Expand Down
9 changes: 8 additions & 1 deletion src/Domain/ValueObject/Location.php
Expand Up @@ -14,11 +14,13 @@
final class Location
{
private string $location;
private ?string $title;
private ?GeographicPosition $geographicPosition = null;

public function __construct(string $location)
public function __construct(string $location, string $title = null)
{
$this->location = $location;
$this->title = $title;
}

public function withGeographicPosition(GeographicPosition $geographicPosition): self
Expand All @@ -43,6 +45,11 @@ public function getGeographicPosition(): GeographicPosition
return $this->geographicPosition;
}

public function getTitle(): string
{
return (string) $this->title;
}

public function __toString(): string
{
return $this->location;
Expand Down
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the eluceo/iCal package.
*
* (c) 2021 Markus Poerschke <markus@poerschke.nrw>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Eluceo\iCal\Presentation\Component\Property\Value;

final class AppleLocationGeoValue extends GeoValue
{
public function __toString(): string
{
return sprintf('geo:%1.6F,%1.6F',
number_format($this->geographicPosition->getLatitude(), 6),
number_format($this->geographicPosition->getLongitude(), 6),
);
}
}
4 changes: 2 additions & 2 deletions src/Presentation/Component/Property/Value/GeoValue.php
Expand Up @@ -14,9 +14,9 @@
use Eluceo\iCal\Domain\ValueObject\GeographicPosition;
use Eluceo\iCal\Presentation\Component\Property\Value;

final class GeoValue extends Value
class GeoValue extends Value
{
private GeographicPosition $geographicPosition;
protected GeographicPosition $geographicPosition;

public function __construct(GeographicPosition $geographicPosition)
{
Expand Down
12 changes: 12 additions & 0 deletions src/Presentation/Factory/EventFactory.php
Expand Up @@ -23,10 +23,12 @@
use Eluceo\iCal\Presentation\Component;
use Eluceo\iCal\Presentation\Component\Property;
use Eluceo\iCal\Presentation\Component\Property\Parameter;
use Eluceo\iCal\Presentation\Component\Property\Value\AppleLocationGeoValue;
use Eluceo\iCal\Presentation\Component\Property\Value\BinaryValue;
use Eluceo\iCal\Presentation\Component\Property\Value\DateTimeValue;
use Eluceo\iCal\Presentation\Component\Property\Value\DateValue;
use Eluceo\iCal\Presentation\Component\Property\Value\GeoValue;
use Eluceo\iCal\Presentation\Component\Property\Value\IntegerValue;
use Eluceo\iCal\Presentation\Component\Property\Value\TextValue;
use Eluceo\iCal\Presentation\Component\Property\Value\UriValue;
use Generator;
Expand Down Expand Up @@ -134,6 +136,16 @@ private function getLocationProperties(Event $event): Generator

if ($event->getLocation()->hasGeographicalPosition()) {
yield new Property('GEO', new GeoValue($event->getLocation()->getGeographicPosition()));
yield new Property(
'X-APPLE-STRUCTURED-LOCATION',
new AppleLocationGeoValue($event->getLocation()->getGeographicPosition()),
[
new Parameter('VALUE', new TextValue('URI')),
new Parameter('X-ADDRESS', new TextValue((string) $event->getLocation())),
new Parameter('X-APPLE-RADIUS', new IntegerValue(49)),
new Parameter('X-TITLE', new TextValue($event->getLocation()->getTitle())),
]
);
}
}

Expand Down
4 changes: 3 additions & 1 deletion tests/Unit/Presentation/Factory/EventFactoryTest.php
Expand Up @@ -70,14 +70,16 @@ public function testEventWithSummaryAndDescription()
public function testEventWithLocation()
{
$geographicalPosition = new GeographicPosition(51.333333333333, 7.05);
$location = (new Location('Location Name'))->withGeographicPosition($geographicalPosition);
$location = (new Location('Location Name', 'Somewhere'))->withGeographicPosition($geographicalPosition);
$event = (new Event())->setLocation($location);

self::assertEventRendersCorrect(
$event,
[
'LOCATION:Location Name',
'GEO:51.333333;7.050000',
'X-APPLE-STRUCTURED-LOCATION;VALUE=URI;X-ADDRESS=Location Name;X-APPLE-RADIU',
' S=49;X-TITLE=Somewhere:geo:51.333333,7.050000',
]
);
}
Expand Down

0 comments on commit 444637c

Please sign in to comment.