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\Exception\ConverterException; |
13
|
|
|
use PommProject\Foundation\Session\Session; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* PgBoolean |
17
|
|
|
* |
18
|
|
|
* Converter for boolean type. |
19
|
|
|
* |
20
|
|
|
* @package Foundation |
21
|
|
|
* @copyright 2014 - 2017 Grégoire HUBERT |
22
|
|
|
* @author Grégoire HUBERT <[email protected]> |
23
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
24
|
|
|
* @see ConverterInterface |
25
|
|
|
*/ |
26
|
|
|
class PgBoolean implements ConverterInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @see ConverterInterface |
30
|
|
|
*/ |
31
|
|
|
public function fromPg($data, $type, Session $session) |
32
|
|
|
{ |
33
|
|
|
$data = trim($data); |
34
|
|
|
|
35
|
|
|
if (!preg_match('/^(t|f)$/', $data)) { |
36
|
|
|
if ($data === '') { |
37
|
|
|
return null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
throw new ConverterException(sprintf("Unknown %s data '%s'.", $type, $data)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return (bool) ($data === 't'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @see ConverterInterface |
48
|
|
|
*/ |
49
|
|
|
public function toPg($data, $type, Session $session) |
50
|
|
|
{ |
51
|
|
|
if ($data === null) { |
52
|
|
|
return sprintf("NULL::%s", $type); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return sprintf("%s '%s'", $type, $data === true ? "true" : "false"); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @see ConverterInterface |
60
|
|
|
*/ |
61
|
|
|
public function toPgStandardFormat($data, $type, Session $session) |
62
|
|
|
{ |
63
|
|
|
if ($data !== null) { |
64
|
|
|
return $data === true ? 't' : 'f'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|