-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Poor man mimic of NumberFormat class
- Loading branch information
1 parent
19720e4
commit b59ee56
Showing
10 changed files
with
111 additions
and
7 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
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
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
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
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,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']; | ||
} | ||
} |
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
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,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); | ||
} | ||
} |
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
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
18 changes: 18 additions & 0 deletions
18
tests/integration/Factory/View/Helper/NumberFormatFactoryTest.php
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,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); | ||
} | ||
} |