1<?php 2 3namespace Doctrine\DBAL\Types; 4 5use Doctrine\DBAL\ParameterType; 6use Doctrine\DBAL\Platforms\AbstractPlatform; 7 8use function assert; 9use function fopen; 10use function fseek; 11use function fwrite; 12use function is_resource; 13use function is_string; 14 15/** 16 * Type that maps ab SQL BINARY/VARBINARY to a PHP resource stream. 17 */ 18class BinaryType extends Type 19{ 20 /** 21 * {@inheritdoc} 22 */ 23 public function getSQLDeclaration(array $column, AbstractPlatform $platform) 24 { 25 return $platform->getBinaryTypeDeclarationSQL($column); 26 } 27 28 /** 29 * {@inheritdoc} 30 */ 31 public function convertToPHPValue($value, AbstractPlatform $platform) 32 { 33 if ($value === null) { 34 return null; 35 } 36 37 if (is_string($value)) { 38 $fp = fopen('php://temp', 'rb+'); 39 assert(is_resource($fp)); 40 fwrite($fp, $value); 41 fseek($fp, 0); 42 $value = $fp; 43 } 44 45 if (! is_resource($value)) { 46 throw ConversionException::conversionFailed($value, Types::BINARY); 47 } 48 49 return $value; 50 } 51 52 /** 53 * {@inheritdoc} 54 */ 55 public function getName() 56 { 57 return Types::BINARY; 58 } 59 60 /** 61 * {@inheritdoc} 62 */ 63 public function getBindingType() 64 { 65 return ParameterType::BINARY; 66 } 67} 68