Files

64 lines
2.4 KiB
PHP
Raw Permalink Normal View History

<?php
declare( strict_types=1 );
namespace StreamGoals\Admin;
use StreamGoals\Overlay\Payloads;
/** Pure display helpers. */
final class Format {
/** Test donations are inserted with goal_id 0 and a paypal_order_id starting with this prefix. */
public const TEST_PREFIX = 'TEST-';
/** PayPal-supported currencies (subset). */
public const CURRENCIES = array(
'USD' => 'US Dollar', 'EUR' => 'Euro', 'GBP' => 'British Pound', 'CAD' => 'Canadian Dollar', 'AUD' => 'Australian Dollar',
'NZD' => 'New Zealand Dollar', 'CHF' => 'Swiss Franc', 'SEK' => 'Swedish Krona', 'NOK' => 'Norwegian Krone', 'DKK' => 'Danish Krone',
'PLN' => 'Polish Zloty', 'CZK' => 'Czech Koruna', 'HUF' => 'Hungarian Forint', 'JPY' => 'Japanese Yen', 'SGD' => 'Singapore Dollar',
'HKD' => 'Hong Kong Dollar', 'MXN' => 'Mexican Peso', 'BRL' => 'Brazilian Real', 'PHP' => 'Philippine Peso', 'TWD' => 'New Taiwan Dollar',
'THB' => 'Thai Baht', 'ILS' => 'Israeli Shekel', 'MYR' => 'Malaysian Ringgit', 'CNY' => 'Chinese Yuan',
);
public static function currency( mixed $c ): string {
$c = is_string( $c ) ? strtoupper( trim( $c ) ) : '';
return isset( self::CURRENCIES[ $c ] ) ? $c : 'USD';
}
public static function money( int $minor, string $currency ): string {
return Payloads::money( $minor, $currency );
}
/** Plain decimal for form inputs, e.g. 1050 USD -> "10.50". */
public static function decimal( int $minor, string $currency ): string {
return ( new \StreamGoals\Core\Money( max( 0, $minor ), $currency ) )->toDecimalString();
}
public static function duration( int $seconds ): string {
$seconds = max( 0, $seconds );
if ( 0 === $seconds ) {
return '0 seconds';
}
$parts = array();
foreach ( array( 'day' => 86400, 'hour' => 3600, 'minute' => 60, 'second' => 1 ) as $name => $size ) {
$n = intdiv( $seconds, $size );
$seconds %= $size;
if ( $n > 0 ) {
$parts[] = $n . ' ' . $name . ( 1 === $n ? '' : 's' );
}
}
return implode( ' ', $parts );
}
public static function isTest( array $row ): bool {
return str_starts_with( (string) ( $row['paypal_order_id'] ?? '' ), self::TEST_PREFIX );
}
/** The real name is shown only for non-anonymous donors. Anonymous names are not stored at all. */
public static function donorName( array $row ): string {
if ( ! empty( $row['is_anonymous'] ) || '' === trim( (string) ( $row['donor_name'] ?? '' ) ) ) {
return 'Anonymous';
}
return (string) $row['donor_name'];
}
}