Passed
Push — develop-3.3.x ( 0d6b30...b6d44e )
by Mario
02:37
created

currency   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 29
c 3
b 0
f 0
dl 0
loc 145
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get_currency_data() 0 5 1
A __construct() 0 11 1
A get_default_currency_data() 0 3 1
A format_currency() 0 8 2
A assign_currency_to_template() 0 8 1
A build_currency_select_menu() 0 11 2
A legacy_currency_format() 0 7 2
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015-2020 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\actions;
12
13
use phpbb\template\template;
14
15
class currency
16
{
17
	protected $entity;
18
	protected $locale;
19
	protected $operator;
20
	protected $template;
21
22
	/**
23
	 * currency constructor.
24
	 *
25
	 * @param \skouat\ppde\entity\currency    $entity   Currency entity object
26
	 * @param \skouat\ppde\actions\locale_icu $locale   PPDE Locale object
27
	 * @param \skouat\ppde\operators\currency $operator Currency operator object
28
	 * @param template                        $template Template object
29
	 *
30
	 * @access public
31
	 */
32
33
	public function __construct(
34
		\skouat\ppde\entity\currency $entity,
35
		locale_icu $locale,
36
		\skouat\ppde\operators\currency $operator,
37
		template $template
38
	)
39
	{
40
		$this->entity = $entity;
41
		$this->locale = $locale;
42
		$this->operator = $operator;
43
		$this->template = $template;
44
	}
45
46
	/**
47
	 * Get currency data based on currency ISO code
48
	 *
49
	 * @param string $iso_code
50
	 *
51
	 * @return array
52
	 * @access public
53
	 */
54
	public function get_currency_data($iso_code): array
55
	{
56
		$this->entity->data_exists($this->entity->build_sql_data_exists($iso_code));
57
58
		return $this->get_default_currency_data($this->entity->get_id());
59
	}
60
61
	/**
62
	 * Retrieves the default currency data.
63
	 *
64
	 * @param int $id The ID of the currency (optional).
65
	 *
66
	 * @return array The default currency data as an array.
67
	 * @access public
68
	 */
69
	public function get_default_currency_data($id = 0): array
70
	{
71
		return $this->entity->get_data($this->operator->build_sql_data($id, true));
72
	}
73
74
	/**
75
	 * Formats the given value as currency based on the PHP intl extension, if available.
76
	 * Otherwise, a basic currency formatter is used.
77
	 *
78
	 * @param float  $value             The value to be formatted as currency.
79
	 * @param string $currency_iso_code The ISO code of the currency.
80
	 * @param string $currency_symbol   The symbol of the currency.
81
	 * @param bool   $on_left           Determines whether the currency symbol should be placed on the left (default:
82
	 *                                  true).
83
	 * @return string The formatted currency string.
84
	 * @access public
85
	 */
86
	public function format_currency($value, $currency_iso_code, $currency_symbol, $on_left = true): string
87
	{
88
		if ($this->locale->is_locale_configured())
89
		{
90
			return $this->locale->numfmt_format_currency($this->locale->numfmt_create(), $value, $currency_iso_code);
91
		}
92
93
		return $this->legacy_currency_format($value, $currency_symbol, $on_left);
94
	}
95
96
	/**
97
	 * Format a value as a legacy currency string
98
	 *
99
	 * @param float  $value           The value to format as currency
100
	 * @param string $currency_symbol The symbol to use as the currency symbol
101
	 * @param bool   $on_left         Optional. Determines whether the currency symbol should be placed on the left or
102
	 *                                right of the formatted value. Default is true (left side).
103
	 * @param string $dec_point       Optional. The string to use as the decimal separator. Default is '.'.
104
	 * @param string $thousands_sep   Optional. The string to use as the thousands separator. Default is an empty
105
	 *                                string.
106
	 * @return string The formatted value as a currency string
107
	 * @access public
108
	 */
109
	public function legacy_currency_format($value, $currency_symbol, $on_left = true, $dec_point = '.', $thousands_sep = ''): string
110
	{
111
		$formatted_value = number_format(round($value, 2), 2, $dec_point, $thousands_sep);
112
113
		return $on_left
114
			? $currency_symbol . $formatted_value
115
			: $formatted_value . $currency_symbol;
116
	}
117
118
	/**
119
	 * Builds a currency select menu.
120
	 *
121
	 * @param int $config_value The selected currency value from the configuration (default is 0).
122
	 *
123
	 * @return void
124
	 * @access public
125
	 */
126
	public function build_currency_select_menu($config_value = 0): void
127
	{
128
		// Grab the list of all enabled currencies; 0 is for all data
129
		$currency_items = $this->entity->get_data($this->operator->build_sql_data(0, true));
130
131
		// Process each menu item for pull-down
132
		foreach ($currency_items as $currency_item)
133
		{
134
			$this->assign_currency_to_template($currency_item, $config_value);
135
		}
136
		unset ($currency_items);
137
	}
138
139
	/**
140
	 * Assign currency information to the template.
141
	 *
142
	 * @param array $currency_item The currency item with the following keys:
143
	 *                             - currency_id: The ID of the currency (integer).
144
	 *                             - currency_iso_code: The ISO code of the currency (string).
145
	 *                             - currency_name: The name of the currency (string).
146
	 *                             - currency_symbol: The symbol of the currency (string).
147
	 * @param int   $config_value  The configuration value used to determine the default currency (integer).
148
	 *
149
	 * @return void
150
	 * @access private
151
	 */
152
	private function assign_currency_to_template(array $currency_item, int $config_value): void
153
	{
154
		$this->template->assign_block_vars('options', [
155
			'CURRENCY_ID'        => (int) $currency_item['currency_id'],
156
			'CURRENCY_ISO_CODE'  => $currency_item['currency_iso_code'],
157
			'CURRENCY_NAME'      => $currency_item['currency_name'],
158
			'CURRENCY_SYMBOL'    => $currency_item['currency_symbol'],
159
			'S_CURRENCY_DEFAULT' => $config_value === (int) $currency_item['currency_id'],
160
		]);
161
	}
162
}
163