BinaryType.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * PHP Version 5.4
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 3.0.0
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. namespace Cake\Database\Type;
  18. use Cake\Database\Driver;
  19. use Cake\Error;
  20. use \PDO;
  21. /**
  22. * Binary type converter.
  23. *
  24. * Use to convert binary data between PHP and the database types.
  25. */
  26. class BinaryType extends \Cake\Database\Type {
  27. /**
  28. * Convert binary data into the database format.
  29. *
  30. * Binary data is not altered before being inserted into the database.
  31. * As PDO will handle reading file handles.
  32. *
  33. * @param string|resource $value The value to convert.
  34. * @param Driver $driver The driver instance to convert with.
  35. * @return string|resource
  36. */
  37. public function toDatabase($value, Driver $driver) {
  38. return $value;
  39. }
  40. /**
  41. * Convert binary into resource handles
  42. *
  43. * @param null|string|resource $value The value to convert.
  44. * @param Driver $driver The driver instance to convert with.
  45. * @return resource
  46. * @throws Cake\Error\Exception
  47. */
  48. public function toPHP($value, Driver $driver) {
  49. if ($value === null) {
  50. return null;
  51. }
  52. if (is_string($value)) {
  53. return fopen('data:text/plain;base64,' . base64_encode($value), 'rb');
  54. }
  55. if (is_resource($value)) {
  56. return $value;
  57. }
  58. throw new Error\Exception(__d('cake_dev', 'Unable to convert %s into binary.', gettype($value)));
  59. }
  60. /**
  61. * Get the correct PDO binding type for Binary data.
  62. *
  63. * @param mixed $value The value being bound.
  64. * @param Driver $driver The driver.
  65. * @return integer
  66. */
  67. public function toStatement($value, Driver $driver) {
  68. return PDO::PARAM_LOB;
  69. }
  70. }