Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use common trait for items and managers with properties
- Loading branch information
Showing
20 changed files
with
393 additions
and
225 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
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
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
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
132 changes: 132 additions & 0 deletions
132
lib/mshoplib/src/MShop/Common/Item/PropertyRef/Traits.php
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,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; | ||
} | ||
} |
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
Oops, something went wrong.