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