BoolType.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.1.2
  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 Cake\Database\TypeInterface;
  19. use InvalidArgumentException;
  20. use PDO;
  21. /**
  22. * Bool type converter.
  23. *
  24. * Use to convert bool data between PHP and the database types.
  25. */
  26. class BoolType extends Type implements TypeInterface
  27. {
  28. /**
  29. * Identifier name for this type.
  30. *
  31. * (This property is declared here again so that the inheritance from
  32. * Cake\Database\Type can be removed in the future.)
  33. *
  34. * @var string|null
  35. */
  36. protected $_name = null;
  37. /**
  38. * Constructor.
  39. *
  40. * (This method is declared here again so that the inheritance from
  41. * Cake\Database\Type can be removed in the future.)
  42. *
  43. * @param string|null $name The name identifying this type
  44. */
  45. public function __construct($name = null)
  46. {
  47. $this->_name = $name;
  48. }
  49. /**
  50. * Convert bool data into the database format.
  51. *
  52. * @param mixed $value The value to convert.
  53. * @param \Cake\Database\Driver $driver The driver instance to convert with.
  54. * @return bool|null
  55. */
  56. public function toDatabase($value, Driver $driver)
  57. {
  58. if ($value === true || $value === false || $value === null) {
  59. return $value;
  60. }
  61. if (in_array($value, [1, 0, '1', '0'], true)) {
  62. return (bool)$value;
  63. }
  64. throw new InvalidArgumentException('Cannot convert value to bool');
  65. }
  66. /**
  67. * Convert bool values to PHP booleans
  68. *
  69. * @param mixed $value The value to convert.
  70. * @param \Cake\Database\Driver $driver The driver instance to convert with.
  71. * @return bool|null
  72. */
  73. public function toPHP($value, Driver $driver)
  74. {
  75. if ($value === null) {
  76. return null;
  77. }
  78. if (is_string($value) && !is_numeric($value)) {
  79. return strtolower($value) === 'true' ? true : false;
  80. }
  81. return !empty($value);
  82. }
  83. /**
  84. * Get the correct PDO binding type for bool data.
  85. *
  86. * @param mixed $value The value being bound.
  87. * @param \Cake\Database\Driver $driver The driver.
  88. * @return int
  89. */
  90. public function toStatement($value, Driver $driver)
  91. {
  92. if ($value === null) {
  93. return PDO::PARAM_NULL;
  94. }
  95. return PDO::PARAM_BOOL;
  96. }
  97. /**
  98. * Marshalls request data into PHP booleans.
  99. *
  100. * @param mixed $value The value to convert.
  101. * @return bool|null Converted value.
  102. */
  103. public function marshal($value)
  104. {
  105. if ($value === null) {
  106. return null;
  107. }
  108. if ($value === 'true') {
  109. return true;
  110. }
  111. if ($value === 'false') {
  112. return false;
  113. }
  114. return !empty($value);
  115. }
  116. }