format( $number ); if ( false === $formatted_number ) { return ''; } return $formatted_number; } /** * Gets time in formatted form. * * Example: * - Input `1000` (seconds) and Output `16:40` which represents "16 minutes, 40 seconds” * * @since 3.7.0 * * @param int|float $seconds Time in seconds that we have to format. * * @return string */ function get_formatted_time( $seconds ): string { $time_formatter = new NumberFormatter( 'en', NumberFormatter::DURATION ); $formatted_time = $time_formatter->format( $seconds ); if ( false === $formatted_time ) { return ''; } return $formatted_time; } /** * Converts to associate array. * * @since 3.7.0 * * @param mixed $obj Input object. * * @return array|WP_Error */ function convert_to_associative_array( $obj ) { $encoded = wp_json_encode( $obj ); if ( false === $encoded ) { return new WP_Error( 'parsely_encoding_failed', __( 'Unable to encode API response for associative array', 'wp-parsely' ) ); } /** * Variable. * * @var array */ return json_decode( $encoded, true ); } /** * Converts a string to a positive integer, removing any non-numeric * characters. * * @param string $string The string to be converted to an integer. * @return int The integer resulting from the conversion. */ function convert_to_positive_integer( string $string ): int { return (int) preg_replace( '/\D/', '', $string ); } /** * Converts endpoint to filter key by replacing `/` with `_`. * * @param string $endpoint Route of the endpoint. * * @since 3.7.0 * * @return string */ function convert_endpoint_to_filter_key( string $endpoint ): string { return trim( str_replace( '/', '_', $endpoint ), '_' ); } /** * Gets content of asset file. * * @param string $path Path of the asset file. * * @since 3.8.0 * * @return Asset_Info */ function get_asset_info( string $path ) { return require plugin_dir_path( PARSELY_FILE ) . $path; }