Skip to content

Commit 8146e8d

Browse files
WIP
1 parent 980ba4b commit 8146e8d

File tree

16 files changed

+507
-10
lines changed

16 files changed

+507
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
</p>
88

99
Symfony number to words hổ trợ chuyển đổi số sang chữ số Tiếng Việt.
10+

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
"symfony/framework-bundle": "^4.3"
2121
},
2222
"require-dev": {
23+
"nyholm/symfony-bundle-test": "^1.4",
2324
"phpunit/phpunit": "~7.5",
25+
"symfony/templating": "^4.3",
26+
"symfony/twig-bundle": "^4.3",
2427
"scrutinizer/ocular": "^1.5"
2528
},
2629
"autoload": {

src/Bundle.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ class Bundle extends BaseBundle
2525
/**
2626
* @inheritDoc
2727
*/
28-
protected function createContainerExtension(): Extension
28+
public function getContainerExtension(): Extension
2929
{
30-
return new Extension();
30+
if (null === $this->extension) {
31+
$this->extension = new Extension();
32+
}
33+
34+
return $this->extension;
3135
}
3236

3337
}

src/DependencyInjection/Extension.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,42 @@ class Extension extends BaseExtension
2424
*/
2525
public function load(array $configs, ContainerBuilder $container): void
2626
{
27+
$this->prepareConfig($configs);
2728
$configuration = new Configuration();
2829
$config = $this->processConfiguration($configuration, $configs);
2930
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
3031
$loader->load('services.yaml');
31-
$loader->load('n2w.yaml');
3232
$definition = $container->getDefinition('n2w');
3333
$definition->addArgument($config);
3434
}
3535

36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function getAlias(): string
40+
{
41+
return 'n2w';
42+
}
43+
44+
/**
45+
* Thiết lập cấu hình mặc định.
46+
*
47+
* @param $configs
48+
*/
49+
protected function prepareConfig(&$configs): void
50+
{
51+
$defaultConfig = [
52+
'defaults' => [
53+
'dictionary' => 'standard'
54+
],
55+
'dictionaries' => [
56+
'standard' => 'n2w_standard_dictionary',
57+
'south' => 'n2w_south_dictionary'
58+
]
59+
];
60+
61+
foreach ($configs as &$config) {
62+
$config = array_merge($defaultConfig, $config);
63+
}
64+
}
3665
}

src/Resources/config/config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
n2w:
2+
defaults:
3+
dictionary: 'standard'
4+
dictionaries:
5+
standard: 'n2w_standard_dictionary'
6+
south: 'n2w_south_dictionary'

src/Resources/config/n2w.yaml

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/Resources/config/services.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ services:
1010
n2w_standard_dictionary:
1111
class: PHPViet\NumberToWords\Dictionary
1212
public: true
13-
PHPViet\NumberToWords\DictionaryInterface: '@n2w_standard_dictionary'
13+
PHPViet\NumberToWords\DictionaryInterface:
14+
class: PHPViet\NumberToWords\Dictionary
15+
public: true
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/symfony-number-to-words
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace PHPViet\Symfony\NumberToWords\Twig\Extension;
10+
11+
use Twig\TwigFilter;
12+
use Twig\Extension\AbstractExtension;
13+
use PHPViet\Symfony\NumberToWords\Service;
14+
15+
/**
16+
* @author Vuong Minh <vuongxuongminh@gmail.com>
17+
* @since 1.0.0
18+
*/
19+
class N2WExtension extends AbstractExtension
20+
{
21+
22+
/**
23+
* Phone number helper.
24+
*
25+
* @var Service
26+
*/
27+
protected $service;
28+
29+
/**
30+
* Khởi tạo extension
31+
*
32+
* @param Service $helper Phone number helper.
33+
*/
34+
public function __construct(Service $helper)
35+
{
36+
$this->service = $helper;
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function getFilters()
43+
{
44+
return [
45+
new TwigFilter('n2w', [$this->service, 'toWords']),
46+
new TwigFilter('n2c', [$this->service, 'toCurrency']),
47+
];
48+
}
49+
50+
}

tests/BundleTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/symfony-number-to-words
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace PHPViet\Symfony\NumberToWords\Tests;
10+
11+
use PHPViet\Symfony\NumberToWords\Service;
12+
use PHPViet\NumberToWords\DictionaryInterface;
13+
14+
/**
15+
* @author Vuong Minh <vuongxuongminh@gmail.com>
16+
* @since 1.0.0
17+
*/
18+
class BundleTest extends TestCase
19+
{
20+
21+
public function testBundle(): void
22+
{
23+
$container = $this->getContainer();
24+
$this->assertTrue($container->has('n2w'));
25+
$this->assertTrue($container->has('n2w_standard_dictionary'));
26+
$this->assertTrue($container->has('n2w_south_dictionary'));
27+
$this->assertTrue($container->has(DictionaryInterface::class));
28+
$service = $container->get('n2w');
29+
$this->assertInstanceOf(Service::class, $service);
30+
}
31+
32+
}

tests/ChangeDictionaryTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/symfony-number-to-words
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace PHPViet\Symfony\NumberToWords\Tests;
10+
11+
/**
12+
* @author Vuong Minh <vuongxuongminh@gmail.com>
13+
* @since 1.0.0
14+
*/
15+
class ChangeDictionaryTest extends TestCase
16+
{
17+
18+
public function testTransform(): void
19+
{
20+
$kernel = $this->createKernel();
21+
$kernel->addConfigFile(__DIR__ . '/Resources/change-dictionary-config.yaml');
22+
$this->bootKernel();
23+
$service = $this->getContainer()->get('n2w');
24+
$this->assertEquals('một ngàn', $service->toWords(1000));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)