Passed
Pull Request — develop-3.2.x (#54)
by Mario
05:24 queued 03:13
created

currency::format_currency()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2018 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(\skouat\ppde\entity\currency $entity, locale_icu $locale, \skouat\ppde\operators\currency $operator, template $template)
34
	{
35
		$this->entity = $entity;
36
		$this->locale = $locale;
37
		$this->operator = $operator;
38
		$this->template = $template;
39
	}
40
41
	/**
42
	 * Get currency data based on currency ISO code
43
	 *
44
	 * @param string $iso_code
45
	 *
46
	 * @return array
47
	 * @access public
48
	 */
49
	public function get_currency_data($iso_code)
50
	{
51
		$this->entity->data_exists($this->entity->build_sql_data_exists($iso_code));
52
53
		return $this->get_default_currency_data($this->entity->get_id());
54
	}
55
56
	/**
57
	 * Get default currency symbol
58
	 *
59
	 * @param int $id Currency identifier; default: 0
60
	 *
61
	 * @return array
62
	 * @access public
63
	 */
64
	public function get_default_currency_data($id = 0)
65
	{
66
		return $this->entity->get_data($this->operator->build_sql_data($id, true));
67
	}
68
69
	/**
70
	 * Format currency value, based on the PHP intl extension.
71
	 * If this PHP Extension is not available, we switch on a basic currency formatter.
72
	 *
73
	 * @param int|float $value
74
	 * @param string    $currency_iso_code
75
	 * @param string    $currency_symbol
76
	 * @param bool      $on_left
77
	 *
78
	 * @return string
79
	 * @access public
80
	 */
81
	public function format_currency($value, $currency_iso_code, $currency_symbol, $on_left = true)
82
	{
83
		if ($this->locale->is_locale_configured())
84
		{
85
			return $this->locale->numfmt_format_currency($this->locale->numfmt_create(), $value, $currency_iso_code);
86
		}
87
88
		return $this->currency_on_left($value, $currency_symbol, $on_left);
0 ignored issues
show
Deprecated Code introduced by
The function skouat\ppde\actions\currency::currency_on_left() has been deprecated: 2.2.0 (To be removed: 3.0.0) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

88
		return /** @scrutinizer ignore-deprecated */ $this->currency_on_left($value, $currency_symbol, $on_left);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
89
	}
90
91
	/**
92
	 * Put the currency on the left or on the right of the amount
93
	 *
94
	 * @param int|float $value
95
	 * @param string    $currency_symbol
96
	 * @param bool      $on_left
97
	 * @param string    $dec_point
98
	 * @param string    $thousands_sep
99
	 *
100
	 * @return string
101
	 * @access public
102
	 * @deprecated 2.2.0 (To be removed: 3.0.0)
103
	 *
104
	 */
105
	public function currency_on_left($value, $currency_symbol, $on_left = true, $dec_point = '.', $thousands_sep = '')
106
	{
107
		if ($on_left)
108
		{
109
			return $currency_symbol . number_format(round($value, 2), 2, $dec_point, $thousands_sep);
110
		}
111
112
		return number_format(round($value, 2), 2, $dec_point, $thousands_sep) . $currency_symbol;
113
	}
114
115
	/**
116
	 * Build pull down menu options of available currency
117
	 *
118
	 * @param int $config_value Currency identifier; default: 0
119
	 *
120
	 * @return void
121
	 * @access public
122
	 */
123
	public function build_currency_select_menu($config_value = 0)
124
	{
125
		// Grab the list of all enabled currencies; 0 is for all data
126
		$currency_items = $this->entity->get_data($this->operator->build_sql_data(0, true));
127
128
		// Process each rule menu item for pull-down
129
		foreach ($currency_items as $currency_item)
130
		{
131
			// Set output block vars for display in the template
132
			$this->template->assign_block_vars('options', array(
133
				'CURRENCY_ID'        => (int) $currency_item['currency_id'],
134
				'CURRENCY_ISO_CODE'  => $currency_item['currency_iso_code'],
135
				'CURRENCY_NAME'      => $currency_item['currency_name'],
136
				'CURRENCY_SYMBOL'    => $currency_item['currency_symbol'],
137
				'S_CURRENCY_DEFAULT' => $config_value == $currency_item['currency_id'],
138
			));
139
		}
140
		unset ($currency_items, $currency_item);
141
	}
142
}
143