Completed
Push — master ( 439406...4b2726 )
by Hong
03:09
created

LimitTrait::page()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
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
    /**
33
     * {@inheritDoc}
34
     */
35
    public function limit(/*# int */ $count, /*# int */ $offset = 0)
36
    {
37
        $clause = &$this->getClause('LIMIT');
38 View Code Duplication
        if ($count || $offset) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
            if (!empty($clause)) {
40
                $clause[0] = (int) $count;
41
            } else {
42
                $clause = [(int) $count, (int) $offset];
43
            }
44
        }
45
        return $this;
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    public function offset(/*# int */ $offset)
52
    {
53
        $clause = &$this->getClause('LIMIT');
54 View Code Duplication
        if (!empty($clause)) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
            $clause[1] = (int) $offset;
56
        } else {
57
            $clause = [-1, (int) $offset];
58
        }
59
        return $this;
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    public function page(/*# int */ $pageNumber, /*# int */ $perPage = 30)
66
    {
67
        $clause = &$this->getClause('LIMIT');
68
        $clause = [(int) $perPage, ($pageNumber - 1) * $perPage];
69
        return $this;
70
    }
71
72
    /**
73
     * Build LIMIT
74
     *
75
     * @return array
76
     * @access protected
77
     */
78
    protected function buildLimit(array $settings)/*# : string */
79
    {
80
        $result = [];
81
        $clause = &$this->getClause('LIMIT');
82
        if (!empty($clause)) {
83
            $res = [];
84
            if ($clause[0]) {
85
                $res[] = 'LIMIT ' . $clause[0];
86
            }
87
            if ($clause[1]) {
88
                $res[] = 'OFFSET ' . $clause[1];
89
            }
90
            $result[] = join(' ', $res);
91
        }
92
        return $this->joinClause('', ' ', $result, $settings);
93
    }
94
95
    abstract protected function &getClause(/*# string */ $clauseName)/*# : array */;
96
    abstract protected function joinClause(
97
        /*# : string */ $prefix,
98
        /*# : string */ $seperator,
99
        array $clause,
100
        array $settings
101
    )/*# : string */;
102
}
103