FloatType.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Database\Type;
  16. use Cake\Database\Driver;
  17. use Cake\Database\Type;
  18. use PDO;
  19. use RuntimeException;
  20. /**
  21. * Float type converter.
  22. *
  23. * Use to convert float/decimal data between PHP and the database types.
  24. */
  25. class FloatType extends Type
  26. {
  27. /**
  28. * The class to use for representing number objects
  29. *
  30. * @var string
  31. */
  32. public static $numberClass = 'Cake\I18n\Number';
  33. /**
  34. * Whether numbers should be parsed using a locale aware parser
  35. * when marshalling string inputs.
  36. *
  37. * @var bool
  38. */
  39. protected $_useLocaleParser = false;
  40. /**
  41. * Convert integer data into the database format.
  42. *
  43. * @param string|resource $value The value to convert.
  44. * @param \Cake\Database\Driver $driver The driver instance to convert with.
  45. * @return string|resource
  46. */
  47. public function toDatabase($value, Driver $driver)
  48. {
  49. if ($value === null || $value === '') {
  50. return null;
  51. }
  52. if (is_array($value)) {
  53. return 1;
  54. }
  55. return floatval($value);
  56. }
  57. /**
  58. * Convert float values to PHP integers
  59. *
  60. * @param null|string|resource $value The value to convert.
  61. * @param \Cake\Database\Driver $driver The driver instance to convert with.
  62. * @return resource
  63. * @throws \Cake\Core\Exception\Exception
  64. */
  65. public function toPHP($value, Driver $driver)
  66. {
  67. if ($value === null) {
  68. return null;
  69. }
  70. if (is_array($value)) {
  71. return 1;
  72. }
  73. return floatval($value);
  74. }
  75. /**
  76. * Get the correct PDO binding type for integer data.
  77. *
  78. * @param mixed $value The value being bound.
  79. * @param \Cake\Database\Driver $driver The driver.
  80. * @return int
  81. */
  82. public function toStatement($value, Driver $driver)
  83. {
  84. return PDO::PARAM_STR;
  85. }
  86. /**
  87. * Marshalls request data into PHP floats.
  88. *
  89. * @param mixed $value The value to convert.
  90. * @return mixed Converted value.
  91. */
  92. public function marshal($value)
  93. {
  94. if ($value === null || $value === '') {
  95. return null;
  96. }
  97. if (is_numeric($value)) {
  98. return (float)$value;
  99. }
  100. if (is_string($value) && $this->_useLocaleParser) {
  101. return $this->_parseValue($value);
  102. }
  103. if (is_array($value)) {
  104. return 1;
  105. }
  106. return $value;
  107. }
  108. /**
  109. * Sets whether or not to parse numbers passed to the marshal() function
  110. * by using a locale aware parser.
  111. *
  112. * @param bool $enable Whether or not to enable
  113. * @return $this
  114. */
  115. public function useLocaleParser($enable = true)
  116. {
  117. if ($enable === false) {
  118. $this->_useLocaleParser = $enable;
  119. return $this;
  120. }
  121. if (static::$numberClass === 'Cake\I18n\Number' ||
  122. is_subclass_of(static::$numberClass, 'Cake\I18n\Number')
  123. ) {
  124. $this->_useLocaleParser = $enable;
  125. return $this;
  126. }
  127. throw new RuntimeException(
  128. sprintf('Cannot use locale parsing with the %s class', static::$numberClass)
  129. );
  130. }
  131. /**
  132. * Converts a string into a float point after parseing it using the locale
  133. * aware parser.
  134. *
  135. * @param string $value The value to parse and convert to an float.
  136. * @return float
  137. */
  138. protected function _parseValue($value)
  139. {
  140. $class = static::$numberClass;
  141. return $class::parseFloat($value);
  142. }
  143. }