1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Pomm's Foundation package. |
4
|
|
|
* |
5
|
|
|
* (c) 2014 - 2017 Grégoire HUBERT <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace PommProject\Foundation\Converter; |
11
|
|
|
|
12
|
|
|
use PommProject\Foundation\Session\Session; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* PgBytea |
16
|
|
|
* |
17
|
|
|
* Bytea converter |
18
|
|
|
* |
19
|
|
|
* @package Foundation |
20
|
|
|
* @copyright 2014 - 2017 Grégoire HUBERT |
21
|
|
|
* @author Grégoire HUBERT <[email protected]> |
22
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
23
|
|
|
*/ |
24
|
|
|
class PgBytea implements ConverterInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @see Pomm\Converter\ConverterInterface |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
public function toPg($data, $type, Session $session) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
return $data !== null |
32
|
|
|
? sprintf( |
33
|
|
|
"%s '%s'", |
34
|
|
|
$type, |
35
|
|
|
$this->escapeByteString($session, $data) |
36
|
|
|
) |
37
|
|
|
: sprintf("NULL::%s", $type) |
38
|
|
|
; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @see Pomm\Converter\ConverterInterface |
43
|
|
|
*/ |
44
|
|
|
public function fromPg($data, $type, Session $session) |
45
|
|
|
{ |
46
|
|
|
return $data !== null |
47
|
|
|
? $session->getConnection()->unescapeBytea($data) |
48
|
|
|
: null |
49
|
|
|
; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @see Pomm\Converter\ConverterInterface |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function toPgStandardFormat($data, $type, Session $session) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
return $data !== null |
58
|
|
|
? sprintf( |
59
|
|
|
'%s', |
60
|
|
|
$this->escapeByteString($session, $data) |
61
|
|
|
) |
62
|
|
|
: null |
63
|
|
|
; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* escapeByteString |
68
|
|
|
* |
69
|
|
|
* Escape a binary string to postgres. |
70
|
|
|
* |
71
|
|
|
* @param mixed $string |
72
|
|
|
* @param Session $session |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
protected function escapeByteString(Session $session, $string) |
76
|
|
|
{ |
77
|
|
|
return preg_replace(["/\\\\/", '/"/'], ["\\", '""'], $session->getConnection()->escapeBytea($string)); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.