Skip to content

Commit a8082a5

Browse files
committed
#7 - Add formatting for units
1 parent 20a31c9 commit a8082a5

File tree

4 files changed

+102
-17
lines changed

4 files changed

+102
-17
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This is a collection of useful classes and functions for every day PHP life. It
99
* Obfuscating sensitive information to protect data against spambots
1010
* Slugifying strings for usage in URLs
1111
* Generating random strings
12+
* Formatting prices and units
1213

1314
These classes are no rocket science, just simple helpers that prevent from wiriting the
1415
same code in various flavours over and over again.
@@ -226,6 +227,8 @@ There are some daily tasks that need to be done in applications. The `Utils` cla
226227

227228
```
228229
use TgUtils\Utils;
230+
use TgUtils\FormatUtils;
231+
use TgUtils\Slugify;
229232
230233
// create a random string
231234
$myId = Utils::generateRandomString(20);
@@ -253,6 +256,12 @@ $masked = Utils::anonymize($aPhoneNumber);
253256
254257
// Slugify a string
255258
$slug = Slugify::slugify('A text that turn into an URL');
259+
260+
// Format a price
261+
$priceString = FormatUtils::formatPrice(3000.643, 'EUR');
262+
263+
// Format a file size
264+
$fileSize = FormatUtils::formatUnit(3000643, 'B');
256265
```
257266

258267
Inspect the [source code](https://github.com/technicalguru/php-utils/blob/src/TgUtils/Utils.php) to find more about the methods available.

src/TgUtils/FormatUtils.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace TgUtils;
4+
5+
use \TgI18n\I18N;
6+
7+
I18N::addI18nFile(__DIR__ . '/../utils_i18n.php', FALSE);
8+
9+
/**
10+
* Provides some formatting utils.
11+
* @author ralph
12+
*
13+
*/
14+
class FormatUtils {
15+
16+
/**
17+
* Format a price value using localization.
18+
* @param float $value - the value
19+
* @param string $currency - the currency
20+
* @param string $language - the language code (optional, default language of I18N class).
21+
* @param string $spaceChar - the space character to be used between value and currency (optional, default is HTML non-breaking space).
22+
* @return string the formatted price in localized manner.
23+
*/
24+
public static function formatPrice($value, $currency, $language = null, $spaceChar = '&nbsp;') {
25+
return number_format(floatval($value), 2, I18N::_('decimal_point', $language), I18N::_('thousand_sep', $language)).$spaceChar.$currency;
26+
}
27+
28+
/**
29+
* Format a unit by using prefixes (decimal and computer bytes only).
30+
* @param int $size - the size to be formatted
31+
* @param string $unit - the unit string
32+
* @param int $precision - how many digits after decimal point shall be displayed (optional, default is 1)
33+
* @param string $language - the language to be used for number formatting separators (optional, default is NULL)
34+
* @param bool $bytes - TRUE when computer byte counting is used, FALSE when decimal base shall be applied (optional, default is TRUE)
35+
* @return string a formatted string
36+
*/
37+
public static function formatUnit($size, $unit, $precision = 1, $language = NULL, $bytes = TRUE) {
38+
$prefixes = $bytes ? array('K', 'M', 'G', 'T' ) : array('k', 'M', 'G', 'T');
39+
$rc = $unit;
40+
$base = $bytes ? 1024 : 1000;
41+
// Only until GB
42+
if ($size > $base) {
43+
$size = $size/$base;
44+
$rc = $prefixes[0].$unit;
45+
}
46+
if ($size > $base) {
47+
$size = $size/$base;
48+
$rc = $prefixes[1].$unit;
49+
}
50+
if ($size > $base) {
51+
$size = $size/$base;
52+
$rc = $prefixes[2].$unit;
53+
}
54+
if ($size > $base) {
55+
$size = $size/$base;
56+
$rc = $prefixes[3].$unit;
57+
}
58+
$size = $rc != $unit ? number_format($size, $precision, I18N::_('decimal_point', $language), I18N::_('thousand_sep', $language)) : number_format($size, 0, I18N::_('decimal_point', $language), I18N::_('thousand_sep', $language));
59+
return $size.' '.$rc;
60+
}
61+
}
62+

src/TgUtils/Utils.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
namespace TgUtils;
44

5-
use \TgI18n\I18N;
6-
7-
I18N::addI18nFile(__DIR__ . '/../utils_i18n.php', FALSE);
8-
95
class Utils {
106

117
/**
@@ -105,17 +101,4 @@ public static function findBy($list, $attr, $value) {
105101
}
106102
return $rc;
107103
}
108-
109-
/**
110-
* Format a price value using localization.
111-
* @param float $value - the value
112-
* @param string $currency - the currency
113-
* @param string $language - the language code (optional, default language of I18N class).
114-
* @param string $spaceChar - the space character to be used between value and currency (optional, default is HTML non-breaking space).
115-
* @return string the formatted price in localized manner.
116-
*/
117-
public static function formatPrice($value, $currency, $language = null, $spaceChar = '&nbsp;') {
118-
return number_format(floatval($value), 2, I18N::_('decimal_point', $language), I18N::_('thousand_sep', $language)).$spaceChar.$currency;
119-
}
120-
121104
}

tests/TgUtils/FormatUtilsTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace TgUtils;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
/**
8+
* Tests some slugify methods.
9+
* @author ralph
10+
*
11+
*/
12+
class FormatUtilsTest extends TestCase {
13+
14+
public function testFormatPrice(): void {
15+
$value = 3000.643;
16+
$this->assertEquals('3.000,64 EUR', FormatUtils::formatPrice($value, 'EUR', 'de', ' '));
17+
}
18+
19+
public function testFormatUnitWithBytes(): void {
20+
$value = 3000643;
21+
$this->assertEquals('2,86 MB', FormatUtils::formatUnit($value, 'B', 2, 'de'));
22+
}
23+
24+
25+
public function testFormatUnitWithDecimal(): void {
26+
$value = 303400643;
27+
$this->assertEquals('303,401 MW', FormatUtils::formatUnit($value, 'W', 3, 'de', FALSE));
28+
}
29+
30+
}
31+

0 commit comments

Comments
 (0)