1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2018 |
6
|
|
|
* @package MShop |
7
|
|
|
* @subpackage Common |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\MShop\Common\Manager\AddressRef; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Common trait for managers retrieving/storing address items |
16
|
|
|
* |
17
|
|
|
* @package MShop |
18
|
|
|
* @subpackage Common |
19
|
|
|
*/ |
20
|
|
|
trait Traits |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Returns the address items for the given parent IDs |
24
|
|
|
* |
25
|
|
|
* @param array $parentIds List of parent IDs |
26
|
|
|
* @param string $domain Domain of the calling manager |
27
|
|
|
* @return array Associative list of parent IDs / address IDs as keys and items implementing |
28
|
|
|
* \Aimeos\MShop\Common\Item\Address\Iface as values |
29
|
|
|
*/ |
30
|
|
|
protected function getAddressItems( array $parentIds, $domain ) |
31
|
|
|
{ |
32
|
|
|
$list = []; |
33
|
|
|
|
34
|
|
|
if( !empty( $parentIds ) ) |
35
|
|
|
{ |
36
|
|
|
$manager = $this->getObject()->getSubManager( 'address' ); |
|
|
|
|
37
|
|
|
|
38
|
|
|
$search = $manager->createSearch(); |
39
|
|
|
$search->setConditions( $search->compare( '==', $domain . '.address.parentid', $parentIds ) ); |
40
|
|
|
$search->setSlice( 0, 0x7fffffff ); |
41
|
|
|
|
42
|
|
|
foreach( $manager->searchItems( $search ) as $id => $addrItem ) { |
43
|
|
|
$list[$addrItem->getParentId()][$id] = $addrItem; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $list; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Adds new, updates existing and deletes removed address items |
53
|
|
|
* |
54
|
|
|
* @param \Aimeos\MShop\Common\Item\AddressRef\Iface $item Item with referenced items |
55
|
|
|
* @param string $domain Domain of the calling manager |
56
|
|
|
* @return \Aimeos\MShop\Common\Item\AddressRef\Iface Item with saved referenced items |
57
|
|
|
*/ |
58
|
|
|
protected function saveAddressItems( \Aimeos\MShop\Common\Item\AddressRef\Iface $item, $domain ) |
59
|
|
|
{ |
60
|
|
|
$manager = $this->getObject()->getSubManager( 'address' ); |
|
|
|
|
61
|
|
|
$manager->deleteItems( array_keys( $item->getAddressItemsDeleted() ) ); |
62
|
|
|
|
63
|
|
|
foreach( $item->getAddressItems() as $addrItem ) |
64
|
|
|
{ |
65
|
|
|
if( $addrItem->getParentId() != $item->getId() ) { |
66
|
|
|
$addrItem->setId( null ); //create new address item if copied |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$addrItem->setParentId( $item->getId() ); |
70
|
|
|
$manager->saveItem( $addrItem ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $item; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.