Passed
Push — master ( 8bcc71...8cfc58 )
by Aimeos
05:38
created

Standard::transform()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 7
c 1
b 0
f 0
nc 5
nop 3
dl 0
loc 16
rs 9.2222
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019
6
 * @package MW
7
 * @subpackage View
8
 */
9
10
11
namespace Aimeos\MW\View\Helper\Map;
12
13
14
/**
15
 * View helper class for mapping arrays/objects
16
 *
17
 * @package MW
18
 * @subpackage View
19
 */
20
class Standard implements Iface
21
{
22
	/**
23
	 * Returns the mapped array
24
	 *
25
	 * @param iterable $cfgkey List of arrays of object that should be mapped
26
	 * @param array $key Name of the property whose value should be the key of the mapped pairs
27
	 * @param string $prop Property name that should be mapped to the key
28
	 * @return \Aimeos\MW\MapIface Associative list of key/value pairs
29
	 */
30
	public function transform( iterable $list, string $key, string $prop ) : \Aimeos\MW\MapIface
31
	{
32
		$result = [];
33
34
		foreach( $list as $entry )
35
		{
36
			if( is_object( $entry ) && method_exists( $entry, 'toArray' ) ) {
37
				$entry = $entry->toArray();
38
			}
39
40
			if( array_key_exists( $key, $entry ) && array_key_exists( $prop, $entry ) ) {
41
				$result[$entry[$key]] = $entry[$prop];
42
			}
43
		}
44
45
		return \Aimeos\MW\Map::from( $result );
46
	}
47
}
48