Skip to content

Commit

Permalink
Refactor action/currency class
Browse files Browse the repository at this point in the history
  • Loading branch information
Skouat committed Oct 6, 2024
1 parent 95f5528 commit b7db6e1
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 155 deletions.
5 changes: 2 additions & 3 deletions actions/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* PayPal Donation extension for the phpBB Forum Software package.
*
* @copyright (c) 2015-2020 Skouat
* @copyright (c) 2015-2024 Skouat
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/
Expand Down Expand Up @@ -239,8 +239,7 @@ public function is_donor_is_member(): void
*
* @return bool True if the donor is anonymous, false otherwise.
*/
private
function is_donor_anonymous(): bool
private function is_donor_anonymous(): bool
{
return (int) $this->transaction_data['user_id'] === ANONYMOUS || !$this->check_donors_status('user', $this->transaction_data['user_id']);
}
Expand Down
79 changes: 34 additions & 45 deletions actions/currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,98 +3,98 @@
*
* PayPal Donation extension for the phpBB Forum Software package.
*
* @copyright (c) 2015-2020 Skouat
* @copyright (c) 2015-2024 Skouat
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/

namespace skouat\ppde\actions;

use phpbb\template\template;
use skouat\ppde\entity\currency as currency_entity;
use skouat\ppde\operators\currency as currency_operator;

