Format.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ W4Co9BZ1T+9KoqYMVBEnVc73kYcyqlgXPXNX6tQGwDc=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2017 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.9 [rev.1.9.01]
  11. */
  12. namespace Cube\Locale;
  13. class Format
  14. {
  15. /**
  16. * US numeric format
  17. * 1,234,567.89
  18. */
  19. const US = 1;
  20. /**
  21. * EU numeric format
  22. * 1.234.567,89
  23. */
  24. const EU = 2;
  25. /**
  26. * available formats
  27. */
  28. protected $_formats = array(
  29. self::US,
  30. self::EU,
  31. );
  32. /**
  33. *
  34. * format set in settings
  35. *
  36. * @var int
  37. */
  38. protected $_format;
  39. /**
  40. *
  41. * decimals for numeric to localized
  42. *
  43. * @var int
  44. */
  45. protected $_decimals = 0;
  46. /**
  47. *
  48. * holds an instance of the object
  49. *
  50. * @var $this
  51. */
  52. private static $_instance;
  53. /**
  54. *
  55. * returns an instance of the object and creates it if it wasnt instantiated yet
  56. *
  57. * @return $this
  58. */
  59. public static function getInstance()
  60. {
  61. if (!self::$_instance instanceof self) {
  62. self::$_instance = new self();
  63. }
  64. return self::$_instance;
  65. }
  66. /**
  67. *
  68. * get format
  69. *
  70. * @return int
  71. */
  72. public function getFormat()
  73. {
  74. return $this->_format;
  75. }
  76. /**
  77. *
  78. * set format
  79. *
  80. * @param int $format
  81. *
  82. * @return $this
  83. */
  84. public function setFormat($format)
  85. {
  86. $this->_format = $format;
  87. return $this;
  88. }
  89. /**
  90. *
  91. * get decimals
  92. *
  93. * @return int
  94. */
  95. public function getDecimals()
  96. {
  97. return $this->_decimals;
  98. }
  99. /**
  100. *
  101. * set decimals
  102. *
  103. * @param int $decimals
  104. *
  105. * @return $this
  106. */
  107. public function setDecimals($decimals)
  108. {
  109. $this->_decimals = $decimals;
  110. return $this;
  111. }
  112. /**
  113. *
  114. * take a localized numeric value and convert it to a standard number
  115. * or return false if the number cannot be formatted based on the settings input
  116. *
  117. * @1.9 if decimals is set to 0 then we will format all input
  118. *
  119. * @param string $value
  120. * @param bool $valueOnFalse
  121. *
  122. * @return float|false
  123. */
  124. public function localizedToNumeric($value, $valueOnFalse = false)
  125. {
  126. if ($this->_isNumeric($value) && $this->getDecimals() > 0) {
  127. return $value;
  128. }
  129. $format = $this->getFormat();
  130. switch ($format) {
  131. case self::US:
  132. if (!preg_match('#^(-+)?\d{1,3}(?:,?\d{3})*(?:\.\d+)?$#', $value)) {
  133. return ($valueOnFalse) ? $value : false;
  134. }
  135. $value = str_replace(',', '', $value);
  136. break;
  137. case self::EU:
  138. if (!preg_match('#^(-+)?\d{1,3}(?:\.?\d{3})*(?:\,\d+)?$#', $value)) {
  139. return ($valueOnFalse) ? $value : false;
  140. }
  141. $value = str_replace(array('.', ','), array('', '.'), $value);
  142. break;
  143. }
  144. return ($this->_isNumeric($value) || $valueOnFalse) ? $value : false;
  145. }
  146. /**
  147. *
  148. * take a numeric value and convert it to a localized number
  149. * or return false if the initial input is not numeric
  150. *
  151. * @param string $value
  152. * @param bool $valueOnFalse
  153. *
  154. * @return string|false
  155. */
  156. public function numericToLocalized($value, $valueOnFalse = false)
  157. {
  158. if (!$this->_isNumeric($value)) {
  159. return ($valueOnFalse) ? $value : false;
  160. }
  161. $format = $this->getFormat();
  162. $decimals = $this->getDecimals();
  163. switch ($format) {
  164. case self::US:
  165. $value = number_format($value, $decimals, '.', ',');
  166. break;
  167. case self::EU:
  168. $value = number_format($value, $decimals, ',', '.');
  169. break;
  170. }
  171. return $value;
  172. }
  173. /**
  174. *
  175. * we use preg rather than is_numeric as we only want to allow decimal values
  176. * (1234567.890)
  177. *
  178. * @param string $value
  179. *
  180. * @return bool
  181. */
  182. protected function _isNumeric($value)
  183. {
  184. return (bool)preg_match('#^-?\d*\.?\d+$#', $value);
  185. }
  186. }