Passed
Push — master ( 6b115b...a9306a )
by Adrian
01:37
created

QueryHelper::isValidOrderBy()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 27
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 11
nc 5
nop 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A QueryHelper::alphaNum() 0 5 1
A QueryHelper::explode() 0 11 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * Author: Adrian Dumitru
5
 * Date: 4/23/2017 12:02 AM
6
 */
7
8
namespace Qpdb\QueryBuilder\Dependencies;
9
10
11
class QueryHelper
12
{
13
14
	/**
15
	 * @param string $string
16
	 * @return mixed|string
17
	 */
18
	public static function clearMultipleSpaces( $string = '' )
19
	{
20
		$string = preg_replace( '/\s+/', ' ', $string );
21
		$string = trim( $string );
22
23
		return $string;
24
	}
25
26
	/**
27
	 * @param $val
28
	 * @return bool
29
	 */
30
	public static function isInteger( $val )
31
	{
32
		$val = trim( $val );
33
34
		return is_numeric( $val ) && floor( $val ) == $val;
0 ignored issues
show
Bug introduced by
$val of type string is incompatible with the type double expected by parameter $value of floor(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
		return is_numeric( $val ) && floor( /** @scrutinizer ignore-type */ $val ) == $val;
Loading history...
35
	}
36
37
	/**
38
	 * @param $val
39
	 * @return bool
40
	 * @i
41
	 */
42
	public static function isDecimal( $val )
43
	{
44
		$val = trim( $val );
45
46
		return is_numeric( $val ) && floor( $val ) != $val;
0 ignored issues
show
Bug introduced by
$val of type string is incompatible with the type double expected by parameter $value of floor(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
		return is_numeric( $val ) && floor( /** @scrutinizer ignore-type */ $val ) != $val;
Loading history...
47
	}
48
49
	/**
50
	 * @param $string
51
	 * @param string $delimiter
52
	 * @return array
53
	 */
54
	public static function explode( $string, $delimiter = ',' )
55
	{
56
		$brutArray = explode( $delimiter, $string );
57
		$newArray = array();
58
		foreach ( $brutArray as $value ) {
59
			$value = trim( $value );
60
			if ( '' !== $value )
61
				$newArray[] = $value;
62
		}
63
64
		return $newArray;
65
	}
66
67
	public static function alphaNum( $string )
68
	{
69
		$string = preg_replace( "/[^a-zA-Z0-9 _,]+/", "", $string );
70
71
		return self::clearMultipleSpaces( $string );
72
	}
73
74
	/**
75
	 * @param array $array
76
	 * @param string $delimiter
77
	 * @return string
78
	 */
79
	public static function implode( array $array, $delimiter = ',' )
80
	{
81
		$string = implode( $delimiter, $array );
82
		$string = trim( $string );
83
		$string = trim( $string, trim( $delimiter ) );
84
		$string = trim( $string );
85
86
		return $string;
87
	}
88
89
	/**
90
	 * @param $string
91
	 * @return mixed|string
92
	 */
93
	public static function clearQuotes( $string )
94
	{
95
		$search = array( '"', "'" );
96
		$replace = '';
97
		$string = str_replace( $search, $replace, $string );
98
99
		return self::clearMultipleSpaces( $string );
100
	}
101
102
	/**
103
	 * @param int $length
104
	 * @return string
105
	 */
106
	public static function random( $length = 5 )
107
	{
108
		$characters = 'abcdefghijklmnopqrstuvwxyz';
109
		$charactersLength = strlen( $characters );
110
		$randomString = '';
111
		for ( $i = 0; $i < $length; $i++ )
112
			$randomString .= $characters[ rand( 0, $charactersLength - 1 ) ];
113
114
		return str_shuffle( $randomString );
115
	}
116
117
	public static function limitString( $rowCount, $offset = null )
118
	{
119
		$rowCount = intval( $rowCount );
120
		if ( is_null( $offset ) )
121
			return $rowCount;
122
		$offset = intval( $offset );
123
124
		return "$offset, $rowCount";
125
	}
126
127
}