Skip to content

Commit

Permalink
Poor man mimic of NumberFormat class
Browse files Browse the repository at this point in the history
  • Loading branch information
chukShirley committed Jan 8, 2020
1 parent 19720e4 commit b59ee56
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 7 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
},
"autoload-dev": {
"psr-4": {
"NoIntlIntegrationTest\\": "tests/integration"
"NoIntlIntegrationTest\\": "tests/integration",
"NoIntlUnitTest\\": "tests/unit"
}
}
}
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
<testsuite name="IntegrationTests">
<directory>./tests/integration</directory>
</testsuite>
<testsuite name="UnitTests">
<directory>./tests/unit</directory>
</testsuite>
</testsuites>
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace NoIntl;
namespace NoIntl\Factory\Translator;

use Interop\Container\ContainerInterface;
use Zend\I18n\Translator\Translator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace NoIntl;
namespace NoIntl\Factory\View\Helper;

use Interop\Container\ContainerInterface;
use NoIntl\View\Helper\CurrencyFormat;
Expand Down
41 changes: 41 additions & 0 deletions src/Factory/View/Helper/NumberFormatFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace NoIntl\Factory\View\Helper;

use Interop\Container\ContainerInterface;
use NoIntl\View\Helper\NumberFormat;
use Zend\ServiceManager\Factory\FactoryInterface;

final class NumberFormatFactory implements FactoryInterface
{
/**
* @param ContainerInterface $container
* @param string $requestedName
* @param array|null $options
* @return NumberFormat|object
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$helper = new NumberFormat();
$helper->setLocale(
$this->getLocale($container)
);

return $helper;
}

/**
* @param ContainerInterface $container
* @return string
*/
private function getLocale(ContainerInterface $container): string
{
$config = $container->get('config');
if (!isset($config['no_intl']['default_locale'])) {
return self::DEFAULT_LOCALE;
}
return $config['no_intl']['default_locale'];
}
}
5 changes: 5 additions & 0 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

namespace NoIntl;

use NoIntl\Factory\Translator\TranslatorFactory;
use NoIntl\Factory\View\Helper\CurrencyFormatFactory;
use NoIntl\Factory\View\Helper\NumberFormatFactory;
use Zend\I18n\Translator\TranslatorInterface;
use Zend\I18n\View\Helper\CurrencyFormat;
use Zend\I18n\View\Helper\NumberFormat;
use Zend\ModuleManager\Feature\ConfigProviderInterface;

final class Module implements ConfigProviderInterface
Expand All @@ -20,6 +24,7 @@ public function getConfig(): array
'factories' => [
TranslatorInterface::class => TranslatorFactory::class,
CurrencyFormat::class => CurrencyFormatFactory::class,
NumberFormat::class => NumberFormatFactory::class,
],
],
];
Expand Down
34 changes: 34 additions & 0 deletions src/View/Helper/NumberFormat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace NoIntl\View\Helper;

final class NumberFormat extends \Zend\I18n\View\Helper\NumberFormat
{
public function __construct()
{
}

/**
* Format a number
*
* @param int|float $number
* @param int|null $formatStyle
* @param int|null $formatType
* @param string|null $locale
* @param int|null $decimals
* @param array|null $textAttributes
* @return string
*/
public function __invoke(
$number,
$formatStyle = null,
$formatType = null,
$locale = null,
$decimals = null,
array $textAttributes = null
) {
return number_format((int)$number, $decimals);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace NoIntlIntegrationTest;
namespace NoIntlIntegrationTest\Factory\Translator;

use NoIntl\TranslatorFactory;
use NoIntl\Factory\Translator\TranslatorFactory;
use PHPUnit\Framework\TestCase;
use Zend\I18n\Translator\Translator;
use Zend\ServiceManager\ServiceManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

declare(strict_types=1);

namespace NoIntlIntegrationTest;
namespace NoIntlIntegrationTest\Factory\View\Helper;

use Interop\Container\ContainerInterface;
use NoIntl\CurrencyFormatFactory;
use NoIntl\Factory\View\Helper\CurrencyFormatFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Zend\I18n\View\Helper\CurrencyFormat;

Expand All @@ -15,6 +16,7 @@ final class CurrencyFormatFactoryTest extends TestCase

public function testCanOverrideCurrencyFormatIntlExtensionRequirement()
{
/** @var ContainerInterface|MockObject $container */
$container = $this->createMock(ContainerInterface::class);
$container->expects($this->once())->method('get')->with('config')->willReturn([
'no_intl' => [
Expand Down
18 changes: 18 additions & 0 deletions tests/integration/Factory/View/Helper/NumberFormatFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace NoIntlIntegrationTest\Factory\View\Helper;

use NoIntl\View\Helper\NumberFormat;
use PHPUnit\Framework\TestCase;

final class NumberFormatFactoryTest extends TestCase
{
public function testCanMimicIntlExtensionBehavior()
{
$helper = new NumberFormat();
$actual = $helper->__invoke(12, null, null, 'en_US', 4);
$this->assertEquals('12.0000', $actual);
}
}

0 comments on commit b59ee56

Please sign in to comment.