LimitTrait::buildLimit()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 13
nc 5
nop 2
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Query
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Query\Traits\Clause;
16
17
use Phossa2\Query\Interfaces\Clause\LimitInterface;
18
19
/**
20
 * LimitTrait
21
 *
22
 * Implementation of LimitInterface
23
 *
24
 * @package Phossa2\Query
25
 * @author  Hong Zhang <[email protected]>
26
 * @see     LimitInterface
27
 * @version 2.0.0
28
 * @since   2.0.0 added
29
 */
30
trait LimitTrait
31
{
32
    use AbstractTrait;
33
34
    /**
35
     * {@inheritDoc}
36
     */
37
    public function limit(/*# int */ $count, /*# int */ $offset = 0)
38
    {
39
        $clause = &$this->getClause('LIMIT');
40 View Code Duplication
        if ($count || $offset) {
41
            if (!empty($clause)) {
42
                $clause[0] = (int) $count;
43
            } else {
44
                $clause = [(int) $count, (int) $offset];
45
            }
46
        }
47
        return $this;
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function offset(/*# int */ $offset)
54
    {
55
        $clause = &$this->getClause('LIMIT');
56 View Code Duplication
        if (!empty($clause)) {
57
            $clause[1] = (int) $offset;
58
        } else {
59
            $clause = [-1, (int) $offset];
60
        }
61
        return $this;
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67
    public function page(/*# int */ $pageNumber, /*# int */ $perPage = 30)
68
    {
69
        $clause = &$this->getClause('LIMIT');
70
        $clause = [(int) $perPage, ($pageNumber - 1) * $perPage];
71
        return $this;
72
    }
73
74
    /**
75
     * Build LIMIT
76
     *
77
     * @param  string $prefix
78
     * @param  settings
79
     * @return array
80
     * @access protected
81
     */
82
    protected function buildLimit(
83
        /*# string */ $prefix,
84
        array $settings
85
    )/*# : string */ {
86
        $result = [];
87
        $clause = &$this->getClause('LIMIT');
88
        if (!empty($clause)) {
89
            $res = [];
90
            if ($clause[0]) {
91
                $res[] = $clause[0];
92
            }
93
            if ($clause[1]) {
94
                $res[] = 'OFFSET ' . $clause[1];
95
            }
96
            $result[] = join(' ', $res);
97
        }
98
        return $this->joinClause($prefix, ' ', $result, $settings);
99
    }
100
}
101