Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
LocalizedNumber
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 1
 __invoke
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * Localization based number formatting
5 *
6 * PHP version 8
7 *
8 * Copyright (C) snowflake productions gmbh 2014.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  View_Helpers
25 * @author   Nicolas Karrer <nkarrer@snowflake.ch>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFind\View\Helper\Root;
31
32use Laminas\View\Helper\AbstractHelper;
33
34/**
35 * Class NumberFormat
36 * allows localization based formatting of numbers in view
37 *
38 * @category VuFind
39 * @package  View_Helpers
40 * @author   Nicolas Karrer <nkarrer@snowflake.ch>
41 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
42 * @link     https://vufind.org/wiki/development Wiki
43 */
44class LocalizedNumber extends AbstractHelper
45{
46    /**
47     * Default decimal point character
48     *
49     * @var string
50     */
51    protected $defaultDecimalPoint = '.';
52
53    /**
54     * Default thousands separator character
55     *
56     * @var string
57     */
58    protected $defaultThousandSep = ',';
59
60    /**
61     * Localize number
62     *
63     * @param int|float $number     Number to format
64     * @param int       $decimals   How many decimal places?
65     * @param bool      $escapeHtml Should we escape the resulting text as HTML?
66     *
67     * @return string
68     */
69    public function __invoke($number, $decimals = 0, $escapeHtml = true)
70    {
71        $translator = $this->getView()->plugin('translate');
72
73        $decimalPoint = $translator(
74            'number_decimal_point',
75            [],
76            $this->defaultDecimalPoint
77        );
78        $thousandSep = $translator(
79            'number_thousands_separator',
80            [],
81            $this->defaultThousandSep
82        );
83        $formattedNumber = number_format(
84            $number,
85            $decimals,
86            $decimalPoint,
87            $thousandSep
88        );
89        if ($escapeHtml) {
90            $escaper = $this->getView()->plugin('escapeHtml');
91            $formattedNumber = $escaper($formattedNumber);
92        }
93
94        return $formattedNumber;
95    }
96}