$maps Remote maps keyed by id, in Mapsemble's order (oldest first). * @param callable(int): string $format_date Formats a unix timestamp for display. * @return list `label` is what the picker shows; `name` is the plain label, for naming a chosen map. */ public static function options( array $maps, callable $format_date ): array { $counts = array(); foreach ( $maps as $id => $map ) { $label = self::label( (string) $id, $map ); $counts[ $label ] = ( $counts[ $label ] ?? 0 ) + 1; } $options = array(); foreach ( array_reverse( $maps, true ) as $id => $map ) { $id = (string) $id; $name = self::label( $id, $map ); $label = $name; if ( $counts[ $name ] > 1 ) { $created = self::created_at( $id ); $label .= ' · ' . ( null !== $created ? $format_date( $created ) . ' · ' : '' ) . substr( $id, -4 ); } $options[] = array( 'value' => $id, 'label' => $label, 'name' => $name, ); } return $options; } /** * The creation time of a map, read from its UUIDv7 id. * * @param string $id Map id. * @return int|null Unix seconds, or null for an id that is not a UUIDv7. */ public static function created_at( string $id ): ?int { $hex = str_replace( '-', '', strtolower( $id ) ); if ( 32 !== strlen( $hex ) || ! ctype_xdigit( $hex ) || '7' !== $hex[12] ) { return null; } // 48 bits overflow a 32-bit int, so hexdec() returns a float there; a double holds them exactly. return (int) floor( hexdec( substr( $hex, 0, 12 ) ) / 1000 ); } /** * A map id short enough to sit in a table cell: its first and last four * characters. Ids of nine characters or fewer are shown whole — shortening * them would save nothing. Counted in characters, not bytes, so a * non-ASCII id is never cut mid-character. * * @param string $id Map id. */ public static function short_id( string $id ): string { return mb_strlen( $id, 'UTF-8' ) > 9 ? mb_substr( $id, 0, 4, 'UTF-8' ) . '…' . mb_substr( $id, -4, null, 'UTF-8' ) : $id; } /** * A map's display label: its trimmed label, or its id when it has none. * * @param string $id Map id. * @param mixed $map Remote map payload (anything: the API is not trusted to send an array). * @return string */ public static function label( string $id, $map ): string { $raw = is_array( $map ) ? ( $map['label'] ?? '' ) : ''; $label = is_scalar( $raw ) ? trim( (string) $raw ) : ''; return '' !== $label ? $label : $id; } }