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

Order   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 67
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getExpr() 0 4 1
A getSort() 0 4 1
A generate() 0 17 4
1
<?php
2
3
namespace BfwSql\Queries\Parts;
4
5
class Order
6
{
7
    /**
8
     * @var string $expr The expression to use into the order
9
     */
10
    protected $expr;
11
    
12
    /**
13
     * @var string|null $sort The sort order : ASC or DESC
14
     */
15
    protected $sort;
16
    
17
    /**
18
     * Define the expression and the order to sort
19
     * 
20
     * @param string $expr The expression to use into the order
21
     * @param string|null $sort The sort order : ASC or DESC
22
     */
23
    public function __construct(string $expr, $sort = 'ASC')
24
    {
25
        $this->expr = $expr;
26
        $this->sort = $sort;
27
    }
28
    
29
    /**
30
     * Getter accessor to property expr
31
     * 
32
     * @return string
33
     */
34
    public function getExpr(): string
35
    {
36
        return $this->expr;
37
    }
38
    
39
    /**
40
     * Getter accessor to property expr
41
     * 
42
     * @return string|null
43
     */
44
    public function getSort()
45
    {
46
        return $this->sort;
47
    }
48
    
49
    /**
50
     * Generate the sql query for to this order expression
51
     * 
52
     * @return string
53
     */
54
    public function generate(): string
55
    {
56
        $expr = $this->expr;
57
        
58
        if (
59
            strpos($expr, ' ') === false &&
60
            strpos($expr, '(') === false
61
        ) {
62
            $expr = '`'.$expr.'`';
63
        }
64
        
65
        if ($this->sort === null) {
66
            return $expr;
67
        }
68
        
69
        return $expr.' '.$this->sort;
70
    }
71
}
72