Completed
Push — 2.0 ( 1160ec...acba87 )
by Vermeulen
05:15
created

Column::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BfwSql\Queries\Parts;
4
5
class Column
6
{
7
    /**
8
     * @var \BfwSql\Queries\Parts\Table $table The table where is the column
9
     */
10
    protected $table;
11
    
12
    /**
13
     * @var string $name The column name
14
     */
15
    protected $name;
16
    
17
    /**
18
     * @var string|null $shortcut The column shortcut
19
     */
20
    protected $shortcut;
21
    
22
    /**
23
     * @var mixed $value The column shortcut
24
     */
25
    protected $value;
26
    
27
    /**
28
     * Construct
29
     * 
30
     * @param \BfwSql\Queries\Parts\Table $table The table where is the column
31
     * @param string $name The column name
32
     * @param string $shortcut The column shortcut
33
     * @param mixed $value The column value
34
     */
35
    public function __construct(
36
        Table $table,
37
        string $name,
38
        string $shortcut = null,
39
        $value = null
40
    ) {
41
        $this->table    = $table;
42
        $this->name     = $name;
43
        $this->shortcut = $shortcut;
44
        $this->value    = $value;
45
    }
46
    
47
    /**
48
     * Getter accessor to property table
49
     * 
50
     * @return \BfwSql\Queries\Parts\Table
51
     */
52
    public function getTable(): Table
53
    {
54
        return $this->table;
55
    }
56
    
57
    /**
58
     * Getter accessor to property name
59
     * 
60
     * @return string
61
     */
62
    public function getName(): string
63
    {
64
        return $this->name;
65
    }
66
    
67
    /**
68
     * Getter accessor to property shortcut
69
     * 
70
     * @return string|null
71
     */
72
    public function getShortcut()
73
    {
74
        return $this->shortcut;
75
    }
76
    
77
    /**
78
     * Getter accessor to property value
79
     * 
80
     * @return mixed
81
     */
82
    public function getValue()
83
    {
84
        return $this->value;
85
    }
86
    
87
    /**
88
     * Generate the sql query for to this column
89
     * 
90
     * @return string
91
     */
92
    public function generate(): string
93
    {
94
        $partQuery = $this->obtainName();
95
        if ($this->shortcut !== null) {
96
            $partQuery .= ' AS `'.$this->shortcut.'`';
97
        }
98
        
99
        return $partQuery;
100
    }
101
    
102
    /**
103
     * Obtain the column name to use into the sql query
104
     * Use shortcut if defined
105
     * Use backtick if needed (not a function or a keyword)
106
     * Add the table name before the column name
107
     * 
108
     * @return string
109
     */
110
    public function obtainName(): string
111
    {
112
        $columnName = $this->name;
113
        $tableName  = $this->table->getName();
114
        
115
        if ($this->table->getShortcut() !== null) {
116
            $tableName = $this->table->getShortcut();
117
        }
118
        
119
        if (
120
            strpos($columnName, ' ') === false &&
121
            strpos($columnName, '(') === false
122
        ) {
123
            //Add quote only if a column has been declared
124
            if ($columnName !== '*') {
125
                $columnName = '`'.$columnName.'`';
126
            }
127
128
            $columnName = '`'.$tableName.'`.'.$columnName;
129
        }
130
        
131
        return $columnName;
132
    }
133
    
134
    /**
135
     * Obtain the value to use into sql query
136
     * Return the string null if the column have the value null in php
137
     * Use quoting system declared into the used query system
138
     * 
139
     * @return string
140
     */
141
    public function obtainValue()
142
    {
143
        if ($this->value === null) {
144
            return 'null';
145
        }
146
        
147
        return $this
0 ignored issues
show
Documentation Bug introduced by
The method getQuoting does not exist on object<BfwSql\Queries\AbstractQuery>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
148
            ->table
149
            ->getQuerySystem()
150
            ->getQuoting()
151
            ->quoteValue($this->name, $this->value)
152
        ;
153
    }
154
}
155