PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/station-games/vendor/cakephp/cakephp/src/Database/Type/BinaryType.php

https://gitlab.com/ViniciusP/project-games
PHP | 82 lines | 34 code | 5 blank | 43 comment | 5 complexity | 9efc223f895fb89ffcd07f8808ace3a8 MD5 | raw file
  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\Core\Exception\Exception;
  17. use Cake\Database\Driver;
  18. use Cake\Database\Driver\Sqlserver;
  19. use Cake\Database\Type;
  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 Type
  27. {
  28. /**
  29. * Convert binary data into the database format.
  30. *
  31. * Binary data is not altered before being inserted into the database.
  32. * As PDO will handle reading file handles.
  33. *
  34. * @param string|resource $value The value to convert.
  35. * @param \Cake\Database\Driver $driver The driver instance to convert with.
  36. * @return string|resource
  37. */
  38. public function toDatabase($value, Driver $driver)
  39. {
  40. return $value;
  41. }
  42. /**
  43. * Convert binary into resource handles
  44. *
  45. * @param null|string|resource $value The value to convert.
  46. * @param \Cake\Database\Driver $driver The driver instance to convert with.
  47. * @return resource|null
  48. * @throws \Cake\Core\Exception\Exception
  49. */
  50. public function toPHP($value, Driver $driver)
  51. {
  52. if ($value === null) {
  53. return null;
  54. }
  55. if (is_string($value) && $driver instanceof Sqlserver) {
  56. $value = pack('H*', $value);
  57. }
  58. if (is_string($value)) {
  59. return fopen('data:text/plain;base64,' . base64_encode($value), 'rb');
  60. }
  61. if (is_resource($value)) {
  62. return $value;
  63. }
  64. throw new Exception(sprintf('Unable to convert %s into binary.', gettype($value)));
  65. }
  66. /**
  67. * Get the correct PDO binding type for Binary data.
  68. *
  69. * @param mixed $value The value being bound.
  70. * @param \Cake\Database\Driver $driver The driver.
  71. * @return int
  72. */
  73. public function toStatement($value, Driver $driver)
  74. {
  75. return PDO::PARAM_LOB;
  76. }
  77. }