Skip to content

Commit

Permalink
Regroup some code under namespaces
Browse files Browse the repository at this point in the history
Move ParserException to Exception namespace

Regroup Money Parsers

Regroup formatters

Fix namespace

Fix exceptions in tests
  • Loading branch information
Márk Sági-Kazár committed Feb 3, 2016
1 parent 9966de0 commit 3a685f1
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 33 deletions.
12 changes: 12 additions & 0 deletions src/Exception/ParserException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Money\Exception;

/**
* Thrown when a string cannot be parsed to a Money object.
*
* @author Frederik Bosch <f.bosch@genkgo.nl>
*/
final class ParserException extends \Exception
{
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

namespace Money;
namespace Money\Formatter;

use Money\Money;
use Money\MoneyFormatter;

/**
* Formats a Money object using intl extension.
Expand Down Expand Up @@ -37,6 +40,7 @@ public function format(Money $money)

$fractionDigits = $this->formatter->getAttribute(\NumberFormatter::FRACTION_DIGITS);
$valueLength = strlen($valueBase);

if ($valueLength > $fractionDigits) {
$subunits = substr($valueBase, 0, $valueLength - $fractionDigits).'.';
$subunits .= substr($valueBase, $valueLength - $fractionDigits);
Expand Down
15 changes: 11 additions & 4 deletions src/MoneyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@

namespace Money;

/**
* Parses a string into a Money object.
*
* @author Frederik Bosch <f.bosch@genkgo.nl>
*/
interface MoneyParser
{
/**
* @param $formattedMoney
* @param null $forceCurrency
* Parses a string into a Money object (including currency).
*
* @param string $money
* @param string|null $forceCurrency
*
* @return Money
*
* @throws ParserException
* @throws Exception\ParserException
*/
public function parse($formattedMoney, $forceCurrency = null);
public function parse($money, $forceCurrency = null);
}
17 changes: 12 additions & 5 deletions src/IntlMoneyParser.php → src/Parser/IntlMoneyParser.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php

namespace Money;
namespace Money\Parser;

use Money\Currency;
use Money\Exception\ParserException;
use Money\Money;
use Money\MoneyParser;

/**
* Parses a string into a Money object using intl extension.
Expand All @@ -25,16 +30,18 @@ public function __construct(\NumberFormatter $formatter)
/**
* {@inheritdoc}
*/
public function parse($formattedMoney, $forceCurrency = null)
public function parse($money, $forceCurrency = null)
{
$decimal = $this->formatter->parseCurrency($formattedMoney, $currency);
$decimal = $this->formatter->parseCurrency($money, $currency);

if ($decimal === false) {
throw new ParserException(
'Cannot parse '.$formattedMoney.' to Money. '.$this->formatter->getErrorMessage()
'Cannot parse '.$money.' to Money. '.$this->formatter->getErrorMessage()
);
}

$decimal = (string) $decimal;

if (strpos($decimal, '.') !== false) {
$decimal = str_replace('.', '', $decimal);
} else {
Expand All @@ -48,7 +55,7 @@ public function parse($formattedMoney, $forceCurrency = null)
}

if ($forceCurrency === null) {
return new Money($decimal, new Currency($currency));
$forceCurrency = $currency;
}

return new Money($decimal, new Currency($forceCurrency));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
<?php

namespace Money;
namespace Money\Parser;

use Money\Currency;
use Money\Exception\ParserException;
use Money\Money;
use Money\MoneyParser;

/**
* Parses a string into a Money object regular expressions.
*
* @author Frederik Bosch <f.bosch@genkgo.nl>
*/
final class StringToUnitsParser implements MoneyParser
{
/**
Expand Down
7 changes: 0 additions & 7 deletions src/ParserException.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Tests\Money;
namespace Tests\Money\Formatter;

use Money\Currency;
use Money\IntlMoneyFormatter;
use Money\Formatter\IntlMoneyFormatter;
use Money\Money;

final class MoneyFormatterTest extends \PHPUnit_Framework_TestCase
class IntlMoneyFormatterTest extends \PHPUnit_Framework_TestCase
{
public function testRoundMoney()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php

namespace Tests\Money;
namespace Tests\Money\Parser;

use Money\IntlMoneyParser;
use Money\Parser\IntlMoneyParser;

final class IntlMoneyParserTest extends \PHPUnit_Framework_TestCase
class IntlMoneyParserTest extends \PHPUnit_Framework_TestCase
{

public static function provideFormattedMoney()
{
return [
Expand Down Expand Up @@ -42,10 +41,11 @@ public function testIntlParser($string, $units)
$this->assertEquals($units, $parser->parse($string, 'USD')->getAmount());
}

/**
* @expectedException \Money\Exception\ParserException
*/
public function testCannotConvertStringToUnits()
{
$this->setExpectedException('Money\\ParserException');

$formatter = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY);
$formatter->setPattern("¤#,##0.00;-¤#,##0.00");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php

namespace Tests\Money;
namespace Tests\Money\Parser;

use Money\StringToUnitsParser;
use Money\Parser\StringToUnitsParser;

final class StringsToUnitParserTest extends \PHPUnit_Framework_TestCase
{

public static function provideStrings()
{
return [
Expand Down Expand Up @@ -36,6 +35,7 @@ public static function provideStrings()
public function testStringToUnits($string, $units)
{
$parser = new StringToUnitsParser();

$this->assertEquals($units, $parser->parse($string, 'USD')->getAmount());
}

Expand All @@ -45,15 +45,17 @@ public function testStringToUnits($string, $units)
public function testCannotConvertStringToUnits()
{
$parser = new StringToUnitsParser();

$parser->parse('THIS_IS_NOT_CONVERTABLE_TO_UNIT', 'USD');
}

/**
* @expectedException \Money\Exception\ParserException
*/
public function testCannotConvertStringToUnitsWithoutCurrency()
{
$this->setExpectedException('Money\\ParserException');

$parser = new StringToUnitsParser();

$parser->parse('$ 100');
}

}

0 comments on commit 3a685f1

Please sign in to comment.