Skip to content

Commit

Permalink
Implemented map view helper
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Oct 18, 2019
1 parent 8bcc717 commit 8cfc587
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/mwlib/src/MW/View/Helper/Map/Iface.php
@@ -0,0 +1,31 @@
<?php

/**
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
* @copyright Aimeos (aimeos.org), 2019
* @package MW
* @subpackage View
*/


namespace Aimeos\MW\View\Helper\Map;


/**
* View helper class for mapping arrays/objects
*
* @package MW
* @subpackage View
*/
interface Iface extends \Aimeos\MW\View\Helper\Iface
{
/**
* Returns the mapped array
*
* @param iterable $cfgkey List of arrays of object that should be mapped
* @param string $key Name of the property whose value should be the key of the mapped pairs
* @param string $prop Property name or names that should be mapped to the key
* @return \Aimeos\MW\MapIface Associative list of key/value pairs
*/
public function transform( iterable $list, string $key, string $prop ) : \Aimeos\MW\MapIface;
}
47 changes: 47 additions & 0 deletions lib/mwlib/src/MW/View/Helper/Map/Standard.php
@@ -0,0 +1,47 @@
<?php

/**
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
* @copyright Aimeos (aimeos.org), 2019
* @package MW
* @subpackage View
*/


namespace Aimeos\MW\View\Helper\Map;


/**
* View helper class for mapping arrays/objects
*
* @package MW
* @subpackage View
*/
class Standard implements Iface
{
/**
* Returns the mapped array
*
* @param iterable $cfgkey List of arrays of object that should be mapped
* @param array $key Name of the property whose value should be the key of the mapped pairs
* @param string $prop Property name that should be mapped to the key
* @return \Aimeos\MW\MapIface Associative list of key/value pairs
*/
public function transform( iterable $list, string $key, string $prop ) : \Aimeos\MW\MapIface
{
$result = [];

foreach( $list as $entry )
{
if( is_object( $entry ) && method_exists( $entry, 'toArray' ) ) {
$entry = $entry->toArray();
}

if( array_key_exists( $key, $entry ) && array_key_exists( $prop, $entry ) ) {
$result[$entry[$key]] = $entry[$prop];
}
}

return \Aimeos\MW\Map::from( $result );
}
}
41 changes: 41 additions & 0 deletions lib/mwlib/tests/MW/View/Helper/Map/StandardTest.php
@@ -0,0 +1,41 @@
<?php

/**
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
* @copyright Aimeos (aimeos.org), 2019
*/


namespace Aimeos\MW\View\Helper\Map;


class StandardTest extends \PHPUnit\Framework\TestCase
{
private $object;


protected function setUp()
{
$this->object = new \Aimeos\MW\View\Helper\Map\Standard( new \Aimeos\MW\View\Standard() );
}


protected function tearDown()
{
unset( $this->object );
}


public function testTransform()
{
$list = [['test1' => 'value1', 'test2' => 'value2']];
$this->assertEquals( ['value1' => 'value2'], $this->object->transform( $list, 'test1', 'test2' )->toArray() );
}


public function testTransformMap()
{
$list = \Aimeos\MW\Map::from( [['test1' => 'value1', 'test2' => 'value2']] );
$this->assertEquals( ['value1' => 'value2'], $this->object->transform( $list, 'test1', 'test2' )->toArray() );
}
}

0 comments on commit 8cfc587

Please sign in to comment.