-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca5a8f4
commit a65dc5c
Showing
28 changed files
with
891 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace Spatie\IcalendarGenerator\Components; | ||
|
||
use Closure; | ||
use DateTimeInterface; | ||
use DateTimeZone; | ||
use Spatie\IcalendarGenerator\ComponentPayload; | ||
use Spatie\IcalendarGenerator\Properties\DateTimeProperty; | ||
use Spatie\IcalendarGenerator\Properties\TextProperty; | ||
use Spatie\IcalendarGenerator\ValueObjects\DateTimeValue; | ||
|
||
class Timezone extends Component | ||
{ | ||
private string $identifier; | ||
|
||
private ?DateTimeValue $lastModified = null; | ||
|
||
private ?string $url = null; | ||
|
||
/** @var \Spatie\IcalendarGenerator\Components\TimezoneEntry[] */ | ||
private array $entries = []; | ||
|
||
public static function create(string $identifier): self | ||
{ | ||
return new self($identifier); | ||
} | ||
|
||
public function __construct(string $identifier) | ||
{ | ||
$this->identifier = $identifier; | ||
} | ||
|
||
public function lastModified(DateTimeInterface $lastModified): self | ||
{ | ||
$this->lastModified = DateTimeValue::create($lastModified) | ||
->convertToTimezone(new DateTimeZone('UTC')); | ||
|
||
return $this; | ||
} | ||
|
||
public function url(string $url): self | ||
{ | ||
$this->url = $url; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param $entry \Spatie\IcalendarGenerator\Components\TimezoneEntry|array | ||
* | ||
* @return \Spatie\IcalendarGenerator\Components\Timezone | ||
*/ | ||
public function entry($entry): Timezone | ||
{ | ||
if (is_null($entry)) { | ||
return $this; | ||
} | ||
|
||
$this->entries = array_merge( | ||
$this->entries, | ||
is_array($entry) ? $entry : [$entry] | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
public function getComponentType(): string | ||
{ | ||
return 'VTIMEZONE'; | ||
} | ||
|
||
public function getRequiredProperties(): array | ||
{ | ||
return [ | ||
'TZID', | ||
]; | ||
} | ||
|
||
protected function payload(): ComponentPayload | ||
{ | ||
return ComponentPayload::create($this->getComponentType()) | ||
->property(TextProperty::create('TZID', $this->identifier)) | ||
->optional( | ||
$this->url, | ||
fn() => TextProperty::create('TZURL', $this->url)->withoutEscaping() | ||
) | ||
->optional( | ||
$this->lastModified, | ||
fn() => DateTimeProperty::create('LAST-MODIFIED', $this->lastModified) | ||
) | ||
->subComponent(...$this->entries); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
namespace Spatie\IcalendarGenerator\Components; | ||
|
||
use DateTimeInterface; | ||
use DateTimeZone; | ||
use Spatie\IcalendarGenerator\ComponentPayload; | ||
use Spatie\IcalendarGenerator\Enums\TimezoneEntryType; | ||
use Spatie\IcalendarGenerator\Properties\DateTimeProperty; | ||
use Spatie\IcalendarGenerator\Properties\RRuleProperty; | ||
use Spatie\IcalendarGenerator\Properties\TextProperty; | ||
use Spatie\IcalendarGenerator\ValueObjects\DateTimeValue; | ||
use Spatie\IcalendarGenerator\ValueObjects\RRule; | ||
use Spatie\IcalendarGenerator\ValueObjects\TimezoneTransition; | ||
|
||
class TimezoneEntry extends Component | ||
{ | ||
private TimezoneEntryType $type; | ||
|
||
private DateTimeValue $starts; | ||
|
||
private string $offsetFrom; | ||
|
||
private string $offsetTo; | ||
|
||
private ?RRule $rrule = null; | ||
|
||
private ?string $name = null; | ||
|
||
private ?string $description = null; | ||
|
||
public static function create( | ||
TimezoneEntryType $type, | ||
DateTimeInterface $starts, | ||
string $offsetFrom, | ||
string $offsetTo | ||
): self { | ||
return new self($type, $starts, $offsetFrom, $offsetTo); | ||
} | ||
|
||
public static function createFromTransition(TimezoneTransition $transition): self | ||
{ | ||
return new self( | ||
$transition->type, | ||
$transition->start, | ||
$transition->offsetFrom->format('%r%H%M'), | ||
$transition->offsetTo->format('%r%H%M') | ||
); | ||
} | ||
|
||
public function __construct( | ||
TimezoneEntryType $type, | ||
DateTimeInterface $starts, | ||
string $offsetFrom, | ||
string $offsetTo | ||
) { | ||
$this->type = $type; | ||
$this->starts = DateTimeValue::create($starts); | ||
$this->offsetFrom = $offsetFrom; | ||
$this->offsetTo = $offsetTo; | ||
} | ||
|
||
public function name(string $name): self | ||
{ | ||
$this->name = $name; | ||
|
||
return $this; | ||
} | ||
|
||
public function description(string $description) | ||
{ | ||
$this->description = $description; | ||
|
||
return $this; | ||
} | ||
|
||
public function rrule(RRule $rrule): self | ||
{ | ||
$this->rrule = $rrule; | ||
|
||
return $this; | ||
} | ||
|
||
public function getComponentType(): string | ||
{ | ||
return $this->type->value; | ||
} | ||
|
||
public function getRequiredProperties(): array | ||
{ | ||
return [ | ||
'DTSTART', | ||
'TZOFFSETFROM', | ||
'TZOFFSETTO', | ||
]; | ||
} | ||
|
||
protected function payload(): ComponentPayload | ||
{ | ||
return ComponentPayload::create($this->getComponentType()) | ||
->property(DateTimeProperty::create('DTSTART', $this->starts, true)) | ||
->property(TextProperty::create('TZOFFSETFROM', $this->offsetFrom)) | ||
->property(TextProperty::create('TZOFFSETTO', $this->offsetTo)) | ||
->optional( | ||
$this->name, | ||
fn() => TextProperty::create('TZNAME', $this->name) | ||
) | ||
->optional( | ||
$this->description, | ||
fn() => TextProperty::create('COMMENT', $this->description) | ||
) | ||
->optional( | ||
$this->rrule, | ||
fn() => RRuleProperty::create('RRULE', $this->rrule) | ||
); | ||
} | ||
} |
Oops, something went wrong.