BinaryType.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\Core\Exception\Exception;
  17. use Cake\Database\Driver;
  18. use Cake\Database\Driver\Sqlserver;
  19. use Cake\Database\Type;
  20. use Cake\Database\TypeInterface;
  21. use PDO;
  22. /**
  23. * Binary type converter.
  24. *
  25. * Use to convert binary data between PHP and the database types.
  26. */
  27. class BinaryType extends Type implements TypeInterface
  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 binary data into the database format.
  52. *
  53. * Binary data is not altered before being inserted into the database.
  54. * As PDO will handle reading file handles.
  55. *
  56. * @param string|resource $value The value to convert.
  57. * @param \Cake\Database\Driver $driver The driver instance to convert with.
  58. * @return string|resource
  59. */
  60. public function toDatabase($value, Driver $driver)
  61. {
  62. return $value;
  63. }
  64. /**
  65. * Convert binary into resource handles
  66. *
  67. * @param resource|string|null $value The value to convert.
  68. * @param \Cake\Database\Driver $driver The driver instance to convert with.
  69. * @return resource|null
  70. * @throws \Cake\Core\Exception\Exception
  71. */
  72. public function toPHP($value, Driver $driver)
  73. {
  74. if ($value === null) {
  75. return null;
  76. }
  77. if (
  78. is_string($value)
  79. && $driver instanceof Sqlserver
  80. && version_compare(PHP_VERSION, '7.0', '<')
  81. ) {
  82. $value = pack('H*', $value);
  83. }
  84. if (is_string($value)) {
  85. return fopen('data:text/plain;base64,' . base64_encode($value), 'rb');
  86. }
  87. if (is_resource($value)) {
  88. return $value;
  89. }
  90. throw new Exception(sprintf('Unable to convert %s into binary.', gettype($value)));
  91. }
  92. /**
  93. * Get the correct PDO binding type for Binary data.
  94. *
  95. * @param mixed $value The value being bound.
  96. * @param \Cake\Database\Driver $driver The driver.
  97. * @return int
  98. */
  99. public function toStatement($value, Driver $driver)
  100. {
  101. return PDO::PARAM_LOB;
  102. }
  103. /**
  104. * Marshals flat data into PHP objects.
  105. *
  106. * Most useful for converting request data into PHP objects
  107. * that make sense for the rest of the ORM/Database layers.
  108. *
  109. * @param mixed $value The value to convert.
  110. * @return mixed Converted value.
  111. */
  112. public function marshal($value)
  113. {
  114. return $value;
  115. }
  116. }