Column   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
lcom 2
cbo 2
dl 0
loc 122
c 1
b 0
f 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A partName() 0 4 1
A getName() 0 4 1
A setName() 0 6 1
A getTable() 0 4 1
A setTable() 0 7 1
A getAlias() 0 4 1
A setAlias() 0 16 3
A isAll() 0 4 1
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 6/3/14
5
 * Time: 12:07 AM.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace NilPortugues\Sql\QueryBuilder\Syntax;
12
13
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryException;
14
15
/**
16
 * Class Column.
17
 */
18
class Column implements QueryPartInterface
19
{
20
    const ALL = '*';
21
22
    /**
23
     * @var Table
24
     */
25
    protected $table;
26
27
    /**
28
     * @var string
29
     */
30
    protected $name;
31
32
    /**
33
     * @var string
34
     */
35
    protected $alias;
36
37
    /**
38
     * @param string $name
39
     * @param string $table
40
     * @param string $alias
41
     */
42
    public function __construct($name, $table, $alias = '')
43
    {
44
        $this->setName($name);
45
        $this->setTable($table);
46
        $this->setAlias($alias);
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function partName()
53
    {
54
        return 'COLUMN';
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getName()
61
    {
62
        return $this->name;
63
    }
64
65
    /**
66
     * @param string $name
67
     *
68
     * @return $this
69
     */
70
    public function setName($name)
71
    {
72
        $this->name = (string) $name;
73
74
        return $this;
75
    }
76
77
    /**
78
     * @return Table
79
     */
80
    public function getTable()
81
    {
82
        return $this->table;
83
    }
84
85
    /**
86
     * @param string $table
87
     *
88
     * @return $this
89
     */
90
    public function setTable($table)
91
    {
92
        $newTable = array($table);
93
        $this->table = SyntaxFactory::createTable($newTable);
94
95
        return $this;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getAlias()
102
    {
103
        return $this->alias;
104
    }
105
106
    /**
107
     * @param null|string $alias
108
     *
109
     * @return $this
110
     *
111
     * @throws QueryException
112
     */
113
    public function setAlias($alias)
114
    {
115
        if (0 == \strlen($alias)) {
116
            $this->alias = null;
117
118
            return $this;
119
        }
120
121
        if ($this->isAll()) {
122
            throw new QueryException("Can't use alias because column name is ALL (*)");
123
        }
124
125
        $this->alias = (string) $alias;
126
127
        return $this;
128
    }
129
130
    /**
131
     * Check whether column name is '*' or not.
132
     *
133
     * @return bool
134
     */
135
    public function isAll()
136
    {
137
        return $this->getName() == self::ALL;
138
    }
139
}
140