$row Donation row (goal_id, base_minor). * @param array> $defs Raw alert definitions. * @param array $triggered Rewards this donation triggered. * @param callable(int):?array{url:string,mime:string} $attachment * @return array{visual:?array{type:string,url:string},sound:?array{url:string},volume:int,duration_ms:int,message:?string}|null */ public static function build( array $row, string $name, string $amountDisplay, array $defs, array $triggered, callable $attachment ): ?array { if ( ! $defs ) { return null; } $ids = array(); $amounts = array(); foreach ( $triggered as $t ) { $aid = (int) ( $t['action_id'] ?? 0 ); $ids[] = $aid; $amounts[ $aid ] = (int) ( $t['amount_minor'] ?? 0 ); } $def = Resolver::pick( (int) ( $row['goal_id'] ?? 0 ), (int) ( $row['base_minor'] ?? 0 ), $ids, $defs, $amounts ); if ( null === $def ) { return null; } return self::fromDef( $def, $name, $amountDisplay, $triggered, $attachment ); } /** @return array|null */ public static function fromDef( array $def, string $name, string $amountDisplay, array $triggered, callable $attachment ): ?array { $visual = null; $vid = (int) ( $def['visual_id'] ?? 0 ); if ( $vid > 0 ) { $a = self::lookup( $attachment, $vid ); $type = $a ? Media::visualType( $a['mime'] ) : null; if ( $a && $type ) { $visual = array( 'type' => $type, 'url' => $a['url'] ); } } $sound = null; $sid = (int) ( $def['sound_id'] ?? 0 ); if ( $sid > 0 ) { $a = self::lookup( $attachment, $sid ); if ( $a && Media::isSound( $a['mime'] ) ) { $sound = array( 'url' => $a['url'] ); } } $reward = ''; $best = -1; foreach ( $triggered as $t ) { $amt = (int) ( $t['amount_minor'] ?? 0 ); if ( 'reward' === ( $def['trigger_type'] ?? '' ) ) { if ( (int) ( $t['action_id'] ?? 0 ) === (int) $def['action_id'] ) { $reward = (string) ( $t['title'] ?? '' ); break; } } elseif ( $amt > $best ) { $best = $amt; $reward = (string) ( $t['title'] ?? '' ); } } $msg = Template::render( (string) ( $def['message'] ?? '' ), $name, $amountDisplay, $reward ); if ( null === $visual && null === $sound && '' === $msg ) { return null; // nothing usable left: behave exactly as a plain alert } $duration = Media::clampDurationMs( $def['duration_ms'] ?? Media::DEFAULT_MS ); if ( $visual && 'video' === $visual['type'] ) { $duration = Media::VIDEO_CAP_MS; } return array( 'visual' => $visual, 'sound' => $sound, 'volume' => Media::clampVolume( $def['volume'] ?? 80 ), 'duration_ms' => $duration, 'message' => '' === $msg ? null : $msg, ); } /** @return array{url:string,mime:string}|null */ private static function lookup( callable $attachment, int $id ): ?array { try { $a = $attachment( $id ); } catch ( \Throwable ) { return null; } if ( ! is_array( $a ) || ! Media::isAllowed( $a['mime'] ?? null ) ) { return null; } $url = Media::safeUrl( $a['url'] ?? null ); return null === $url ? null : array( 'url' => $url, 'mime' => (string) $a['mime'] ); } }