BoolType.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.1.2
  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. * Bool type converter.
  24. *
  25. * Use to convert bool data between PHP and the database types.
  26. */
  27. class BoolType 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. * Convert bool data into the database format.
  52. *
  53. * @param mixed $value The value to convert.
  54. * @param \Cake\Database\Driver $driver The driver instance to convert with.
  55. * @return bool|null
  56. */
  57. public function toDatabase($value, Driver $driver)
  58. {
  59. if ($value === true || $value === false || $value === null) {
  60. return $value;
  61. }
  62. if (in_array($value, [1, 0, '1', '0'], true)) {
  63. return (bool)$value;
  64. }
  65. throw new InvalidArgumentException(sprintf(
  66. 'Cannot convert value of type `%s` to bool',
  67. getTypeName($value)
  68. ));
  69. }
  70. /**
  71. * Convert bool values to PHP booleans
  72. *
  73. * @param mixed $value The value to convert.
  74. * @param \Cake\Database\Driver $driver The driver instance to convert with.
  75. * @return bool|null
  76. */
  77. public function toPHP($value, Driver $driver)
  78. {
  79. if ($value === null || $value === true || $value === false) {
  80. return $value;
  81. }
  82. if (!is_numeric($value)) {
  83. return strtolower($value) === 'true';
  84. }
  85. return !empty($value);
  86. }
  87. /**
  88. * {@inheritDoc}
  89. *
  90. * @return array
  91. */
  92. public function manyToPHP(array $values, array $fields, Driver $driver)
  93. {
  94. foreach ($fields as $field) {
  95. if (!isset($values[$field]) || $values[$field] === true || $values[$field] === false) {
  96. continue;
  97. }
  98. if ($values[$field] === '1') {
  99. $values[$field] = true;
  100. continue;
  101. }
  102. if ($values[$field] === '0') {
  103. $values[$field] = false;
  104. continue;
  105. }
  106. $value = $values[$field];
  107. if (!is_numeric($value)) {
  108. $values[$field] = strtolower($value) === 'true';
  109. continue;
  110. }
  111. $values[$field] = !empty($value);
  112. }
  113. return $values;
  114. }
  115. /**
  116. * Get the correct PDO binding type for bool data.
  117. *
  118. * @param mixed $value The value being bound.
  119. * @param \Cake\Database\Driver $driver The driver.
  120. * @return int
  121. */
  122. public function toStatement($value, Driver $driver)
  123. {
  124. if ($value === null) {
  125. return PDO::PARAM_NULL;
  126. }
  127. return PDO::PARAM_BOOL;
  128. }
  129. /**
  130. * Marshals request data into PHP booleans.
  131. *
  132. * @param mixed $value The value to convert.
  133. * @return bool|null Converted value.
  134. */
  135. public function marshal($value)
  136. {
  137. if ($value === null) {
  138. return null;
  139. }
  140. if ($value === 'true') {
  141. return true;
  142. }
  143. if ($value === 'false') {
  144. return false;
  145. }
  146. if (!is_scalar($value)) {
  147. return null;
  148. }
  149. return !empty($value);
  150. }
  151. }