Skip to content

Commit

Permalink
Product properties can be retrieved as part of the product items
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Feb 18, 2017
1 parent d60aa0f commit b67a387
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 9 deletions.
3 changes: 2 additions & 1 deletion lib/mshoplib/src/MShop/Catalog/Manager/Base.php
Expand Up @@ -67,9 +67,10 @@ public function registerItemFilter( $name, \Closure $fcn )
* @param array $itemMap Associative list of catalog ID / tree node pairs
* @param array $domains List of domains (e.g. text, media) whose items should be attached to the catalog items
* @param string $prefix Domain prefix
* @param array $local Associative list of IDs as keys and the associative array of items as values
* @return array List of items implementing \Aimeos\MShop\Catalog\Item\Iface
*/
protected function buildItems( array $itemMap, array $domains, $prefix )
protected function buildItems( array $itemMap, array $domains, $prefix, array $local = array() )
{
$items = $listItemMap = $refItemMap = $refIdMap = array();

Expand Down
10 changes: 8 additions & 2 deletions lib/mshoplib/src/MShop/Common/Manager/ListRef/Base.php
Expand Up @@ -97,9 +97,10 @@ abstract protected function createItemBase( array $values = array(), array $list
* @param array $map Associative list of IDs as keys and the associative array of values
* @param array $domains List of domains to fetch list items and referenced items for
* @param string $prefix Domain prefix
* @param array $local Associative list of IDs as keys and the associative array of items as values
* @return array List of items implementing \Aimeos\MShop\Common\Item\Iface
*/
protected function buildItems( array $map, array $domains, $prefix )
protected function buildItems( array $map, array $domains, $prefix, array $local = array() )
{
$items = $listItemMap = $refItemMap = $refIdMap = array();

Expand Down Expand Up @@ -131,7 +132,12 @@ protected function buildItems( array $map, array $domains, $prefix )
$refItems = $refItemMap[$id];
}

$items[$id] = $this->createItemBase( $values, $listItems, $refItems );
$localItems = array();
if( isset( $local[$id] ) ) {
$localItems = $local[$id];
}

$items[$id] = $this->createItemBase( $values, $listItems, $refItems, $localItems );
}

return $items;
Expand Down
7 changes: 7 additions & 0 deletions lib/mshoplib/src/MShop/Product/Item/Iface.php
Expand Up @@ -22,6 +22,13 @@ interface Iface
extends \Aimeos\MShop\Common\Item\Config\Iface, \Aimeos\MShop\Common\Item\ListRef\Iface,
\Aimeos\MShop\Common\Item\Time\Iface, \Aimeos\MShop\Common\Item\Typeid\Iface
{
/**
* Returns the property items of the product
*
* @return \Aimeos\MShop\Product\Item\Property\Iface[] Associative list of property IDs as keys and property items as values
*/
public function getPropertyItems( $type = null );

/**
* Returns the status of the product item.
*
Expand Down
31 changes: 30 additions & 1 deletion lib/mshoplib/src/MShop/Product/Item/Standard.php
Expand Up @@ -23,6 +23,7 @@ class Standard
implements \Aimeos\MShop\Product\Item\Iface
{
private $values;
private $propItems;


/**
Expand All @@ -31,15 +32,43 @@ class Standard
* @param array $values Parameter for initializing the basic properties
* @param \Aimeos\MShop\Common\Lists\Item\Iface[] $listItems List of list items
* @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items
* @param \Aimeos\MShop\Product\Item\Property\Iface[] $propItems List of property items
*/
public function __construct( array $values = array(), array $listItems = array(), array $refItems = array() )
public function __construct( array $values = array(), array $listItems = array(),
array $refItems = array(), array $propItems = array() )
{
parent::__construct( 'product.', $values, $listItems, $refItems );

$this->propItems = $propItems;
$this->values = $values;
}


/**
* Returns the property items of the product
*
* @return \Aimeos\MShop\Product\Item\Property\Iface[] Associative list of property IDs as keys and property items as values
*/
public function getPropertyItems( $type = null )
{
if( $type !== null )
{
$list = array();

foreach( $this->propItems as $propId => $propItem )
{
if( $propItem->getType() === $type ) {
$list[$propId] = $propItem;
}
}

return $list;
}

return $this->propItems;
}


/**
* Returns the type ID of the product item.
*
Expand Down
38 changes: 35 additions & 3 deletions lib/mshoplib/src/MShop/Product/Manager/Standard.php
Expand Up @@ -596,7 +596,13 @@ public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = arr
}
}

return $this->buildItems( $map, $ref, 'product' );
$propItems = array();
if( in_array( 'product/property', $ref, true ) ) {
$propItems = $this->getPropertyItems( array_keys( $map ) );
}


return $this->buildItems( $map, $ref, 'product', $propItems );
}


Expand Down Expand Up @@ -701,10 +707,36 @@ public function createSearch( $default = false )
* @param array $values Associative list of key/value pairs
* @param array $listitems List of items implementing \Aimeos\MShop\Common\Item\Lists\Iface
* @param array $refItems List of items implementing \Aimeos\MShop\Common\Item\Iface
* @param array $propertyItems List of items implementing \Aimeos\MShop\Product\Item\Property\Iface
* @return \Aimeos\MShop\Product\Item\Iface New product item
*/
protected function createItemBase( array $values = array(), array $listitems = array(), array $refItems = array() )
protected function createItemBase( array $values = array(), array $listitems = array(),
array $refItems = array(), array $propertyItems = array() )
{
return new \Aimeos\MShop\Product\Item\Standard( $values, $listitems, $refItems, $propertyItems );
}


/**
* Returns the property items for the given product IDs
*
* @param array $prodIds List of product IDs
* @return array Associative list of product IDs / property IDs as keys and items implementing
* \Aimeos\MShop\Product\Item\Property\Iface as values
*/
protected function getPropertyItems( array $prodIds )
{
return new \Aimeos\MShop\Product\Item\Standard( $values, $listitems, $refItems );
$list = array();
$propManager = $this->getSubManager( 'property' );

$propSearch = $propManager->createSearch();
$propSearch->setConditions( $propSearch->compare( '==', 'product.property.parentid', $prodIds ) );
$propSearch->setSlice( 0, 0x7fffffff );

foreach( $propManager->searchItems( $propSearch ) as $id => $propItem ) {
$list[$propItem->getParentId()][$id] = $propItem;
}

return $list;
}
}
39 changes: 38 additions & 1 deletion lib/mshoplib/tests/MShop/Product/Item/StandardTest.php
Expand Up @@ -35,7 +35,20 @@ protected function setUp()
'product.editor' => 'unitTestUser'
);

