Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Format.php
Go to the documentation of this file.
1 <?php
7 
9 {
13  protected $_scopeResolver;
14 
18  protected $_localeResolver;
19 
23  protected $currencyFactory;
24 
30  public function __construct(
31  \Magento\Framework\App\ScopeResolverInterface $scopeResolver,
32  \Magento\Framework\Locale\ResolverInterface $localeResolver,
34  ) {
35  $this->_scopeResolver = $scopeResolver;
36  $this->_localeResolver = $localeResolver;
37  $this->currencyFactory = $currencyFactory;
38  }
39 
57  public function getNumber($value)
58  {
59  if ($value === null) {
60  return null;
61  }
62 
63  if (!is_string($value)) {
64  return (float)$value;
65  }
66 
67  //trim spaces and apostrophes
68  $value = preg_replace('/[^0-9^\^.,-]/m', '', $value);
69 
70  $separatorComa = strpos($value, ',');
71  $separatorDot = strpos($value, '.');
72 
73  if ($separatorComa !== false && $separatorDot !== false) {
74  if ($separatorComa > $separatorDot) {
75  $value = str_replace(['.', ','], ['', '.'], $value);
76  } else {
77  $value = str_replace(',', '', $value);
78  }
79  } elseif ($separatorComa !== false) {
80  $value = str_replace(',', '.', $value);
81  }
82 
83  return (float)$value;
84  }
85 
93  public function getPriceFormat($localeCode = null, $currencyCode = null)
94  {
95  $localeCode = $localeCode ?: $this->_localeResolver->getLocale();
96  if ($currencyCode) {
97  $currency = $this->currencyFactory->create()->load($currencyCode);
98  } else {
99  $currency = $this->_scopeResolver->getScope()->getCurrentCurrency();
100  }
101 
102  $formatter = new \NumberFormatter(
103  $localeCode . '@currency=' . $currency->getCode(),
104  \NumberFormatter::CURRENCY
105  );
106  $format = $formatter->getPattern();
107  $decimalSymbol = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
108  $groupSymbol = $formatter->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL);
109 
110  $pos = strpos($format, ';');
111  if ($pos !== false) {
112  $format = substr($format, 0, $pos);
113  }
114  $format = preg_replace("/[^0\#\.,]/", '', $format);
115  $totalPrecision = 0;
116  $decimalPoint = strpos($format, '.');
117  if ($decimalPoint !== false) {
118  $totalPrecision = strlen($format) - (strrpos($format, '.') + 1);
119  } else {
120  $decimalPoint = strlen($format);
121  }
122  $requiredPrecision = $totalPrecision;
123  $t = substr($format, $decimalPoint);
124  $pos = strpos($t, '#');
125  if ($pos !== false) {
126  $requiredPrecision = strlen($t) - $pos - $totalPrecision;
127  }
128 
129  if (strrpos($format, ',') !== false) {
130  $group = $decimalPoint - strrpos($format, ',') - 1;
131  } else {
132  $group = strrpos($format, '.');
133  }
134 
135  $result = [
136  //TODO: change interface
137  'pattern' => $currency->getOutputFormat(),
138  'precision' => $totalPrecision,
139  'requiredPrecision' => $requiredPrecision,
140  'decimalSymbol' => $decimalSymbol,
141  'groupSymbol' => $groupSymbol,
142  'groupLength' => $group,
143  'integerRequired' => $totalPrecision == 0,
144  ];
145 
146  return $result;
147  }
148 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
getPriceFormat($localeCode=null, $currencyCode=null)
Definition: Format.php:93
$group
Definition: sections.phtml:16
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
$pos
Definition: list.phtml:42
__construct(\Magento\Framework\App\ScopeResolverInterface $scopeResolver, \Magento\Framework\Locale\ResolverInterface $localeResolver, \Magento\Directory\Model\CurrencyFactory $currencyFactory)
Definition: Format.php:30