diff --git a/composer.json b/composer.json index 91d69e7..665f9d7 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "itsnubix/nowcal", - "version": "1.2.2", + "version": "1.2.3", "description": "A modern PHP library for generating iCalendar v2.0 events", "keywords": [ "icalendar", diff --git a/src/NowCal/NowCal.php b/src/NowCal/NowCal.php index edc7ee3..56d51c2 100644 --- a/src/NowCal/NowCal.php +++ b/src/NowCal/NowCal.php @@ -56,7 +56,6 @@ class NowCal * @var array */ public const REQUIRED = [ - 'uid', 'stamp', 'start', 'prodid', @@ -71,6 +70,7 @@ class NowCal */ public const CASTS = [ 'end' => 'datetime', + 'method' => 'upper', 'stamp' => 'datetime', 'start' => 'datetime', 'created' => 'datetime', @@ -97,7 +97,7 @@ class NowCal * * @see https://tools.ietf.org/html/rfc5545#section-3.7.3 */ - private string $prodid = '-//itsnubix//NowCal//EN'; + private string $prodid = '-//NowCal//EN'; /** * Specifies the minimum iCalendar specification that is required @@ -324,7 +324,7 @@ public function timezone(string|DateTimeZone|Closure $timezone): self */ public function method(string $method): self { - $this->set('method', strtoupper($method)); + $this->set('method', $method); return $this; } @@ -386,6 +386,11 @@ protected function set(string|array $key, $val = null): void return; } + // if (method_exists($this, $key)) { + // $this->{$key}($val); + // return; + // } + $this->{$key} = $val; } @@ -413,6 +418,7 @@ protected function merge(array $props): void protected function cast(mixed $value, ?string $as = null): string { return match ($as) { + 'upper' => $this->castUpper($value), 'datetime' => $this->castDateTime($value), 'interval' => $this->castInterval($value), 'timezone' => $this->castTimezone($value), @@ -428,6 +434,14 @@ protected function hasCaster(string $key): bool return array_key_exists($key, static::CASTS); } + /** + * Cast the specified value to uppercase. + */ + public function castUpper($value): string + { + return strtoupper($value); + } + /** * Cast the specified value as a datetime. */ @@ -516,7 +530,9 @@ protected function createTimezone(): void continue; } - $standard = $transition; + if (!$transition['isdst']) { + $standard = $transition; + } } $this->output[] = 'BEGIN:STANDARD'; @@ -550,6 +566,7 @@ protected function formatOffset(int $offset): string protected function createEvent(): void { $this->output[] = 'BEGIN:VEVENT'; + $this->output[] = $this->getUidAttribute(); foreach ($this->event_parameters as $key) { $this->output[] = $this->getParameter($key); @@ -572,7 +589,9 @@ protected function getParameter(string $key): string } if ($this->required($key)) { - throw new Exception('Key "' . $key . '" is not set but is required'); + $value = $this->{$key} ?? ''; + + return $this->getParameterKey($key) . ':' . $value; } } @@ -632,13 +651,13 @@ protected function getEventParametersAttribute(): array array_merge(static::VEVENT, static::ALLOWED), fn($key) => match ($key) { 'method', 'timezone' => false, - default => $this->has($key), + default => $this->required($key) || $this->has($key), }, ); } /** - * Create and return a UUID. + * Create and return a uid. */ protected function getUidAttribute(): string { diff --git a/tests/NowCal/NowCalTest.php b/tests/NowCal/NowCalTest.php index 0a4b6b4..7325335 100644 --- a/tests/NowCal/NowCalTest.php +++ b/tests/NowCal/NowCalTest.php @@ -155,10 +155,7 @@ public function test_places_that_do_not_witness_dst_dont_get_daylight_hours() public function test_it_can_customize_the_uid() { $this->nowcal->uid($uid = 'abcd-1234'); - - echo $this->nowcal->plain; - - $this->assertEquals('UID:' . $uid, 'UID:' . $this->nowcal->uid); + $this->assertStringContainsString('UID:' . $uid, $this->nowcal->plain); } public function test_it_can_set_a_sequence() @@ -172,6 +169,6 @@ public function test_it_can_set_a_method() { $this->nowcal->method($method = 'request'); - $this->assertEquals(strtoupper($method), $this->nowcal->method); + $this->assertEquals($method, $this->nowcal->method); } }