1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of BraincraftedBootstrapBundle. |
4
|
|
|
* (c) 2012-2013 by Florian Eckerstorfer |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Braincrafted\Bundle\BootstrapBundle\Form\Type; |
8
|
|
|
|
9
|
|
|
use Symfony\Component\Form\Extension\Core\Type\MoneyType as BaseMoneyType; |
10
|
|
|
use Symfony\Component\Form\FormInterface; |
11
|
|
|
use Symfony\Component\Form\FormView; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* MoneyType |
15
|
|
|
* |
16
|
|
|
* @package BraincraftedBootstrapBundle |
17
|
|
|
* @subpackage Form |
18
|
|
|
* @author Florian Eckerstorfer <[email protected]> |
19
|
|
|
* @copyright 2012-2013 Florian Eckerstorfer |
20
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License |
21
|
|
|
* @link http://bootstrap.braincrafted.com Bootstrap for Symfony2 |
22
|
|
|
*/ |
23
|
|
|
class MoneyType extends BaseMoneyType |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function buildView(FormView $view, FormInterface $form, array $options) |
29
|
|
|
{ |
30
|
|
|
$view->vars['money_pattern'] = self::getPattern($options['currency']); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function getBlockPrefix() |
37
|
|
|
{ |
38
|
|
|
return 'money'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Backward compatibility for SF < 3.0 |
43
|
|
|
* |
44
|
|
|
* @return null|string |
45
|
|
|
*/ |
46
|
|
|
public function getName() { |
47
|
|
|
return $this->getBlockPrefix(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns the pattern for this locale |
52
|
|
|
* |
53
|
|
|
* The pattern contains the placeholder "{{ widget }}" where the HTML tag should |
54
|
|
|
* be inserted |
55
|
|
|
* |
56
|
|
|
* @param string $currency |
57
|
|
|
* |
58
|
|
|
* @return string Returns the pattern |
59
|
|
|
*/ |
60
|
|
|
protected static function getPattern($currency) |
61
|
|
|
{ |
62
|
|
|
if (!$currency) { |
63
|
|
|
return '{{ widget }}'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$locale = \Locale::getDefault(); |
67
|
|
|
|
68
|
|
|
if (!isset(self::$patterns[$locale])) { |
69
|
|
|
self::$patterns[$locale] = array(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (!isset(self::$patterns[$locale][$currency])) { |
73
|
|
|
$format = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); |
74
|
|
|
$pattern = $format->formatCurrency('123', $currency); |
75
|
|
|
|
76
|
|
|
// the spacings between currency symbol and number are ignored, because |
77
|
|
|
// a single space leads to better readability in combination with input |
78
|
|
|
// fields |
79
|
|
|
// the regex also considers non-break spaces (0xC2 or 0xA0 in UTF-8) |
80
|
|
|
|
81
|
|
|
preg_match( |
82
|
|
|
'/^([^\s\xc2\xa0]*)[\s\xc2\xa0]*123(?:[,.]0+)?[\s\xc2\xa0]*([^\s\xc2\xa0]*)$/u', |
83
|
|
|
$pattern, |
84
|
|
|
$matches |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
self::$patterns[$locale][$currency] = self::parsePatternMatches($matches); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return self::$patterns[$locale][$currency]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Parses the given pattern matches array and returns the pattern string. |
95
|
|
|
* |
96
|
|
|
* @param array $matches Pattern matches |
97
|
|
|
* |
98
|
|
|
* @return string Pattern |
99
|
|
|
*/ |
100
|
|
|
protected static function parsePatternMatches(array $matches) |
101
|
|
|
{ |
102
|
|
|
if (!empty($matches[1])) { |
103
|
|
|
return '{{ tag_start }}'.$matches[1].'{{ tag_end }} {{ widget }}'; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (!empty($matches[2])) { |
107
|
|
|
return '{{ widget }} {{ tag_start }}'.$matches[2].'{{ tag_end }}'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// @codeCoverageIgnoreStart |
111
|
|
|
return '{{ widget }}'; |
112
|
|
|
// @codeCoverageIgnoreEnd |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|