Standard   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 1
b 0
f 0
dl 0
loc 43
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 13 4
A __construct() 0 5 1
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 * @package Base
7
 * @subpackage View
8
 */
9
10
11
namespace Aimeos\Base\View\Helper\Formparam;
12
13
14
/**
15
 * View helper class for generating form parameter names.
16
 *
17
 * @package Base
18
 * @subpackage View
19
 */
20
class Standard
21
	extends \Aimeos\Base\View\Helper\Base
22
	implements \Aimeos\Base\View\Helper\Formparam\Iface
23
{
24
	private array $names;
25
26
27
	/**
28
	 * Initializes the URL view helper.
29
	 *
30
	 * @param \Aimeos\Base\View\Iface $view View instance with registered view helpers
31
	 * @param string[] $names Prefix names when generating form parameters (will be "name1[name2][name3]..." )
32
	 */
33
	public function __construct( \Aimeos\Base\View\Iface $view, array $names = [] )
34
	{
35
		parent::__construct( $view );
36
37
		$this->names = $names;
38
	}
39
40
41
	/**
42
	 * Returns the name of the form parameter.
43
	 * The result is a string that allows parameters to be passed as arrays if
44
	 * this is necessary, e.g. "name1[name2][name3]..."
45
	 *
46
	 * @param string|array $names Name or list of names
47
	 * @param bool $prefix TRUE to use available prefix, FALSE for names without prefix
48
	 * @return string Form parameter name
49
	 */
50
	public function transform( $names, bool $prefix = true ) : string
51
	{
52
		$names = array_merge( $prefix ? $this->names : [], (array) $names );
53
54
		if( ( $result = array_shift( $names ) ) === null ) {
55
			return '';
56
		}
57
58
		foreach( $names as $name ) {
59
			$result .= '[' . $name . ']';
60
		}
61
62
		return $result;
63
	}
64
}
65