Passed
Branch 3.2.x (99ec7e)
by Mario
04:20
created

currency   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 94
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A build_currency_select_menu() 0 18 2
A get_currency_data() 0 5 1
A __construct() 0 5 1
A get_default_currency_data() 0 3 1
A currency_on_left() 0 3 2
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 $operator;
19
	protected $template;
20
21
	/**
22
	 * currency constructor.
23
	 *
24
	 * @param \skouat\ppde\entity\currency    $entity   Currency entity object
25
	 * @param \skouat\ppde\operators\currency $operator Currency operator object
26
	 * @param template                        $template Template object
27
	 *
28
	 * @access public
29
	 */
30
31
	public function __construct(\skouat\ppde\entity\currency $entity, \skouat\ppde\operators\currency $operator, template $template)
32
	{
33
		$this->entity = $entity;
34
		$this->operator = $operator;
35
		$this->template = $template;
36
	}
37
38
	/**
39
	 * Get currency data based on currency ISO code
40
	 *
41
	 * @param string $iso_code
42
	 *
43
	 * @return array
44
	 * @access public
45
	 */
46
	public function get_currency_data($iso_code)
47
	{
48
		$this->entity->data_exists($this->entity->build_sql_data_exists($iso_code));
49
50
		return $this->get_default_currency_data($this->entity->get_id());
51
	}
52
53
	/**
54
	 * Get default currency symbol
55
	 *
56
	 * @param int $id Currency identifier; default: 0
57
	 *
58
	 * @return array
59
	 * @access public
60
	 */
61
	public function get_default_currency_data($id = 0)
62
	{
63
		return $this->entity->get_data($this->operator->build_sql_data($id, true));
64
	}
65
66
	/**
67
	 * Put the currency on the left or on the right of the amount
68
	 *
69
	 * @param int|float $value
70
	 * @param string    $currency
71
	 * @param bool      $on_left
72
	 * @param string    $dec_point
73
	 * @param string    $thousands_sep
74
	 *
75
	 * @return string
76
	 * @access public
77
	 */
78
	public function currency_on_left($value, $currency, $on_left = true, $dec_point = '.', $thousands_sep = '')
79
	{
80
		return $on_left ? $currency . number_format(round($value, 2), 2, $dec_point, $thousands_sep) : number_format(round($value, 2), 2, $dec_point, $thousands_sep) . $currency;
81
	}
82
83
	/**
84
	 * Build pull down menu options of available currency
85
	 *
86
	 * @param int $config_value Currency identifier; default: 0
87
	 *
88
	 * @return void
89
	 * @access public
90
	 */
91
	public function build_currency_select_menu($config_value = 0)
92
	{
93
		// Grab the list of all enabled currencies; 0 is for all data
94
		$currency_items = $this->entity->get_data($this->operator->build_sql_data(0, true));
95
96
		// Process each rule menu item for pull-down
97
		foreach ($currency_items as $currency_item)
98
		{
99
			// Set output block vars for display in the template
100
			$this->template->assign_block_vars('options', array(
101
				'CURRENCY_ID'        => (int) $currency_item['currency_id'],
102
				'CURRENCY_ISO_CODE'  => $currency_item['currency_iso_code'],
103
				'CURRENCY_NAME'      => $currency_item['currency_name'],
104
				'CURRENCY_SYMBOL'    => $currency_item['currency_symbol'],
105
				'S_CURRENCY_DEFAULT' => $config_value == $currency_item['currency_id'],
106
			));
107
		}
108
		unset ($currency_items, $currency_item);
109
	}
110
}
111