|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Phossa Project |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.4 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Library |
|
8
|
|
|
* @package Phossa2\Query |
|
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
|
10
|
|
|
* @license http://mit-license.org/ MIT License |
|
11
|
|
|
* @link http://www.phossa.com/ |
|
12
|
|
|
*/ |
|
13
|
|
|
/*# declare(strict_types=1); */ |
|
14
|
|
|
|
|
15
|
|
|
namespace Phossa2\Query\Traits; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Query\Interfaces\MappingInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* MappingTrait |
|
21
|
|
|
* |
|
22
|
|
|
* Implementation of MappingInterface |
|
23
|
|
|
* |
|
24
|
|
|
* @package Phossa2\Query |
|
25
|
|
|
* @author Hong Zhang <[email protected]> |
|
26
|
|
|
* @see MappingInterface |
|
27
|
|
|
* @version 2.1.0 |
|
28
|
|
|
* @since 2.1.0 added |
|
29
|
|
|
*/ |
|
30
|
|
|
trait MappingTrait |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* table mapping storage |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
* @access protected |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $tbl_mapping = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* column mapping storage |
|
42
|
|
|
* |
|
43
|
|
|
* @var array |
|
44
|
|
|
* @access protected |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $col_mapping = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritDoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function setMapping(/*# string */ $id, $tableMap, $columnMap) |
|
52
|
|
|
{ |
|
53
|
|
|
if ($tableMap) { |
|
54
|
|
|
$this->tbl_mapping[$id] = $tableMap; |
|
55
|
|
|
} else { |
|
56
|
|
|
unset($this->tbl_mapping[$id]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if ($columnMap) { |
|
60
|
|
|
$this->col_mapping[$id] = $columnMap; |
|
61
|
|
|
} else { |
|
62
|
|
|
unset($this->col_mapping[$id]); |
|
63
|
|
|
} |
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Map the given table name to another name |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $id |
|
71
|
|
|
* @param string $tableName |
|
72
|
|
|
* @return string |
|
73
|
|
|
* @access protected |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function mapTable( |
|
76
|
|
|
/*# string */ $id, |
|
77
|
|
|
/*# string */ $tableName |
|
|
|
|
|
|
78
|
|
|
)/*# : string */ { |
|
79
|
|
|
if (isset($this->tbl_mapping[$id])) { |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Map the given column name to another name |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $columnName |
|
87
|
|
|
* @return string |
|
88
|
|
|
* @access public |
|
89
|
|
|
*/ |
|
90
|
|
|
public function mapColumn(/*# string */ $columnName)/*# : string */ |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Do the actual mapping |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $input |
|
99
|
|
|
* @param string|callable $mapping |
|
100
|
|
|
* @return string |
|
101
|
|
|
* @access protected |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function doMapping( |
|
104
|
|
|
/*# string */ $input, |
|
105
|
|
|
$mapping |
|
106
|
|
|
)/*# : string */ { |
|
107
|
|
|
if (is_string($mapping)) { |
|
108
|
|
|
return $mapping . $input; |
|
109
|
|
|
} elseif (is_callable($mapping)) { |
|
110
|
|
|
return (string) $mapping($input); |
|
111
|
|
|
} else { |
|
112
|
|
|
return $input; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.