Passed
Push — master ( 592389...04392b )
by
unknown
01:41
created

Url::getCurrentUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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