Limit::getLimitSyntax()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * Author: Adrian Dumitru
5
 * Date: 4/22/2017 11:40 PM
6
 */
7
8
namespace Qpdb\QueryBuilder\Traits;
9
10
11
use Qpdb\QueryBuilder\Dependencies\QueryException;
12
use Qpdb\QueryBuilder\Dependencies\QueryHelper;
13
use Qpdb\QueryBuilder\Dependencies\QueryStructure;
14
15
/**
16
 * Trait Limit
17
 * @package Qpdb\QueryBuilder\Traits
18
 * @property QueryStructure $queryStructure
19
 */
20
trait Limit
21
{
22
23
	/**
24
	 * @param int $limit
25
	 * @param null $offset
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $offset is correct as it would always require null to be passed?
Loading history...
26
	 * @return $this
27
	 * @throws QueryException
28
	 */
29
	public function limit( $limit, $offset = null )
30
	{
31
		$limit = trim( $limit );
32
33
		if ( !QueryHelper::isInteger( $limit ) )
34
			throw new QueryException( 'Invalid Limit value', QueryException::QUERY_ERROR_INVALID_LIMIT );
35
36
		$limit = $this->queryStructure->bindParam('lim', (int)$limit);
37
38
		if ( is_null( $offset ) ) {
0 ignored issues
show
introduced by
The condition is_null($offset) is always true.
Loading history...
39
			$this->queryStructure->setElement( QueryStructure::LIMIT, $limit );
40
41
			return $this;
42
		}
43
44
		$offset = trim( $offset );
45
46
		if ( !QueryHelper::isInteger( $offset ) )
47
			throw new QueryException( 'Invalid Limit offset', QueryException::QUERY_ERROR_INVALID_LIMIT_OFFSET );
48
49
		$offset = $this->queryStructure->bindParam('ofs', (int)$offset);
50
51
		$this->queryStructure->setElement( QueryStructure::LIMIT, $offset . ', ' . $limit );
52
53
		return $this;
54
	}
55
56
	private function getLimitSyntax()
57
	{
58
		if ( !$this->queryStructure->getElement( QueryStructure::LIMIT ) )
59
			return '';
60
61
		return 'LIMIT ' . $this->queryStructure->getElement( QueryStructure::LIMIT );
62
	}
63
64
}
65
66