Skip to content

Commit c084730

Browse files
author
MartkCz
committed
added formatter
1 parent 712dcd0 commit c084730

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

src/Formatter.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace WebChemistry\Invoice;
4+
5+
class Formatter implements IFormatter {
6+
7+
const ENGLISH = 'en',
8+
CZECH = 'cs';
9+
10+
/** @var array */
11+
private static $options = [
12+
'cs' => [
13+
'number' => [
14+
'dec' => ',',
15+
'sep' => ' '
16+
],
17+
'money' => '%money %currency',
18+
'date' => 'd.m.Y',
19+
],
20+
'en' => [
21+
'number' => [
22+
'dec' => NULL,
23+
'sep' => NULL
24+
],
25+
'money' => '%currency %money',
26+
'date' => 'd/m/Y',
27+
],
28+
];
29+
30+
/** @var string */
31+
private $lang;
32+
33+
public function __construct($lang = self::ENGLISH) {
34+
$this->lang = $lang;
35+
}
36+
37+
public function formatNumber($float) {
38+
return number_format($float, 2, self::$options[$this->lang]['number']['dec'], self::$options[$this->lang]['number']['sep']);
39+
}
40+
41+
public function formatMoney($float, $currency) {
42+
return strtr(self::$options[$this->lang]['money'], ['%money' => $this->formatNumber($float), '%currency' => $currency]);
43+
}
44+
45+
public function formatDate(\DateTime $date) {
46+
return $date->format(self::$options[$this->lang]['date']);
47+
}
48+
49+
}

src/IFormatter.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WebChemistry\Invoice;
6+
7+
interface IFormatter {
8+
9+
/**
10+
* @param float $float
11+
* @return string
12+
*/
13+
public function formatNumber($float);
14+
15+
/**
16+
* @param float $float
17+
* @param string $currency
18+
* @return string
19+
*/
20+
public function formatMoney($float, $currency);
21+
22+
/**
23+
* @param \DateTime $date
24+
* @return string
25+
*/
26+
public function formatDate(\DateTime $date);
27+
28+
}

0 commit comments

Comments
 (0)