FloatType.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://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 Cake\Database\TypeInterface;
  19. use Cake\Database\Type\BatchCastingInterface;
  20. use PDO;
  21. use RuntimeException;
  22. /**
  23. * Float type converter.
  24. *
  25. * Use to convert float/decimal data between PHP and the database types.
  26. */
  27. class FloatType extends Type implements TypeInterface, BatchCastingInterface
  28. {
  29. /**
  30. * Identifier name for this type.
  31. *
  32. * (This property is declared here again so that the inheritance from
  33. * Cake\Database\Type can be removed in the future.)
  34. *
  35. * @var string|null
  36. */
  37. protected $_name;
  38. /**
  39. * Constructor.
  40. *
  41. * (This method is declared here again so that the inheritance from
  42. * Cake\Database\Type can be removed in the future.)
  43. *
  44. * @param string|null $name The name identifying this type
  45. */
  46. public function __construct($name = null)
  47. {
  48. $this->_name = $name;
  49. }
  50. /**
  51. * The class to use for representing number objects
  52. *
  53. * @var string
  54. */
  55. public static $numberClass = 'Cake\I18n\Number';
  56. /**
  57. * Whether numbers should be parsed using a locale aware parser
  58. * when marshalling string inputs.
  59. *
  60. * @var bool
  61. */
  62. protected $_useLocaleParser = false;
  63. /**
  64. * Convert integer data into the database format.
  65. *
  66. * @param string|resource $value The value to convert.
  67. * @param \Cake\Database\Driver $driver The driver instance to convert with.
  68. * @return float|null
  69. */
  70. public function toDatabase($value, Driver $driver)
  71. {
  72. if ($value === null || $value === '') {
  73. return null;
  74. }
  75. return (float)$value;
  76. }
  77. /**
  78. * Convert float values to PHP integers
  79. *
  80. * @param null|string|resource $value The value to convert.
  81. * @param \Cake\Database\Driver $driver The driver instance to convert with.
  82. * @return float|null
  83. * @throws \Cake\Core\Exception\Exception
  84. */
  85. public function toPHP($value, Driver $driver)
  86. {
  87. if ($value === null) {
  88. return null;
  89. }
  90. return (float)$value;
  91. }
  92. /**
  93. * {@inheritDoc}
  94. *
  95. * @return array
  96. */
  97. public function manyToPHP(array $values, array $fields, Driver $driver)
  98. {
  99. foreach ($fields as $field) {
  100. if (!isset($values[$field])) {
  101. continue;
  102. }
  103. $values[$field] = (float)$values[$field];
  104. }
  105. return $values;
  106. }
  107. /**
  108. * Get the correct PDO binding type for integer data.
  109. *
  110. * @param mixed $value The value being bound.
  111. * @param \Cake\Database\Driver $driver The driver.
  112. * @return int
  113. */
  114. public function toStatement($value, Driver $driver)
  115. {
  116. return PDO::PARAM_STR;
  117. }
  118. /**
  119. * Marshalls request data into PHP floats.
  120. *
  121. * @param mixed $value The value to convert.
  122. * @return float|null Converted value.
  123. */
  124. public function marshal($value)
  125. {
  126. if ($value === null || $value === '') {
  127. return null;
  128. }
  129. if (is_numeric($value)) {
  130. return (float)$value;
  131. }
  132. if (is_string($value) && $this->_useLocaleParser) {
  133. return $this->_parseValue($value);
  134. }
  135. if (is_array($value)) {
  136. return 1.0;
  137. }
  138. return $value;
  139. }
  140. /**
  141. * Sets whether or not to parse numbers passed to the marshal() function
  142. * by using a locale aware parser.
  143. *
  144. * @param bool $enable Whether or not to enable
  145. * @return $this
  146. */
  147. public function useLocaleParser($enable = true)
  148. {
  149. if ($enable === false) {
  150. $this->_useLocaleParser = $enable;
  151. return $this;
  152. }
  153. if (static::$numberClass === 'Cake\I18n\Number' ||
  154. is_subclass_of(static::$numberClass, 'Cake\I18n\Number')
  155. ) {
  156. $this->_useLocaleParser = $enable;
  157. return $this;
  158. }
  159. throw new RuntimeException(
  160. sprintf('Cannot use locale parsing with the %s class', static::$numberClass)
  161. );
  162. }
  163. /**
  164. * Converts a string into a float point after parsing it using the locale
  165. * aware parser.
  166. *
  167. * @param string $value The value to parse and convert to an float.
  168. * @return float
  169. */
  170. protected function _parseValue($value)
  171. {
  172. $class = static::$numberClass;
  173. return $class::parseFloat($value);
  174. }
  175. }