Completed
Push — master ( 0bc343...39185b )
by Hong
01:56
created

MappingTrait::setMapping()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 10
nc 4
nop 3
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
0 ignored issues
show
Unused Code introduced by
The parameter $tableName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
    )/*# : string */ {
79
        if (isset($this->tbl_mapping[$id])) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
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 */
0 ignored issues
show
Unused Code introduced by
The parameter $columnName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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