Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
@@ -12,31 +12,44 @@
|
||||
namespace IPS\nexus;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use IPS\Lang;
|
||||
use IPS\Math\Number;
|
||||
use IPS\Member;
|
||||
use IPS\Settings;
|
||||
use NumberFormatter;
|
||||
use function defined;
|
||||
use function floatval;
|
||||
use function function_exists;
|
||||
use function intval;
|
||||
use function is_string;
|
||||
use function strlen;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Money Object
|
||||
*/
|
||||
class _Money
|
||||
class Money
|
||||
{
|
||||
/**
|
||||
* Get available currencies
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function currencies()
|
||||
public static function currencies() : array
|
||||
{
|
||||
if ( $currencies = json_decode( \IPS\Settings::i()->nexus_currency, TRUE ) )
|
||||
if ( $currencies = json_decode( Settings::i()->nexus_currency, TRUE ) )
|
||||
{
|
||||
return array_keys( $currencies );
|
||||
}
|
||||
else
|
||||
{
|
||||
return array( \IPS\Settings::i()->nexus_currency );
|
||||
return array( Settings::i()->nexus_currency );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +59,7 @@ class _Money
|
||||
* @param string $currency Currency code
|
||||
* @return int
|
||||
*/
|
||||
public static function numberOfDecimalsForCurrency( $currency )
|
||||
public static function numberOfDecimalsForCurrency( string $currency ) : int
|
||||
{
|
||||
switch ( $currency )
|
||||
{
|
||||
@@ -95,17 +108,17 @@ class _Money
|
||||
/**
|
||||
* @brief Amount
|
||||
*/
|
||||
public $amount;
|
||||
public ?Number $amount = null;
|
||||
|
||||
/**
|
||||
* @brief Currency
|
||||
*/
|
||||
public $currency;
|
||||
public string $currency = "";
|
||||
|
||||
/**
|
||||
* @brief Number of decimal points
|
||||
*/
|
||||
protected $numberOfDecimalPlaces = 2;
|
||||
protected int $numberOfDecimalPlaces = 2;
|
||||
|
||||
/**
|
||||
* Contructor
|
||||
@@ -114,18 +127,18 @@ class _Money
|
||||
* @param string $currency Currency code
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $amount, $currency )
|
||||
public function __construct( mixed $amount, string $currency )
|
||||
{
|
||||
$this->currency = $currency;
|
||||
$this->numberOfDecimalPlaces = static::numberOfDecimalsForCurrency( $currency );
|
||||
|
||||
if ( !( $amount instanceof \IPS\Math\Number ) )
|
||||
if ( !( $amount instanceof Number ) )
|
||||
{
|
||||
if ( \is_string( $amount ) )
|
||||
if ( is_string( $amount ) )
|
||||
{
|
||||
$amount = \floatval( $amount );
|
||||
$amount = floatval( $amount );
|
||||
}
|
||||
$amount = new \IPS\Math\Number( number_format( $amount, $this->numberOfDecimalPlaces, '.', '' ) );
|
||||
$amount = new Number( number_format( $amount, $this->numberOfDecimalPlaces, '.', '' ) );
|
||||
}
|
||||
|
||||
$this->amount = $amount->round( $this->numberOfDecimalPlaces );
|
||||
@@ -135,9 +148,9 @@ class _Money
|
||||
* Amount as string (not formatted, not locale aware)
|
||||
* Used for gateways where, for example, (float) 10.5 is not acceptable, and (string) "10.50" is required
|
||||
*
|
||||
* @return \IPS\nexus\Money
|
||||
* @return string
|
||||
*/
|
||||
public function amountAsString()
|
||||
public function amountAsString() : string
|
||||
{
|
||||
return sprintf( "%01." . $this->numberOfDecimalPlaces . "F", (string) $this->amount );
|
||||
}
|
||||
@@ -147,27 +160,27 @@ class _Money
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
public function __toString() : string
|
||||
{
|
||||
return $this->toString( \IPS\Member::loggedIn()->language() );
|
||||
return $this->toString( Member::loggedIn()->language() );
|
||||
}
|
||||
|
||||
/**
|
||||
* To string for language
|
||||
*
|
||||
* @param \IPS\Lang $language The language to use
|
||||
* @param Lang $language The language to use
|
||||
* @return string
|
||||
*/
|
||||
public function toString( \IPS\Lang $language )
|
||||
public function toString( Lang $language ) : string
|
||||
{
|
||||
/* If intl is installed, use NumberFormatter */
|
||||
if( class_exists( "NumberFormatter" ) and trim( $language->locale['int_curr_symbol'] ) === $this->currency )
|
||||
{
|
||||
$formatter = new \NumberFormatter( $language->short, \NumberFormatter::CURRENCY );
|
||||
$formatter = new NumberFormatter( $language->short, NumberFormatter::CURRENCY );
|
||||
|
||||
if ( \floatval( (string) $this->amount ) == \intval( (string) $this->amount ) )
|
||||
if ( floatval( (string) $this->amount ) == intval( (string) $this->amount ) )
|
||||
{
|
||||
$formatter->setAttribute( \NumberFormatter::FRACTION_DIGITS, 0 );
|
||||
$formatter->setAttribute( NumberFormatter::FRACTION_DIGITS, 0 );
|
||||
}
|
||||
|
||||
$result = $formatter->formatCurrency( (string) $this->amount, $this->currency );
|
||||
@@ -180,7 +193,7 @@ class _Money
|
||||
}
|
||||
|
||||
/* If this currency matches the locale the user is using, and we are not using PHP 7.4, AND money_format is supported (Windows doesn't support it), use that */
|
||||
if ( \function_exists( 'money_format' ) and version_compare( PHP_VERSION, '7.4.0', '<' ) and trim( $language->locale['int_curr_symbol'] ) === $this->currency )
|
||||
if ( function_exists( 'money_format' ) and version_compare( PHP_VERSION, '7.4.0', '<' ) and trim( $language->locale['int_curr_symbol'] ) === $this->currency )
|
||||
{
|
||||
/* We need to make sure the locale is correct */
|
||||
$currentLocale = setlocale( LC_ALL, '0' );
|
||||
@@ -190,14 +203,14 @@ class _Money
|
||||
$return = money_format( '%n', (string) $this->amount );
|
||||
|
||||
/* Then put the locale back */
|
||||
\IPS\Lang::restoreLocale( $currentLocale );
|
||||
Lang::restoreLocale( $currentLocale );
|
||||
|
||||
/* And return */
|
||||
return $return;
|
||||
}
|
||||
|
||||
/* If it matches any of the installed languages, we can do something with the locale data */
|
||||
foreach ( \IPS\Lang::languages() as $lang )
|
||||
foreach ( Lang::languages() as $lang )
|
||||
{
|
||||
if ( isset( $lang->locale['int_curr_symbol'] ) AND trim( $lang->locale['int_curr_symbol'] ) === $this->currency )
|
||||
{
|
||||
@@ -210,7 +223,7 @@ class _Money
|
||||
if ( $positiveNegativeSymbol )
|
||||
{
|
||||
$positiveNegativeSymbolFormat = $this->amount->isPositive() ? $lang->locale['p_sign_posn'] : $lang->locale['n_sign_posn'];
|
||||
$positiveNegativeSymbolFormatLength = \strlen( $positiveNegativeSymbolFormat );
|
||||
$positiveNegativeSymbolFormatLength = strlen( $positiveNegativeSymbolFormat );
|
||||
for ( $i=0; $i < $positiveNegativeSymbolFormatLength; $i++ )
|
||||
{
|
||||
switch ( $positiveNegativeSymbolFormat[ $i ] )
|
||||
@@ -268,12 +281,12 @@ class _Money
|
||||
/**
|
||||
* Get output for API
|
||||
*
|
||||
* @param \IPS\Member|NULL $authorizedMember The member making the API request or NULL for API Key / client_credentials
|
||||
* @param Member|NULL $authorizedMember The member making the API request or NULL for API Key / client_credentials
|
||||
* @return array
|
||||
* @apiresponse string currency The currency code (e.g. 'USD')
|
||||
* @apiresponse string amount The amount
|
||||
*/
|
||||
public function apiOutput( \IPS\Member $authorizedMember = NULL )
|
||||
public function apiOutput( ?Member $authorizedMember = NULL ): array
|
||||
{
|
||||
return array(
|
||||
'currency' => $this->currency,
|
||||
|
||||
Reference in new issue
Block a user