| 1: | <?php |
| 2: | declare(strict_types=1); |
| 3: | |
| 4: | namespace om\ICal; |
| 5: | |
| 6: | use DateInterval; |
| 7: | use DateTimeInterface; |
| 8: | use InvalidArgumentException; |
| 9: | use om\ICal\Value\CalAddress; |
| 10: | use om\ICal\Value\Classification; |
| 11: | use om\ICal\Value\Conference; |
| 12: | use om\ICal\Value\DateTimeValue; |
| 13: | use om\ICal\Value\Image; |
| 14: | use om\ICal\Value\Link; |
| 15: | use om\ICal\Value\Period; |
| 16: | use om\ICal\Value\PropertyFactory; |
| 17: | use om\ICal\Value\Relation; |
| 18: | use om\ICal\Value\Status; |
| 19: | use om\ICal\Value\Transparency; |
| 20: | use om\RRule\Rule; |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | final class Event extends Item { |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | public static function new( |
| 63: | ?string $uid = null, |
| 64: | DateTimeInterface|DateTimeValue|string|null $stamp = null, |
| 65: | DateTimeInterface|DateTimeValue|string|null $start = null, |
| 66: | DateTimeInterface|DateTimeValue|string|null $end = null, |
| 67: | DateInterval|string|null $duration = null, |
| 68: | ?string $summary = null, |
| 69: | ?string $description = null, |
| 70: | ?string $location = null, |
| 71: | ?string $url = null, |
| 72: | Status|string|null $status = null, |
| 73: | Transparency|string|null $transparency = null, |
| 74: | Classification|string|null $classification = null, |
| 75: | ?int $priority = null, |
| 76: | ?int $sequence = null, |
| 77: | iterable $categories = [], |
| 78: | CalAddress|string|null $organizer = null, |
| 79: | iterable $attendees = [], |
| 80: | Rule|string|null $rrule = null, |
| 81: | iterable $rdates = [], |
| 82: | iterable $exdates = [], |
| 83: | DateTimeInterface|DateTimeValue|string|null $recurrenceId = null, |
| 84: | ?array $geo = null, |
| 85: | ?string $color = null, |
| 86: | iterable $images = [], |
| 87: | iterable $conferences = [], |
| 88: | iterable $links = [], |
| 89: | iterable $relatedTo = [], |
| 90: | iterable $alarms = [], |
| 91: | iterable $locations = [], |
| 92: | array $properties = [], |
| 93: | ): self { |
| 94: | $builder = ComponentBuilder::item('VEVENT', $uid, $stamp); |
| 95: | $startValue = $builder->start($start); |
| 96: | $endValue = $builder->end('DTEND', $end, $duration, $startValue); |
| 97: | $builder->recurrence($startValue, $rrule, $rdates, $exdates, $recurrenceId) |
| 98: | ->descriptive($summary, $description, $location, $url, $classification, $priority, $sequence, $categories, $organizer, $attendees, $geo, $color, $images, $conferences, $links, $relatedTo) |
| 99: | ->status($status) |
| 100: | ->transparency($transparency) |
| 101: | ->alarms($alarms, $startValue !== null, $endValue !== null, 'DTEND') |
| 102: | ->locations($locations); |
| 103: | return new self($builder->build($properties), new Calendar()); |
| 104: | } |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | public function end(): ?DateTimeValue { |
| 110: | return $this->date('DTEND') ?? parent::end(); |
| 111: | } |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | public function duration(): DateInterval { |
| 117: | return $this->remember('duration', $this->calculateDuration(...)); |
| 118: | } |
| 119: | |
| 120: | private function calculateDuration(): DateInterval { |
| 121: | $start = $this->start(); |
| 122: | $end = $this->date('DTEND'); |
| 123: | if ($start !== null && $end !== null) { |
| 124: | return self::between($start, $end); |
| 125: | } |
| 126: | $duration = $this->property('DURATION'); |
| 127: | if ($duration !== null && ($interval = $this->calendar->values()->duration($duration)) !== null) { |
| 128: | return $interval; |
| 129: | } |
| 130: | return new DateInterval($start?->isDate() ? 'P1D' : 'PT0S'); |
| 131: | } |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | public function transparency(): string { |
| 137: | return strtoupper($this->text('TRANSP') ?? 'OPAQUE'); |
| 138: | } |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | public function geo(): ?array { |
| 144: | $property = $this->property('GEO'); |
| 145: | return $property === null ? null : $this->calendar->values()->geo($property); |
| 146: | } |
| 147: | } |
| 148: | |