IntegerType.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 InvalidArgumentException;
  21. use PDO;
  22. /**
  23. * Integer type converter.
  24. *
  25. * Use to convert integer data between PHP and the database types.
  26. */
  27. class IntegerType 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. * Checks if the value is not a numeric value
  52. *
  53. * @throws \InvalidArgumentException
  54. * @param mixed $value Value to check
  55. * @return void
  56. */
  57. protected function checkNumeric($value)
  58. {
  59. if (!is_numeric($value)) {
  60. throw new InvalidArgumentException(sprintf(
  61. 'Cannot convert value of type `%s` to integer',
  62. getTypeName($value)
  63. ));
  64. }
  65. }
  66. /**
  67. * Convert integer data into the database format.
  68. *
  69. * @param mixed $value The value to convert.
  70. * @param \Cake\Database\Driver $driver The driver instance to convert with.
  71. * @return int|null
  72. */
  73. public function toDatabase($value, Driver $driver)
  74. {
  75. if ($value === null || $value === '') {
  76. return null;
  77. }
  78. $this->checkNumeric($value);
  79. return (int)$value;
  80. }
  81. /**
  82. * Convert integer values to PHP integers
  83. *
  84. * @param mixed $value The value to convert.
  85. * @param \Cake\Database\Driver $driver The driver instance to convert with.
  86. * @return int|null
  87. */
  88. public function toPHP($value, Driver $driver)
  89. {
  90. if ($value === null) {
  91. return $value;
  92. }
  93. return (int)$value;
  94. }
  95. /**
  96. * {@inheritDoc}
  97. *
  98. * @return array
  99. */
  100. public function manyToPHP(array $values, array $fields, Driver $driver)
  101. {
  102. foreach ($fields as $field) {
  103. if (!isset($values[$field])) {
  104. continue;
  105. }
  106. $this->checkNumeric($values[$field]);
  107. $values[$field] = (int)$values[$field];
  108. }
  109. return $values;
  110. }
  111. /**
  112. * Get the correct PDO binding type for integer data.
  113. *
  114. * @param mixed $value The value being bound.
  115. * @param \Cake\Database\Driver $driver The driver.
  116. * @return int
  117. */
  118. public function toStatement($value, Driver $driver)
  119. {
  120. return PDO::PARAM_INT;
  121. }
  122. /**
  123. * Marshals request data into PHP floats.
  124. *
  125. * @param mixed $value The value to convert.
  126. * @return int|null Converted value.
  127. */
  128. public function marshal($value)
  129. {
  130. if ($value === null || $value === '') {
  131. return null;
  132. }
  133. if (is_numeric($value)) {
  134. return (int)$value;
  135. }
  136. return null;
  137. }
  138. }