Passed
Push — development ( e7eb97...e615e2 )
by Spuds
01:30 queued 23s
created

Standard::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Standard representation of any URL that doesn't have a custom builder
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * @version 2.0 dev
11
 *
12
 */
13
14
namespace ElkArte\UrlGenerator\Standard;
15
16
use ElkArte\UrlGenerator\AbstractUrlGenerator;
17
18
/**
19
 * Class Standard
20
 *
21
 * @package ElkArte\UrlGenerator\Standard
22
 */
23
class Standard extends AbstractUrlGenerator
24
{
25
	/** {@inheritDoc} */
26
	protected $_types = ['standard'];
27
28
	/**
29
	 * {@inheritDoc}
30
	 */
31
	public function generate($params)
32
	{
33 39
		return $this->generateQuery($params);
34
	}
35 39
36
	/**
37
	 * {@inheritDoc}
38
	 */
39
	protected function generateQuery($params): string
40
	{
41 41
		if (!is_array($params))
42
		{
43 41
			return '';
44 41
		}
45
46 33
		$args = [];
47
		foreach ($params as $k => $v)
48 8
		{
49
			if (is_int($k))
50 2
			{
51
				if ($v === '')
52 6
				{
53
					continue;
54
				}
55
56 33
				$args[$k] = $v;
57
				continue;
58
			}
59
60 41
			// A substitution token like $1 $2{stuff}, should be left alone
61
			if (is_string($v) && $v !== '')
62
			{
63
				// A sprintf token (%1$d %2$s etc.) should be left alone, as should
64
				// a substitution token like $1 $2{stuff}
65
				if (($v[0] === '$' && preg_match('~^\$\d({.*})?$~m', $v) !== 0)
66
					|| ($v[0] === '%' && preg_match('~^%\d\$[ds]$~m', $v) !== 0))
67
				{
68
					$args[$k] = $k . '=' . $v;
69
					continue;
70
				}
71
			}
72
73
			if ($v === null)
74
			{
75
				continue;
76
			}
77
78
			$args[$k] = $k . '=' . urlencode($v);
79
		}
80
81
		$args = $this->getHash($args);
82
83
		return (!empty($args) ? $this->_separator : '') . implode($this->_separator, $args);
84
	}
85
}
86