QueryHelper::addBacktick()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 14
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
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
		//$string = preg_replace( "/[^a-zA-Z0-9_]+/", "", $string );
72
73
		return self::clearMultipleSpaces( $string );
74
	}
75
76
	/**
77
	 * @param array $array
78
	 * @param string $delimiter
79
	 * @return string
80
	 */
81
	public static function implode( array $array, $delimiter = ',' )
82
	{
83
		$string = implode( $delimiter, $array );
84
		$string = trim( $string );
85
		$string = trim( $string, trim( $delimiter ) );
86
		$string = trim( $string );
87
88
		return $string;
89
	}
90
91
	/**
92
	 * @param $string
93
	 * @return mixed|string
94
	 */
95
	public static function clearQuotes( $string )
96
	{
97
		$search = array( '"', "'" );
98
		$replace = '';
99
		$string = str_replace( $search, $replace, $string );
100
101
		return self::clearMultipleSpaces( $string );
102
	}
103
104
	/**
105
	 * @param int $length
106
	 * @return string
107
	 */
108
	public static function random( $length = 5 )
109
	{
110
		$characters = 'abcdefghijklmnopqrstuvwxyz';
111
		$charactersLength = strlen( $characters );
112
		$randomString = '';
113
		for ( $i = 0; $i < $length; $i++ )
114
			$randomString .= $characters[ rand( 0, $charactersLength - 1 ) ];
115
116
		return str_shuffle( $randomString );
117
	}
118
119
120
	public static function addBacktick( $string )
121
	{
122
		$string = str_replace( '`', '', $string );
123
		$stringArrayBacktick = [];
124
		$string = self::clearMultipleSpaces( $string );
125
		$stringArray = explode( '.', $string );
126
		foreach ( $stringArray as $part ) {
127
			$part = self::clearMultipleSpaces( $part );
128
			if ( empty( $part ) )
129
				continue;
130
			$stringArrayBacktick[] = '`' . $part . '`';
131
		}
132
133
		return implode( '.', $stringArrayBacktick );
134
	}
135
136
}