Skip to content

Commit

Permalink
added formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
MartkCz committed Jun 1, 2017
1 parent 712dcd0 commit c084730
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/Formatter.php
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']);
}

}
28 changes: 28 additions & 0 deletions src/IFormatter.php
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);

}

0 comments on commit c084730

Please sign in to comment.