Skip to content

Commit

Permalink
Use common trait for items and managers with properties
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Jun 16, 2018
1 parent 7f402b9 commit a6068ba
Show file tree
Hide file tree
Showing 20 changed files with 393 additions and 225 deletions.
30 changes: 4 additions & 26 deletions lib/mshoplib/src/MShop/Attribute/Item/Standard.php
Expand Up @@ -22,8 +22,10 @@ class Standard
extends \Aimeos\MShop\Common\Item\ListRef\Base
implements \Aimeos\MShop\Attribute\Item\Iface
{
use \Aimeos\MShop\Common\Item\PropertyRef\Traits;


private $values;
private $propItems;


/**
Expand All @@ -39,35 +41,11 @@ public function __construct( array $values = [], array $listItems = [],
{
parent::__construct( 'attribute.', $values, $listItems, $refItems );

$this->propItems = $propItems;
$this->setPropertyItems( $propItems );
$this->values = $values;
}


/**
* Returns the property items of the attribute
*
* @param string|null $type Name of the property item type or null for all
* @param boolean $active True to return only active items, false to return all
* @return \Aimeos\MShop\Product\Item\Property\Iface[] Associative list of property IDs as keys and property items as values
*/
public function getPropertyItems( $type = null, $active = true )
{
$list = [];

foreach( $this->propItems as $propId => $propItem )
{
if( ( $type === null || $propItem->getType() === $type )
&& ( $active === false || $propItem->isAvailable() )
) {
$list[$propId] = $propItem;
}
}

return $list;
}


/**
* Returns the domain of the attribute item.
*
Expand Down
39 changes: 8 additions & 31 deletions lib/mshoplib/src/MShop/Attribute/Manager/Standard.php
Expand Up @@ -21,6 +21,9 @@ class Standard
extends \Aimeos\MShop\Common\Manager\ListRef\Base
implements \Aimeos\MShop\Attribute\Manager\Iface
{
use \Aimeos\MShop\Common\Manager\PropertyRef\Traits;


private $searchConfig = array(
'attribute.id' => array(
'code' => 'attribute.id',
Expand Down Expand Up @@ -246,7 +249,9 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
throw new \Aimeos\MShop\Attribute\Exception( sprintf( 'Object is not of required type "%1$s"', $iface ) );
}

if( !$item->isModified() ) {
if( !$item->isModified() )
{
$item = $this->savePropertyItems( $item, 'attribute' );
return $this->saveRefItems( $item, 'attribute' );
}

Expand Down Expand Up @@ -408,6 +413,7 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
throw $e;
}

$item = $this->savePropertyItems( $item, 'attribute' );
return $this->saveRefItems( $item, 'attribute' );
}

Expand Down Expand Up @@ -653,7 +659,7 @@ public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = [],
}
}

$propItems = $this->getPropertyItems( array_keys( $map ) );
$propItems = $this->getPropertyItems( array_keys( $map ), 'attribute' );

return $this->buildItems( $map, null, 'attribute', $propItems );
}
Expand Down Expand Up @@ -702,33 +708,4 @@ protected function createItemBase( array $values = [], array $listItems = [],
{
return new \Aimeos\MShop\Attribute\Item\Standard( $values, $listItems, $refItems, $propertyItems );
}


/**
* Returns the property items for the given attribute IDs
*
* @param array $attrIds List of attribute IDs
* @return array Associative list of attribute IDs / property IDs as keys and items implementing
* \Aimeos\MShop\Product\Item\Property\Iface as values
*/
protected function getPropertyItems( array $attrIds )
{
$list = [];

if( !empty( $attrIds ) )
{
$propManager = $this->getObject()->getSubManager( 'property' );

$propSearch = $propManager->createSearch();
$propSearch->setConditions( $propSearch->compare( '==', 'attribute.property.parentid', $attrIds ) );
$propSearch->setSortations( [$propSearch->sort( '+', 'attribute.property.type.position' )] );
$propSearch->setSlice( 0, 0x7fffffff );

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

return $list;
}
}
3 changes: 2 additions & 1 deletion lib/mshoplib/src/MShop/Catalog/Manager/Base.php
Expand Up @@ -68,9 +68,10 @@ public function registerItemFilter( $name, \Closure $fcn )
* @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
* @param array $local2 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, $domains, $prefix, array $local = [] )
protected function buildItems( array $itemMap, $domains, $prefix, array $local = [], array $local2 = [] )
{
$items = $listItemMap = $refItemMap = $refIdMap = [];

Expand Down
33 changes: 33 additions & 0 deletions lib/mshoplib/src/MShop/Common/Item/PropertyRef/Iface.php
Expand Up @@ -19,6 +19,39 @@
*/
interface Iface
{
/**
* Adds a new property item or overwrite an existing one
*
* @param \Aimeos\MShop\Product\Item\Property\Iface $item New or existing property item
* @return \Aimeos\MShop\Common\Item\Iface Self object for method chaining
*/
public function addPropertyItem( \Aimeos\MShop\Product\Item\Property\Iface $item );

/**
* Removes an existing property item
*
* @param \Aimeos\MShop\Product\Item\Property\Iface $item Existing property item
* @return \Aimeos\MShop\Common\Item\Iface Self object for method chaining
* @throws \Aimeos\MShop\Exception If given property item isn't found
*/
public function deletePropertyItem( \Aimeos\MShop\Product\Item\Property\Iface $item );

/**
* Removes a list of existing property items
*
* @param \Aimeos\MShop\Common\Item\Property\Iface[] $items Existing property items
* @return \Aimeos\MShop\Common\Item\Iface Self object for method chaining
* @throws \Aimeos\MShop\Exception If a property item isn't found
*/
public function deletePropertyItems( array $items );

/**
* Returns the deleted property items
*
* @return \Aimeos\MShop\Common\Item\Property\Iface[] Property items
*/
public function getPropertyItemsDeleted();

/**
* Returns the property items of the product
*
Expand Down
132 changes: 132 additions & 0 deletions lib/mshoplib/src/MShop/Common/Item/PropertyRef/Traits.php
@@ -0,0 +1,132 @@
<?php

/**
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
* @copyright Aimeos (aimeos.org), 2018
* @package MShop
* @subpackage Common
*/


namespace Aimeos\MShop\Common\Item\PropertyRef;


/**
* Common trait for items containing property items
*
* @package MShop
* @subpackage Common
*/
trait Traits
{
private $propItems = [];
private $propRmItems = [];
private $propMax = 0;


/**
* Adds a new property item or overwrite an existing one
*
* @param \Aimeos\MShop\Product\Item\Property\Iface $item New or existing property item
* @return \Aimeos\MShop\Common\Item\Iface Self object for method chaining
*/
public function addPropertyItem( \Aimeos\MShop\Product\Item\Property\Iface $item )
{
$id = $item->getId() ?: 'id-' . $this->propMax++;
$this->propItems[$id] = $item;

return $this;
}


/**
* Removes an existing property item
*
* @param \Aimeos\MShop\Product\Item\Property\Iface $item Existing property item
* @return \Aimeos\MShop\Common\Item\Iface Self object for method chaining
* @throws \Aimeos\MShop\Exception If given property item isn't found
*/
public function deletePropertyItem( \Aimeos\MShop\Product\Item\Property\Iface $item )
{
foreach( $this->propItems as $key => $pitem )
{
if( $pitem === $item )
{
$this->propRmItems[$item->getId()] = $item;
unset( $this->propItems[$key] );

return $this;
}
}

throw new \Aimeos\MShop\Exception( sprintf( 'Property item for removal not found' ) );
}


/**
* Removes a list of existing property items
*
* @param \Aimeos\MShop\Common\Item\Property\Iface[] $items Existing property items
* @return \Aimeos\MShop\Common\Item\Iface Self object for method chaining
* @throws \Aimeos\MShop\Exception If a property item isn't found
*/
public function deletePropertyItems( array $items )
{
foreach( $items as $item )
{
if( !( $item instanceof \Aimeos\MShop\Common\Item\Property\Iface ) ) {
throw new \Aimeos\MShop\Exception( sprintf( 'Not a property item' ) );
}

$this->deletePropertyItem( $item );
}

return $this;
}


/**
* Returns the deleted property items
*
* @return \Aimeos\MShop\Common\Item\Property\Iface[] Property items
*/
public function getPropertyItemsDeleted()
{
return $this->propRmItems;
}


/**
* Returns the property items of the product
*
* @param string|null $type Name of the property item type or null for all
* @param boolean $active True to return only active items, false to return all
* @return \Aimeos\MShop\Product\Item\Property\Iface[] Associative list of property IDs as keys and property items as values
*/
public function getPropertyItems( $type = null, $active = true )
{
$list = [];

foreach( $this->propItems as $propId => $propItem )
{
if( ( $type === null || $propItem->getType() === $type )
&& ( $active === false || $propItem->isAvailable() )
) {
$list[$propId] = $propItem;
}
}

return $list;
}


/**
* Sets the property items in the trait
*
* @param \Aimeos\MShop\Common\Item\Property\Iface[] $items Property items
*/
protected function setPropertyItems( array $items )
{
$this->propItems = $items;
}
}
10 changes: 8 additions & 2 deletions lib/mshoplib/src/MShop/Common/Manager/ListRef/Base.php
Expand Up @@ -101,9 +101,10 @@ abstract protected function createItemBase( array $values = [], array $listItems
* @param array|null $domains List of domains to fetch list items and referenced items for or null for all
* @param string $prefix Domain prefix
* @param array $local Associative list of IDs as keys and the associative array of items as values
* @param array $local2 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, $domains, $prefix, array $local = [] )
protected function buildItems( array $map, $domains, $prefix, array $local = [], array $local2 = [] )
{
$items = $listItemMap = $refItemMap = $refIdMap = [];

Expand Down Expand Up @@ -140,7 +141,12 @@ protected function buildItems( array $map, $domains, $prefix, array $local = []
$localItems = $local[$id];
}

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

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

return $items;
Expand Down

0 comments on commit a6068ba

Please sign in to comment.