Passed
Push — master ( 74a67e...bf1276 )
by Atanas
01:58
created

Url::addLeadingSlash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace WPEmerge\Helpers;
4
5
use WPEmerge\Requests\Request;
6
7
/**
8
 * A collection of tools dealing with urls
9
 */
10
class Url {
11
	/**
12
	 * Get the path for the request relative to the home url
13
	 *
14
	 * @return string
15
	 */
16 3
	public static function getPath( Request $request ) {
17 3
		$url = $request->getUrl();
18 3
		$relative_url = substr( $url, strlen( home_url( '/' ) ) );
19 3
		$relative_url = static::addLeadingSlash( $relative_url );
20 3
		$relative_url = preg_replace( '~\?.*~', '', $relative_url );
21 3
		$relative_url = static::addTrailingSlash( $relative_url );
22 3
		return $relative_url;
23
	}
24
25
	/**
26
	 * Ensure url has a leading slash
27
	 *
28
	 * @param  string $url
29
	 * @return string
30
	 */
31 4
	public static function addLeadingSlash( $url ) {
32 4
		return '/' . static::removeLeadingSlash( $url );
33
	}
34
35
	/**
36
	 * Ensure url does not have a leading slash
37
	 *
38
	 * @param  string $url
39
	 * @return string
40
	 */
41 5
	public static function removeLeadingSlash( $url ) {
42 5
		return preg_replace( '/^\/+/', '', $url );
43
	}
44
45
	/**
46
	 * Ensure url has a trailing slash
47
	 *
48
	 * @param  string $url
49
	 * @return string
50
	 */
51 4
	public static function addTrailingSlash( $url ) {
52 4
		return trailingslashit( $url );
53
	}
54
55
	/**
56
	 * Ensure url does not have a trailing slash
57
	 *
58
	 * @param  string $url
59
	 * @return string
60
	 */
61 1
	public static function removeTrailingSlash( $url ) {
62 1
		return untrailingslashit( $url );
63
	}
64
}
65