ParseQuery   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 92
ccs 0
cts 38
cp 0
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A board() 0 3 1
A standard() 0 3 1
A topic() 0 3 1
A profile() 0 3 1
A parse() 0 5 1
A process() 0 20 4
1
<?php
2
3
/**
4
 * This class takes care of converting a Semantic URL into a Standard one so that
5
 * the request parser can do its work and explode everything into an array of values.
6
 *
7
 * @package   ElkArte Forum
8
 * @copyright ElkArte Forum contributors
9
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
10
 *
11
 * @version 2.0 Beta 1
12
 *
13
 */
14
15
namespace ElkArte\UrlGenerator\Semantic;
16
17
use ElkArte\UrlGenerator\AbstractParseQuery;
18
19
/**
20
 * Class ParseQuery
21
 *
22
 * @package ElkArte\UrlGenerator\Semantic
23
 */
24
class ParseQuery extends AbstractParseQuery
25
{
26
	/**
27
	 * Public facing function that converts the query part of the URL from the
28
	 * semantic format back to the standard ElkArte one
29
	 *
30
	 * @param string $query The semantic query
31
	 * @return string $query The corresponding standard query
32
	 */
33
	public function parse($query)
34
	{
35
		$call = $this->parsers[$query[0]] ?? $this->parsers['s'];
36
37
		return $this->{$call}($query);
38
	}
39
40
	/**
41
	 * The standard way to convert it (i.e., do nothing).
42
	 * This is used when the parse method cannot identify the type of URL
43
	 * it is facing, so it assumes the URL is standard.
44
	 *
45
	 * @param string $query The semantic query
46
	 * @return string $query The corresponding standard query
47
	 */
48
	protected function standard($query): string
49
	{
50
		return $query;
51
	}
52
53
	/**
54
	 * Boards have to have a "board" parameter, and this method ensures the query
55
	 * has it.
56
	 *
57
	 * @param string $query The semantic query
58
	 * @return string $query The corresponding standard query
59
	 */
60
	protected function board($query): string
61
	{
62
		return 'board=' . $this->process($query);
63
	}
64
65
	/**
66
	 * This method splits the semantic URL into pieces (exploding at each "/")
67
	 * and puts more or less everything back together into the standard format.
68
	 * Some more processing takes care of "-" => ".".
69
	 *
70
	 * @param string $query The semantic query
71
	 * @return string $query The corresponding standard query
72
	 */
73
	protected function process($query): string
74
	{
75
		$parts = explode('/', $query);
76
		if (count($parts) === 1)
77
		{
78
			return $this->separator . $query;
79
		}
80
81
		$split_query = explode('?', $parts[isset($parts[2]) ? 2 : 1]);
82
83
		if (isset($parts[2]))
84
		{
85
			$real_query = substr($parts[1], strrpos($parts[1], '-') + 1) . '.' . substr($split_query[0], 5);
86
		}
87
		else
88
		{
89
			$real_query = substr($split_query[0], strrpos($split_query[0], '-') + 1);
90
		}
91
92
		return $real_query . ($this->separator . ($split_query[1] ?? ''));
93
	}
94
95
	/**
96
	 * Topics have to have a "topic" parameter, and this method ensures the query
97
	 * has it.
98
	 *
99
	 * @param string $query The semantic query
100
	 * @return string $query The corresponding standard query
101
	 */
102
	protected function topic($query): string
103
	{
104
		return 'topic=' . $this->process($query);
105
	}
106
107
	/**
108
	 * Profiles are an action to begin with and then have the "u" holding the user id.
109
	 *
110
	 * @param string $query The semantic query
111
	 * @return string $query The corresponding standard query
112
	 */
113
	protected function profile($query): string
114
	{
115
		return 'action=profile;u=' . $this->process($query);
116
	}
117
}
118