-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
MartkCz
committed
Jun 1, 2017
1 parent
712dcd0
commit c084730
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace WebChemistry\Invoice; | ||
|
||
class Formatter implements IFormatter { | ||
|
||
const ENGLISH = 'en', | ||
CZECH = 'cs'; | ||
|
||
/** @var array */ | ||
private static $options = [ | ||
'cs' => [ | ||
'number' => [ | ||
'dec' => ',', | ||
'sep' => ' ' | ||
], | ||
'money' => '%money %currency', | ||
'date' => 'd.m.Y', | ||
], | ||
'en' => [ | ||
'number' => [ | ||
'dec' => NULL, | ||
'sep' => NULL | ||
], | ||
'money' => '%currency %money', | ||
'date' => 'd/m/Y', | ||
], | ||
]; | ||
|
||
/** @var string */ | ||
private $lang; | ||
|
||
public function __construct($lang = self::ENGLISH) { | ||
$this->lang = $lang; | ||
} | ||
|
||
public function formatNumber($float) { | ||
return number_format($float, 2, self::$options[$this->lang]['number']['dec'], self::$options[$this->lang]['number']['sep']); | ||
} | ||
|
||
public function formatMoney($float, $currency) { | ||
return strtr(self::$options[$this->lang]['money'], ['%money' => $this->formatNumber($float), '%currency' => $currency]); | ||
} | ||
|
||
public function formatDate(\DateTime $date) { | ||
return $date->format(self::$options[$this->lang]['date']); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WebChemistry\Invoice; | ||
|
||
interface IFormatter { | ||
|
||
/** | ||
* @param float $float | ||
* @return string | ||
*/ | ||
public function formatNumber($float); | ||
|
||
/** | ||
* @param float $float | ||
* @param string $currency | ||
* @return string | ||
*/ | ||
public function formatMoney($float, $currency); | ||
|
||
/** | ||
* @param \DateTime $date | ||
* @return string | ||
*/ | ||
public function formatDate(\DateTime $date); | ||
|
||
} |