class currency
{
protected $entity;
protected $locale;
protected $operator;
protected $template;
protected $default_currency_data;

/**
* currency constructor.
*
* @param \skouat\ppde\entity\currency $entity Currency entity object
* @param \skouat\ppde\actions\locale_icu $locale PPDE Locale object
* @param \skouat\ppde\operators\currency $operator Currency operator object
* @param template $template Template object
*
* @access public
* @param currency_entity $entity Currency entity object
* @param locale_icu $locale PPDE Locale object
* @param currency_operator $operator Currency operator object
* @param template $template Template object
*/

public function __construct(
\skouat\ppde\entity\currency $entity,
currency_entity $entity,
locale_icu $locale,
\skouat\ppde\operators\currency $operator,
currency_operator $operator,
template $template
)
{
$this->entity = $entity;
$this->locale = $locale;
$this->operator = $operator;
$this->template = $template;
$this->default_currency_data = [];
}

/**
* Get currency data based on currency ISO code
*
* @param string $iso_code
*
* @return array
* @access public
* @param string $iso_code The ISO code of the currency
* @return array Currency data
*/
public function get_currency_data($iso_code): array
public function set_currency_data_from_iso_code(string $iso_code): void
{
$this->entity->data_exists($this->entity->build_sql_data_exists($iso_code));

return $this->get_default_currency_data($this->entity->get_id());
$this->set_default_currency_data($this->entity->get_id());
}

/**
* Retrieves the default currency data.
* Sets the default currency data.
*
* @param int $id The ID of the currency (optional).
*/
public function set_default_currency_data(int $id): void
{
$this->default_currency_data = $this->entity->get_data($this->operator->build_sql_data($id, true))[0];
}

/**
* Gets the default currency data.
*
* @return array The default currency data as an array.
* @access public
*/
public function get_default_currency_data($id = 0): array
public function get_default_currency_data(): array
{
return $this->entity->get_data($this->operator->build_sql_data($id, true));
return ($this->default_currency_data ?? []);
}

/**
* Formats the given value as currency based on the PHP intl extension, if available.
* Otherwise, a basic currency formatter is used.
*
* @param float $value The value to be formatted as currency.
* @param string $currency_iso_code The ISO code of the currency.
* @param string $currency_symbol The symbol of the currency.
* @param bool $on_left Determines whether the currency symbol should be placed on the left (default:
* true).
* @param float $amount The amount to be formatted as currency.
* @return string The formatted currency string.
* @access public
*/
public function format_currency($value, $currency_iso_code, $currency_symbol, $on_left = true): string
public function format_currency(float $amount): string
{
if ($this->locale->is_locale_configured())
{
return $this->locale->numfmt_format_currency($this->locale->numfmt_create(), $value, $currency_iso_code);
return $this->locale->numfmt_format_currency($this->locale->numfmt_create(), $amount, $this->default_currency_data['currency_iso_code']);
}

return $this->legacy_currency_format($value, $currency_symbol, $on_left);
return $this->format_currency_without_intl($amount, $this->default_currency_data['currency_symbol'], $this->default_currency_data['currency_on_left']);
}

/**
* Format a value as a legacy currency string
* Format a value as a currency string without using the intl extension
*
* @param float $value The value to format as currency
* @param string $currency_symbol The symbol to use as the currency symbol
Expand All @@ -104,26 +104,19 @@ public function format_currency($value, $currency_iso_code, $currency_symbol, $o
* @param string $thousands_sep Optional. The string to use as the thousands separator. Default is an empty
* string.
* @return string The formatted value as a currency string
* @access public
*/
public function legacy_currency_format($value, $currency_symbol, $on_left = true, $dec_point = '.', $thousands_sep = ''): string
public function format_currency_without_intl(float $value, string $currency_symbol, bool $on_left = true, string $dec_point = '.', string $thousands_sep = ''): string
{
$formatted_value = number_format(round($value, 2), 2, $dec_point, $thousands_sep);

return $on_left
? $currency_symbol . $formatted_value
: $formatted_value . $currency_symbol;
return $on_left ? $currency_symbol . $formatted_value : $formatted_value . $currency_symbol;
}

/**
* Builds a currency select menu.
*
* @param int $config_value The selected currency value from the configuration (default is 0).
*
* @return void
* @access public
*/
public function build_currency_select_menu($config_value = 0): void
public function build_currency_select_menu(int $config_value = 0): void
{
// Grab the list of all enabled currencies; 0 is for all data
$currency_items = $this->entity->get_data($this->operator->build_sql_data(0, true));
Expand All @@ -133,7 +126,6 @@ public function build_currency_select_menu($config_value = 0): void
{
$this->assign_currency_to_template($currency_item, $config_value);
}
unset ($currency_items);
}

/**
Expand All @@ -145,9 +137,6 @@ public function build_currency_select_menu($config_value = 0): void
* - currency_name (string): The name of the currency.
* - currency_symbol (string): The symbol of the currency.
* @param int $config_value The configuration value used to determine the default currency (integer).
*
* @return void
* @access private
*/
private function assign_currency_to_template(array $currency_item, int $config_value): void
{
Expand Down
56 changes: 17 additions & 39 deletions actions/vars.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public function __construct(
/**
* Get template vars
*
* @return array $this->dp_vars
* @return array Array of template variables
*/
public function get_vars(): array
{
$currency_data = $this->get_currency_data();
$this->dp_vars = $this->populate_template_vars($currency_data);
$this->actions_currency->set_default_currency_data((int) $this->config['ppde_default_currency']);
$this->dp_vars = $this->populate_template_vars();

if ($this->actions_core->is_in_admin())
{
Expand All @@ -65,49 +65,28 @@ public function get_vars(): array
return $this->dp_vars;
}

/**
* Get currency data
*
* @return array
*/
private function get_currency_data(): array
{
$default_currency_data = $this->actions_currency->get_default_currency_data((int) $this->config['ppde_default_currency']);

return [
$default_currency_data[0]['currency_iso_code'],
$default_currency_data[0]['currency_symbol'],
(bool) $default_currency_data[0]['currency_on_left'],
];
}

/**
* Populate template vars
*
* @param array $currency_data
* @return array
* @return array Array of template variables
*/
private function populate_template_vars(array $currency_data): array
private function populate_template_vars(): array
{
return [
['var' => '{USER_ID}', 'value' => $this->user->data['user_id']],
['var' => '{USERNAME}', 'value' => $this->user->data['username']],
['var' => '{SITE_NAME}', 'value' => $this->config['sitename']],
['var' => '{SITE_DESC}', 'value' => $this->config['site_desc']],
['var' => '{BOARD_CONTACT}', 'value' => $this->config['board_contact']],
['var' => '{BOARD_EMAIL}', 'value' => $this->config['board_email']],
['var' => '{BOARD_SIG}', 'value' => $this->config['board_email_sig']],
['var' => '{DONATION_GOAL}', 'value' => $this->actions_currency->format_currency(
(float) $this->config['ppde_goal'], ...$currency_data)],
['var' => '{DONATION_RAISED}', 'value' => $this->actions_currency->format_currency(
(float) $this->config['ppde_raised'], ...$currency_data)],
['var' => '{USER_ID}', 'value' => $this->user->data['user_id']],
['var' => '{USERNAME}', 'value' => $this->user->data['username']],
['var' => '{SITE_NAME}', 'value' => $this->config['sitename']],
['var' => '{SITE_DESC}', 'value' => $this->config['site_desc']],
['var' => '{BOARD_CONTACT}', 'value' => $this->config['board_contact']],
['var' => '{BOARD_EMAIL}', 'value' => $this->config['board_email']],
['var' => '{BOARD_SIG}', 'value' => $this->config['board_email_sig']],
['var' => '{DONATION_GOAL}', 'value' => $this->actions_currency->format_currency((float) $this->config['ppde_goal'])],
['var' => '{DONATION_RAISED}', 'value' => $this->actions_currency->format_currency((float) $this->config['ppde_raised'])],
];
}

/**
* Adds predefined language keys variables to the donation pages.
*
* @return void
*/
private function add_predefined_lang_vars(): void
{
Expand All @@ -120,13 +99,12 @@ private function add_predefined_lang_vars(): void
/**
* Replace template vars in the message
*
* @param string $message
* @return string
* @param string $message The message containing template variables
* @return string The message with template variables replaced
*/
public function replace_template_vars(string $message): string
{
$tpl_ary = array_column($this->dp_vars, 'value', 'var');

return str_replace(array_keys($tpl_ary), array_values($tpl_ary), $message);
return strtr($message, $tpl_ary);
}
}
31 changes: 7 additions & 24 deletions controller/admin/transactions_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* PayPal Donation extension for the phpBB Forum Software package.
*
* @copyright (c) 2015-2020 Skouat
* @copyright (c) 2015-2024 Skouat
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/
Expand Down Expand Up @@ -972,38 +972,21 @@ private function assign_hidden_fields(array $data): void
*/
private function assign_currency_data(array $data): void
{
$currency_mc_data = $this->ppde_actions_currency->get_currency_data($data['mc_currency']);
$currency_settle_data = $this->ppde_actions_currency->get_currency_data($data['settle_currency']);
$this->ppde_actions_currency->set_currency_data_from_iso_code($data['mc_currency']);
$this->ppde_actions_currency->set_currency_data_from_iso_code($data['settle_currency']);

$this->template->assign_vars([
'EXCHANGE_RATE' => '1 ' . $data['mc_currency'] . ' = ' . $data['exchange_rate'] . ' ' . $data['settle_currency'],
'MC_GROSS' => $this->format_currency($data['mc_gross'], $currency_mc_data[0]),
'MC_FEE' => $this->format_currency($data['mc_fee'], $currency_mc_data[0]),
'MC_NET' => $this->format_currency($data['net_amount'], $currency_mc_data[0]),
'SETTLE_AMOUNT' => $this->format_currency($data['settle_amount'], $currency_settle_data[0]),
'MC_GROSS' => $this->ppde_actions_currency->format_currency($data['mc_gross']),
'MC_FEE' => $this->ppde_actions_currency->format_currency($data['mc_fee']),
'MC_NET' => $this->ppde_actions_currency->format_currency($data['net_amount']),
'SETTLE_AMOUNT' => $this->ppde_actions_currency->format_currency($data['settle_amount']),
'L_PPDE_DT_SETTLE_AMOUNT' => $this->language->lang('PPDE_DT_SETTLE_AMOUNT', $data['settle_currency']),
'L_PPDE_DT_EXCHANGE_RATE_EXPLAIN' => $this->language->lang('PPDE_DT_EXCHANGE_RATE_EXPLAIN', $this->user->format_date($data['payment_date'])),
'S_CONVERT' => !((int) $data['settle_amount'] === 0 && empty($data['exchange_rate'])),
]);
}

/**
* Format currency amount.
*
* @param float $amount The amount to format.
* @param array $currency_data Currency data including ISO code, symbol, and position.
* @return string Formatted currency string.
*/
private function format_currency(float $amount, array $currency_data): string
{
return $this->ppde_actions_currency->format_currency(
$amount,
$currency_data['currency_iso_code'],
$currency_data['currency_symbol'],
(bool) $currency_data['currency_on_left']
);
}

/**
* Assign user data to template variables.
*
Expand Down
Loading

0 comments on commit b7db6e1

Please sign in to comment.