Completed
Branch 2.0 (acba87)
by Vermeulen
02:20
created

Order   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getExpr() 0 3 1
A getSort() 0 3 1
A __construct() 0 4 1
A generate() 0 16 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