IntegerType.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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\Error;
  18. use PDO;
  19. /**
  20. * Integer type converter.
  21. *
  22. * Use to convert integer data between PHP and the database types.
  23. */
  24. class IntegerType extends \Cake\Database\Type {
  25. /**
  26. * Convert integer data into the database format.
  27. *
  28. * @param string|resource $value The value to convert.
  29. * @param Driver $driver The driver instance to convert with.
  30. * @return string|resource
  31. */
  32. public function toDatabase($value, Driver $driver) {
  33. if ($value === null || $value === '') {
  34. return null;
  35. }
  36. return (int)$value;
  37. }
  38. /**
  39. * Convert integer values to PHP integers
  40. *
  41. * @param null|string|resource $value The value to convert.
  42. * @param Driver $driver The driver instance to convert with.
  43. * @return resource
  44. * @throws \Cake\Core\Exception\Exception
  45. */
  46. public function toPHP($value, Driver $driver) {
  47. if ($value === null) {
  48. return null;
  49. }
  50. return (int)$value;
  51. }
  52. /**
  53. * Get the correct PDO binding type for integer data.
  54. *
  55. * @param mixed $value The value being bound.
  56. * @param Driver $driver The driver.
  57. * @return int
  58. */
  59. public function toStatement($value, Driver $driver) {
  60. return PDO::PARAM_INT;
  61. }
  62. /**
  63. * Marshalls request data into PHP floats.
  64. *
  65. * @param mixed $value The value to convert.
  66. * @return mixed Converted value.
  67. */
  68. public function marshal($value) {
  69. if ($value === null || $value === '') {
  70. return null;
  71. }
  72. return (int)$value;
  73. }
  74. }