Completed
Push — master ( da5ca0...e55bc1 )
by Atanas
02:05
created

Url   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 54
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addTrailingSlash() 0 2 1
A removeLeadingSlash() 0 2 1
A addLeadingSlash() 0 2 1
A removeTrailingSlash() 0 2 1
A getPath() 0 7 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
	 * @param  Request $request
15
	 * @return string
16
	 */
17 3
	public static function getPath( Request $request ) {
18 3
		$url = $request->getUrl();
19 3
		$relative_url = substr( $url, strlen( home_url( '/' ) ) );
20 3
		$relative_url = static::addLeadingSlash( $relative_url );
21 3
		$relative_url = preg_replace( '~\?.*~', '', $relative_url );
22 3
		$relative_url = static::addTrailingSlash( $relative_url );
23 3
		return $relative_url;
24
	}
25
26
	/**
27
	 * Ensure url has a leading slash
28
	 *
29
	 * @param  string $url
30
	 * @return string
31
	 */
32 1
	public static function addLeadingSlash( $url ) {
33 1
		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 1
	public static function removeLeadingSlash( $url ) {
43 1
		return preg_replace( '/^\/+/', '', $url );
44
	}
45
46
	/**
47
	 * Ensure url has a trailing slash
48
	 *
49
	 * @param  string $url
50
	 * @return string
51
	 */
52 1
	public static function addTrailingSlash( $url ) {
53 1
		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 1
	public static function removeTrailingSlash( $url ) {
63 1
		return untrailingslashit( $url );
64
	}
65
}
66