Skip to content

Commit b59ee56

Browse files
committed
Poor man mimic of NumberFormat class
1 parent 19720e4 commit b59ee56

10 files changed

+111
-7
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
},
2525
"autoload-dev": {
2626
"psr-4": {
27-
"NoIntlIntegrationTest\\": "tests/integration"
27+
"NoIntlIntegrationTest\\": "tests/integration",
28+
"NoIntlUnitTest\\": "tests/unit"
2829
}
2930
}
3031
}

phpunit.xml

+3
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@
1515
<testsuite name="IntegrationTests">
1616
<directory>./tests/integration</directory>
1717
</testsuite>
18+
<testsuite name="UnitTests">
19+
<directory>./tests/unit</directory>
20+
</testsuite>
1821
</testsuites>
1922
</phpunit>

src/TranslatorFactory.php src/Factory/Translator/TranslatorFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace NoIntl;
5+
namespace NoIntl\Factory\Translator;
66

77
use Interop\Container\ContainerInterface;
88
use Zend\I18n\Translator\Translator;

src/CurrencyFormatFactory.php src/Factory/View/Helper/CurrencyFormatFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace NoIntl;
5+
namespace NoIntl\Factory\View\Helper;
66

77
use Interop\Container\ContainerInterface;
88
use NoIntl\View\Helper\CurrencyFormat;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NoIntl\Factory\View\Helper;
6+
7+
use Interop\Container\ContainerInterface;
8+
use NoIntl\View\Helper\NumberFormat;
9+
use Zend\ServiceManager\Factory\FactoryInterface;
10+
11+
final class NumberFormatFactory implements FactoryInterface
12+
{
13+
/**
14+
* @param ContainerInterface $container
15+
* @param string $requestedName
16+
* @param array|null $options
17+
* @return NumberFormat|object
18+
*/
19+
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
20+
{
21+
$helper = new NumberFormat();
22+
$helper->setLocale(
23+
$this->getLocale($container)
24+
);
25+
26+
return $helper;
27+
}
28+
29+
/**
30+
* @param ContainerInterface $container
31+
* @return string
32+
*/
33+
private function getLocale(ContainerInterface $container): string
34+
{
35+
$config = $container->get('config');
36+
if (!isset($config['no_intl']['default_locale'])) {
37+
return self::DEFAULT_LOCALE;
38+
}
39+
return $config['no_intl']['default_locale'];
40+
}
41+
}

src/Module.php

+5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
namespace NoIntl;
66

7+
use NoIntl\Factory\Translator\TranslatorFactory;
8+
use NoIntl\Factory\View\Helper\CurrencyFormatFactory;
9+
use NoIntl\Factory\View\Helper\NumberFormatFactory;
710
use Zend\I18n\Translator\TranslatorInterface;
811
use Zend\I18n\View\Helper\CurrencyFormat;
12+
use Zend\I18n\View\Helper\NumberFormat;
913
use Zend\ModuleManager\Feature\ConfigProviderInterface;
1014

1115
final class Module implements ConfigProviderInterface
@@ -20,6 +24,7 @@ public function getConfig(): array
2024
'factories' => [
2125
TranslatorInterface::class => TranslatorFactory::class,
2226
CurrencyFormat::class => CurrencyFormatFactory::class,
27+
NumberFormat::class => NumberFormatFactory::class,
2328
],
2429
],
2530
];

src/View/Helper/NumberFormat.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NoIntl\View\Helper;
6+
7+
final class NumberFormat extends \Zend\I18n\View\Helper\NumberFormat
8+
{
9+
public function __construct()
10+
{
11+
}
12+
13+
/**
14+
* Format a number
15+
*
16+
* @param int|float $number
17+
* @param int|null $formatStyle
18+
* @param int|null $formatType
19+
* @param string|null $locale
20+
* @param int|null $decimals
21+
* @param array|null $textAttributes
22+
* @return string
23+
*/
24+
public function __invoke(
25+
$number,
26+
$formatStyle = null,
27+
$formatType = null,
28+
$locale = null,
29+
$decimals = null,
30+
array $textAttributes = null
31+
) {
32+
return number_format((int)$number, $decimals);
33+
}
34+
}

tests/integration/TranslatorFactoryTest.php tests/integration/Factory/Translator/TranslatorFactoryTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
declare(strict_types=1);
44

5-
namespace NoIntlIntegrationTest;
5+
namespace NoIntlIntegrationTest\Factory\Translator;
66

7-
use NoIntl\TranslatorFactory;
7+
use NoIntl\Factory\Translator\TranslatorFactory;
88
use PHPUnit\Framework\TestCase;
99
use Zend\I18n\Translator\Translator;
1010
use Zend\ServiceManager\ServiceManager;

tests/integration/CurrencyFormatFactoryTest.php tests/integration/Factory/View/Helper/CurrencyFormatFactoryTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
declare(strict_types=1);
44

5-
namespace NoIntlIntegrationTest;
5+
namespace NoIntlIntegrationTest\Factory\View\Helper;
66

77
use Interop\Container\ContainerInterface;
8-
use NoIntl\CurrencyFormatFactory;
8+
use NoIntl\Factory\View\Helper\CurrencyFormatFactory;
9+
use PHPUnit\Framework\MockObject\MockObject;
910
use PHPUnit\Framework\TestCase;
1011
use Zend\I18n\View\Helper\CurrencyFormat;
1112

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

1617
public function testCanOverrideCurrencyFormatIntlExtensionRequirement()
1718
{
19+
/** @var ContainerInterface|MockObject $container */
1820
$container = $this->createMock(ContainerInterface::class);
1921
$container->expects($this->once())->method('get')->with('config')->willReturn([
2022
'no_intl' => [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NoIntlIntegrationTest\Factory\View\Helper;
6+
7+
use NoIntl\View\Helper\NumberFormat;
8+
use PHPUnit\Framework\TestCase;
9+
10+
final class NumberFormatFactoryTest extends TestCase
11+
{
12+
public function testCanMimicIntlExtensionBehavior()
13+
{
14+
$helper = new NumberFormat();
15+
$actual = $helper->__invoke(12, null, null, 'en_US', 4);
16+
$this->assertEquals('12.0000', $actual);
17+
}
18+
}

0 commit comments

Comments
 (0)