Skip to content

Commit 980ba4b

Browse files
Added base src
1 parent b4f90e8 commit 980ba4b

File tree

7 files changed

+250
-1
lines changed

7 files changed

+250
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"require": {
1818
"php": "^7.1",
1919
"phpviet/number-to-words": "^1.0",
20-
"symfony/framework-bundle": "^4.0"
20+
"symfony/framework-bundle": "^4.3"
2121
},
2222
"require-dev": {
2323
"phpunit/phpunit": "~7.5",

src/Bundle.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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;
10+
11+
use Symfony\Component\HttpKernel\Bundle\Bundle as BaseBundle;
12+
use PHPViet\Symfony\NumberToWords\DependencyInjection\Extension;
13+
14+
/**
15+
* @author Vuong Minh <vuongxuongminh@gmail.com>
16+
* @since 1.0.0
17+
*/
18+
class Bundle extends BaseBundle
19+
{
20+
/**
21+
* @inheritDoc
22+
*/
23+
protected $name = 'PHPVietNumberToWordsBundle';
24+
25+
/**
26+
* @inheritDoc
27+
*/
28+
protected function createContainerExtension(): Extension
29+
{
30+
return new Extension();
31+
}
32+
33+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\DependencyInjection;
10+
11+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
12+
use Symfony\Component\Config\Definition\ConfigurationInterface;
13+
14+
/**
15+
* @author Vuong Minh <vuongxuongminh@gmail.com>
16+
* @since 1.0.0
17+
*/
18+
class Configuration implements ConfigurationInterface
19+
{
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function getConfigTreeBuilder(): TreeBuilder
24+
{
25+
$treeBuilder = new TreeBuilder('n2w');
26+
$rootNode = $treeBuilder->getRootNode();
27+
$rootNode
28+
->children()
29+
->arrayNode('defaults')
30+
->useAttributeAsKey('name')
31+
->prototype('variable')
32+
->end()
33+
->end()
34+
->arrayNode('dictionaries')
35+
->useAttributeAsKey('name')
36+
->prototype('variable')
37+
->end()
38+
->end()
39+
->end();
40+
41+
return $treeBuilder;
42+
}
43+
44+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\DependencyInjection;
10+
11+
use Symfony\Component\Config\FileLocator;
12+
use Symfony\Component\DependencyInjection\ContainerBuilder;
13+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
14+
use Symfony\Component\HttpKernel\DependencyInjection\Extension as BaseExtension;
15+
16+
/**
17+
* @author Vuong Minh <vuongxuongminh@gmail.com>
18+
* @since 1.0.0
19+
*/
20+
class Extension extends BaseExtension
21+
{
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function load(array $configs, ContainerBuilder $container): void
26+
{
27+
$configuration = new Configuration();
28+
$config = $this->processConfiguration($configuration, $configs);
29+
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
30+
$loader->load('services.yaml');
31+
$loader->load('n2w.yaml');
32+
$definition = $container->getDefinition('n2w');
33+
$definition->addArgument($config);
34+
}
35+
36+
}

src/Resources/config/n2w.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: '@n2w_standard_dictionary'
4+
dictionaries:
5+
standard: '@n2w_standard_dictionary'
6+
south: '@n2w_south_dictionary'

src/Resources/config/services.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
services:
2+
n2w:
3+
class: PHPViet\Symfony\NumberToWords\Service
4+
arguments: ['@service_container']
5+
public: true
6+
shared: false
7+
n2w_south_dictionary:
8+
class: PHPViet\NumberToWords\SouthDictionary
9+
public: true
10+
n2w_standard_dictionary:
11+
class: PHPViet\NumberToWords\Dictionary
12+
public: true
13+
PHPViet\NumberToWords\DictionaryInterface: '@n2w_standard_dictionary'

src/Service.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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;
10+
11+
use PHPViet\NumberToWords\Transformer;
12+
use PHPViet\NumberToWords\DictionaryInterface;
13+
use Symfony\Component\DependencyInjection\ContainerInterface;
14+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
15+
16+
/**
17+
* @author Vuong Minh <vuongxuongminh@gmail.com>
18+
* @since 1.0.0
19+
*/
20+
class Service
21+
{
22+
/**
23+
* Container hổ trợ việc lấy các service từ điển.
24+
*
25+
* @var ContainerInterface
26+
*/
27+
protected $container;
28+
29+
/**
30+
* Chứa thông tin cấu hình từ dev.
31+
*
32+
* @var array
33+
*/
34+
protected $config;
35+
36+
/**
37+
* Khởi tạo dịch vụ chuyển đổi số sang chữ số.
38+
*
39+
* @param ContainerInterface $container
40+
* @param array $config
41+
*/
42+
public function __construct(ContainerInterface $container, array $config = [])
43+
{
44+
$this->container = $container;
45+
$this->config = $config;
46+
}
47+
48+
/**
49+
* Chuyển đổi số sang chữ số.
50+
*
51+
* @param string|int|float $number
52+
* @param string|null $dictionary
53+
* @return string
54+
*/
55+
public function toWords($number, ?string $dictionary = null): string
56+
{
57+
$dictionary = $this->getDictionaryInstance($dictionary);
58+
$transformer = $this->createTransformer($dictionary);
59+
60+
return $transformer->toWords($number);
61+
}
62+
63+
/**
64+
* Chuyển đổi số sang tiền tệ.
65+
*
66+
* @param string|float|int $number
67+
* @param array|string[]|string $unit
68+
* @param string|null $dictionary
69+
* @return string
70+
*/
71+
public function toCurrency($number, $unit = 'đồng', ?string $dictionary = null): string
72+
{
73+
$dictionary = $this->getDictionaryInstance($dictionary);
74+
$transformer = $this->createTransformer($dictionary);
75+
76+
return $transformer->toCurrency($number, $unit);
77+
}
78+
79+
/**
80+
* Khởi tạo đối tượng hổ trợ việc chuyển đổi.
81+
*
82+
* @param DictionaryInterface|null $dictionary
83+
* @return Transformer
84+
*/
85+
protected function createTransformer(?DictionaryInterface $dictionary): Transformer
86+
{
87+
return new Transformer($dictionary);
88+
}
89+
90+
/**
91+
* Trả về từ điển dùng để chuyển đổi số sang chữ số.
92+
*
93+
* @param string|null $dictionary
94+
* @return DictionaryInterface|null
95+
*/
96+
protected function getDictionaryInstance(?string $dictionary): ?DictionaryInterface
97+
{
98+
$dictionary = $dictionary ?? $this->getDefaultDictionary();
99+
100+
if (!$dictionaryService = $this->config['dictionaries'][$dictionary] ?? null) {
101+
throw new InvalidConfigurationException(sprintf('Dictionary (%s) is not defined!', $dictionary));
102+
}
103+
104+
return $this->container->get($dictionaryService);
105+
}
106+
107+
/**
108+
* Trả về từ điển mặc định nếu không chỉ định.
109+
*
110+
* @return string
111+
*/
112+
protected function getDefaultDictionary(): string
113+
{
114+
return $this->config['defaults']['dictionary'];
115+
}
116+
117+
}

0 commit comments

Comments
 (0)