Order   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

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