'MO', 2 => 'TU', 3 => 'WE', 4 => 'TH', 5 => 'FR', 6 => 'SA', 7 => 'SU', ); private const ORDINALS = array( 'First' => 1, 'Second' => 2, 'Third' => 3, 'Fourth' => 4, 'Fifth' => 5, 'Last' => -1, ); private const SIMPLE_FREQS = array( 'Every Day' => 'DAILY', 'Every Week' => 'WEEKLY', 'Every Month' => 'MONTHLY', 'Every Year' => 'YEARLY', ); private const CUSTOM_FREQS = array( 'Daily' => 'DAILY', 'Weekly' => 'WEEKLY', 'Monthly' => 'MONTHLY', 'Yearly' => 'YEARLY', ); /** * Convert one _EventRecurrence array. * * @param array $recurrence Unserialized _EventRecurrence. * @param DateTimeImmutable $dtstart Event start (local, event tz). */ public function convert( array $recurrence, DateTimeImmutable $dtstart ): TecRecurrenceConversion { $rules = isset( $recurrence['rules'] ) && is_array( $recurrence['rules'] ) ? array_values( $recurrence['rules'] ) : array(); if ( array() === $rules ) { return $this->fallback( 'No recurrence rules found.' ); } if ( count( $rules ) > 1 ) { return $this->fallback( 'Multiple recurrence rules on one event are not convertible to a single RRULE.' ); } $rule = $rules[0]; if ( ! is_array( $rule ) ) { return $this->fallback( 'Malformed recurrence rule.' ); } $parts = $this->frequency_parts( $rule, $dtstart ); if ( null === $parts ) { $type = isset( $rule['type'] ) && is_scalar( $rule['type'] ) ? (string) $rule['type'] : '(none)'; return $this->fallback( sprintf( 'Unsupported recurrence pattern "%s".', $type ) ); } $end = $this->end_parts( $rule ); if ( null === $end ) { $end_type = isset( $rule['end-type'] ) && is_scalar( $rule['end-type'] ) ? (string) $rule['end-type'] : '(none)'; return $this->fallback( sprintf( 'Unsupported end condition "%s".', $end_type ) ); } $exdates = $this->exclusion_dates( $recurrence, $dtstart ); if ( null === $exdates ) { return $this->fallback( 'Recurring or non-date exclusions cannot be converted.' ); } return new TecRecurrenceConversion( rrule: implode( ';', array_merge( $parts, $end ) ), exdates: $exdates, warnings: array(), ); } /** * FREQ/INTERVAL/BY* parts for the rule, or null when unsupported. * * @param array $rule One rules[] entry. * @param DateTimeImmutable $dtstart Event start. * @return ?list */ private function frequency_parts( array $rule, DateTimeImmutable $dtstart ): ?array { $type = isset( $rule['type'] ) && is_scalar( $rule['type'] ) ? (string) $rule['type'] : ''; if ( isset( self::SIMPLE_FREQS[ $type ] ) ) { return array( 'FREQ=' . self::SIMPLE_FREQS[ $type ] ); } if ( 'Custom' !== $type ) { return null; } $custom = isset( $rule['custom'] ) && is_array( $rule['custom'] ) ? $rule['custom'] : array(); $ctype = isset( $custom['type'] ) && is_scalar( $custom['type'] ) ? (string) $custom['type'] : ''; if ( ! isset( self::CUSTOM_FREQS[ $ctype ] ) ) { return null; } $parts = array( 'FREQ=' . self::CUSTOM_FREQS[ $ctype ] ); $interval = isset( $custom['interval'] ) && is_numeric( $custom['interval'] ) ? (int) $custom['interval'] : 1; if ( $interval > 1 ) { $parts[] = 'INTERVAL=' . $interval; } if ( 'Daily' === $ctype ) { return $parts; } if ( 'Weekly' === $ctype ) { $byday = $this->weekday_list( $custom ); if ( null !== $byday ) { $parts[] = 'BYDAY=' . $byday; } return $parts; } if ( 'Monthly' === $ctype ) { return $this->monthly_parts( $parts, $custom, $dtstart ); } // Yearly — the only remaining CUSTOM_FREQS key after the guard above. return $this->yearly_parts( $parts, $custom ); } /** * BYDAY weekday csv from custom.week.day, or null when absent. * * @param array $custom The custom{} sub-array. */ private function weekday_list( array $custom ): ?string { if ( ! isset( $custom['week'] ) || ! is_array( $custom['week'] ) || ! isset( $custom['week']['day'] ) || ! is_array( $custom['week']['day'] ) ) { return null; } $days = array(); foreach ( $custom['week']['day'] as $day ) { if ( is_numeric( $day ) && isset( self::WEEKDAYS[ (int) $day ] ) ) { $days[] = self::WEEKDAYS[ (int) $day ]; } } return array() === $days ? null : implode( ',', $days ); } /** * Monthly BY* parts, or null when the month selector is unsupported. * * @param list $parts Parts so far. * @param array $custom The custom{} sub-array. * @param DateTimeImmutable $dtstart Event start. * @return ?list */ private function monthly_parts( array $parts, array $custom, DateTimeImmutable $dtstart ): ?array { $month = isset( $custom['month'] ) && is_array( $custom['month'] ) ? $custom['month'] : array(); if ( ( $month['same-day'] ?? '' ) === 'yes' || array() === $month ) { $parts[] = 'BYMONTHDAY=' . (int) $dtstart->format( 'j' ); return $parts; } $number = $month['number'] ?? null; if ( is_numeric( $number ) ) { $parts[] = 'BYMONTHDAY=' . (int) $number; return $parts; } if ( is_string( $number ) && isset( self::ORDINALS[ $number ] ) && isset( $month['day'] ) && is_numeric( $month['day'] ) && isset( self::WEEKDAYS[ (int) $month['day'] ] ) ) { $parts[] = 'BYDAY=' . self::ORDINALS[ $number ] . self::WEEKDAYS[ (int) $month['day'] ]; return $parts; } return null; } /** * Yearly BY* parts, or null when the year selector is unsupported. * * The same-day case emits no BY* selector: the recurrence day comes * from DTSTART at expansion time. * * @param list $parts Parts so far. * @param array $custom The custom{} sub-array. * @return ?list */ private function yearly_parts( array $parts, array $custom ): ?array { $year = isset( $custom['year'] ) && is_array( $custom['year'] ) ? $custom['year'] : array(); $months = array(); if ( isset( $year['month'] ) && is_array( $year['month'] ) ) { foreach ( $year['month'] as $m ) { if ( is_numeric( $m ) && (int) $m >= 1 && (int) $m <= 12 ) { $months[] = (int) $m; } } } if ( array() !== $months ) { $parts[] = 'BYMONTH=' . implode( ',', $months ); } if ( ( $year['same-day'] ?? 'yes' ) === 'yes' ) { return $parts; } $number = $year['number'] ?? null; if ( is_string( $number ) && isset( self::ORDINALS[ $number ] ) && isset( $year['day'] ) && is_numeric( $year['day'] ) && isset( self::WEEKDAYS[ (int) $year['day'] ] ) ) { $parts[] = 'BYDAY=' . self::ORDINALS[ $number ] . self::WEEKDAYS[ (int) $year['day'] ]; return $parts; } return null; } /** * COUNT/UNTIL parts (possibly empty for Never), or null when unsupported. * * @param array $rule One rules[] entry. * @return ?list */ private function end_parts( array $rule ): ?array { $end_type = isset( $rule['end-type'] ) && is_scalar( $rule['end-type'] ) ? (string) $rule['end-type'] : 'Never'; switch ( $end_type ) { case 'Never': return array(); case 'After': $count = isset( $rule['end-count'] ) && is_numeric( $rule['end-count'] ) ? (int) $rule['end-count'] : 0; return $count > 0 ? array( 'COUNT=' . $count ) : null; case 'On': $end = isset( $rule['end'] ) && is_scalar( $rule['end'] ) ? (string) $rule['end'] : ''; $dt = DateTimeImmutable::createFromFormat( 'Y-m-d', $end ); if ( false === $dt ) { return null; } // End of the given local day, floating (matches local DTSTART). return array( 'UNTIL=' . $dt->format( 'Ymd' ) . 'T235959' ); } return null; } /** * Exclusion dates at the event's start time, or null when an exclusion * is not a plain date (recurring exclusions are not convertible). * * @param array $recurrence Full _EventRecurrence array. * @param DateTimeImmutable $dtstart Event start. * @return ?list */ private function exclusion_dates( array $recurrence, DateTimeImmutable $dtstart ): ?array { $exclusions = isset( $recurrence['exclusions'] ) && is_array( $recurrence['exclusions'] ) ? $recurrence['exclusions'] : array(); $exdates = array(); foreach ( $exclusions as $exclusion ) { if ( ! is_array( $exclusion ) ) { return null; } $custom = isset( $exclusion['custom'] ) && is_array( $exclusion['custom'] ) ? $exclusion['custom'] : array(); $date = isset( $custom['date'] ) && is_array( $custom['date'] ) && isset( $custom['date']['date'] ) && is_scalar( $custom['date']['date'] ) ? (string) $custom['date']['date'] : ''; if ( ( $custom['type'] ?? '' ) !== 'Date' || false === DateTimeImmutable::createFromFormat( 'Y-m-d', $date ) ) { return null; } $exdates[] = $date . ' ' . $dtstart->format( 'H:i:s' ); } return $exdates; } private function fallback( string $reason ): TecRecurrenceConversion { return new TecRecurrenceConversion( rrule: null, exdates: array(), warnings: array( $reason ) ); } }