OffsetBuilder   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 17
c 1
b 0
f 0
dl 0
loc 40
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getOffset() 0 5 2
A offset() 0 3 1
A buildOffset() 0 14 5
1
<?php
2
namespace Kir\MySQL\Builder\Traits;
3
4
use Kir\MySQL\Builder\InvalidValueException;
5
use Kir\MySQL\Builder\Value\OptionalValue;
6
7
trait OffsetBuilder {
8
	private null|int|OptionalValue $offset = null;
9
10
	/**
11
	 * @return null|int
12
	 */
13
	protected function getOffset(): ?int {
14
		if($this->offset instanceof OptionalValue) {
15
			return $this->offset->getValue();
16
		}
17
		return $this->offset;
18
	}
19
20
	/**
21
	 * @param null|int|OptionalValue $offset
22
	 * @return $this
23
	 */
24
	public function offset($offset = 0) {
25
		$this->offset = $offset;
26
		return $this;
27
	}
28
29
	/**
30
	 * @param string $query
31
	 * @return string
32
	 */
33
	protected function buildOffset(string $query): string {
34
		$offset = $this->getOffset();
35
		if($this->offset instanceof OptionalValue) {
36
			if($this->offset->isValid()) {
37
				$value = $this->offset->getValue();
38
				if(!preg_match('{\\d+}', $value)) {
39
					throw new InvalidValueException('Value for OFFSET has to be a number');
40
				}
41
				$query .= "OFFSET\n\t{$this->offset->getValue()}\n";
42
			}
43
		} elseif($offset !== null) {
44
			$query .= "OFFSET\n\t{$this->offset}\n";
45
		}
46
		return $query;
47
	}
48
}
49