|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of Pomm's Foundation package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) 2014 - 2015 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\Converter\Type\BaseRange; |
|
14
|
|
|
use PommProject\Foundation\Session\Session; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* TypeConverter |
|
18
|
|
|
* |
|
19
|
|
|
* Abstract class for converter that use object types like point, circle, |
|
20
|
|
|
* numrange etc. |
|
21
|
|
|
* |
|
22
|
|
|
* @package Foundation |
|
23
|
|
|
* @copyright 2014 - 2015 Grégoire HUBERT |
|
24
|
|
|
* @author Grégoire HUBERT |
|
25
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
|
26
|
|
|
* @abstract |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract class TypeConverter implements ConverterInterface |
|
29
|
|
|
{ |
|
30
|
|
|
protected $class_name; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* getTypeClassName |
|
34
|
|
|
* |
|
35
|
|
|
* Return the type class name |
|
36
|
|
|
* |
|
37
|
|
|
* @access protected |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
|
|
abstract protected function getTypeClassName(); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* __construct |
|
44
|
|
|
* |
|
45
|
|
|
* Set the type class name. |
|
46
|
|
|
* |
|
47
|
|
|
* @access public |
|
48
|
|
|
* @param string $class_name |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct($class_name = null) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->class_name = |
|
53
|
|
|
$class_name === null |
|
54
|
|
|
? $this->getTypeClassName() |
|
55
|
|
|
: $class_name |
|
56
|
|
|
; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* fromPg |
|
61
|
|
|
* |
|
62
|
|
|
* @see ConverterInterface |
|
63
|
|
|
*/ |
|
64
|
|
|
public function fromPg($data, $type, Session $session) |
|
65
|
|
|
{ |
|
66
|
|
|
$data = trim($data); |
|
67
|
|
|
|
|
68
|
|
|
return |
|
69
|
|
|
$data !== '' |
|
70
|
|
|
? $this->createObjectFrom($data) |
|
71
|
|
|
: null |
|
72
|
|
|
; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* toPg |
|
77
|
|
|
* |
|
78
|
|
|
* @see ConverterInterface |
|
79
|
|
|
*/ |
|
80
|
|
View Code Duplication |
public function toPg($data, $type, Session $session) |
|
81
|
|
|
{ |
|
82
|
|
|
return |
|
83
|
|
|
$data !== null |
|
84
|
|
|
? sprintf("%s('%s')", $type, $this->checkData($data)) |
|
85
|
|
|
: sprintf("NULL::%s", $type) |
|
86
|
|
|
; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* toPgStandardFormat |
|
91
|
|
|
* |
|
92
|
|
|
* @see ConverterInterface |
|
93
|
|
|
*/ |
|
94
|
|
View Code Duplication |
public function toPgStandardFormat($data, $type, Session $session) |
|
95
|
|
|
{ |
|
96
|
|
|
return |
|
97
|
|
|
$data !== null |
|
98
|
|
|
? sprintf("%s", str_replace('"', '""', (string) $this->checkData($data))) |
|
99
|
|
|
: null |
|
100
|
|
|
; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* checkData |
|
105
|
|
|
* |
|
106
|
|
|
* Check if data is suitable for Pg conversion. If not an attempt is made |
|
107
|
|
|
* to build the object from the given definition. |
|
108
|
|
|
* |
|
109
|
|
|
* @access public |
|
110
|
|
|
* @param mixed $data |
|
111
|
|
|
* @return object |
|
112
|
|
|
*/ |
|
113
|
|
|
public function checkData($data) |
|
114
|
|
|
{ |
|
115
|
|
|
$class_name = $this->getTypeClassName(); |
|
116
|
|
|
|
|
117
|
|
|
if (!$data instanceof $class_name) { |
|
118
|
|
|
$data = $this->createObjectFrom($data); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $data; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* createObjectFrom |
|
126
|
|
|
* |
|
127
|
|
|
* Create a range object from a given definition. If the object creation |
|
128
|
|
|
* fails, an exception is thrown. |
|
129
|
|
|
* |
|
130
|
|
|
* @access protected |
|
131
|
|
|
* @param mixed $data |
|
132
|
|
|
* @return BaseRange |
|
133
|
|
|
* @throws ConverterException |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function createObjectFrom($data) |
|
136
|
|
|
{ |
|
137
|
|
|
$class_name = $this->class_name; |
|
138
|
|
|
|
|
139
|
|
|
try { |
|
140
|
|
|
return new $class_name($data); |
|
141
|
|
|
} catch (\InvalidArgumentException $e) { |
|
142
|
|
|
throw new ConverterException( |
|
143
|
|
|
sprintf("Unable to create a '%s' instance.", $class_name), |
|
144
|
|
|
null, |
|
145
|
|
|
$e |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|