$this->object = new \Aimeos\MShop\Product\Item\Standard( $this->values );
$propItems = array(
2 => new \Aimeos\MShop\Product\Item\Property\Standard( array(
'product.property.id' => 2,
'product.property.parentid' => 1,
'product.property.type' => 'proptest',
) ),
3 => new \Aimeos\MShop\Product\Item\Property\Standard( array(
'product.property.id' => 3,
'product.property.parentid' => 1,
'product.property.type' => 'proptype',
) ),
);

$this->object = new \Aimeos\MShop\Product\Item\Standard( $this->values, array(), array(), $propItems );
}


Expand Down Expand Up @@ -76,6 +89,30 @@ public function testGetSiteId()
}


public function testGetPropertyItems()
{
$propItems = $this->object->getPropertyItems();

$this->assertEquals( 2, count( $propItems ) );

foreach( $propItems as $propItem ) {
$this->assertInstanceOf( '\Aimeos\MShop\Product\Item\Property\Iface', $propItem );
}
}


public function testGetPropertyItemsType()
{
$propItems = $this->object->getPropertyItems( 'proptest' );

$this->assertEquals( 1, count( $propItems ) );

foreach( $propItems as $propItem ) {
$this->assertInstanceOf( '\Aimeos\MShop\Product\Item\Property\Iface', $propItem );
}
}


public function testGetTypeId()
{
$this->assertEquals( 2, $this->object->getTypeId() );
Expand Down
3 changes: 2 additions & 1 deletion lib/mshoplib/tests/MShop/Product/Manager/StandardTest.php
Expand Up @@ -81,7 +81,7 @@ public function testFindItem()

public function testGetItem()
{
$domains = array( 'text', 'product', 'price', 'media', 'attribute' );
$domains = array( 'text', 'product', 'price', 'media', 'attribute', 'product/property' );

$search = $this->object->createSearch();
$conditions = array(
Expand All @@ -97,6 +97,7 @@ public function testGetItem()

$this->assertEquals( $product, $this->object->getItem( $product->getId(), $domains ) );
$this->assertEquals( 6, count( $product->getRefItems( 'text' ) ) );
$this->assertEquals( 4, count( $product->getPropertyItems() ) );
$this->assertNotEquals( '', $product->getTypeName() );
}

Expand Down

0 comments on commit b67a387

Please sign in to comment.