-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() ); | ||
} | ||
} |