Passed
Push — master ( c89772...da5ca0 )
by Atanas
01:54
created

Url::getPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
	 * @param  Request $request
15
	 * @return string
16
	 */
17
	public static function getPath( Request $request ) {
18
		$url = $request->getUrl();
19
		$relative_url = substr( $url, strlen( home_url( '/' ) ) );
20
		$relative_url = static::addLeadingSlash( $relative_url );
21
		$relative_url = preg_replace( '~\?.*~', '', $relative_url );
22
		$relative_url = static::addTrailingSlash( $relative_url );
23
		return $relative_url;
24
	}
25
26
	/**
27
	 * Ensure url has a leading slash
28
	 *
29
	 * @param  string $url
30
	 * @return string
31
	 */
32
	public static function addLeadingSlash( $url ) {
33
		return '/' . static::removeLeadingSlash( $url );
34
	}
35
36
	/**
37
	 * Ensure url does not have a leading slash
38
	 *
39
	 * @param  string $url
40
	 * @return string
41
	 */
42
	public static function removeLeadingSlash( $url ) {
43
		return preg_replace( '/^\/+/', '', $url );
44
	}
45
46
	/**
47
	 * Ensure url has a trailing slash
48
	 *
49
	 * @param  string $url
50
	 * @return string
51
	 */
52
	public static function addTrailingSlash( $url ) {
53
		return trailingslashit( $url );
54
	}
55
56
	/**
57
	 * Ensure url does not have a trailing slash
58
	 *
59
	 * @param  string $url
60
	 * @return string
61
	 */
62
	public static function removeTrailingSlash( $url ) {
63
		return untrailingslashit( $url );
64
	}
65
}
66