1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\Ee\Observers\EeProductAttributeObserverTrait |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/techdivision/import-product-ee |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\Ee\Observers; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\StoreViewCodes; |
24
|
|
|
use TechDivision\Import\Product\Ee\Utils\MemberNames; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Trait that provides basic product attribute functionality. |
28
|
|
|
* |
29
|
|
|
* @author Tim Wagner <[email protected]> |
30
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
31
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
32
|
|
|
* @link https://github.com/techdivision/import-product-ee |
33
|
|
|
* @link http://www.techdivision.com |
34
|
|
|
*/ |
35
|
|
|
trait EeProductAttributeObserverTrait |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
40
|
|
|
* |
41
|
|
|
* @return array The prepared attributes |
42
|
|
|
*/ |
43
|
|
|
public function prepareAttributes() |
44
|
|
|
{ |
45
|
|
|
|
46
|
|
|
// load the attribute value |
47
|
|
|
$attributeValue = $this->getAttributeValue(); |
|
|
|
|
48
|
|
|
|
49
|
|
|
// laod the callbacks for the actual attribute code |
50
|
|
|
$callbacks = $this->getCallbacksByType($this->getAttributeCode()); |
|
|
|
|
51
|
|
|
|
52
|
|
|
// invoke the pre-cast callbacks |
53
|
|
|
foreach ($callbacks as $callback) { |
54
|
|
|
$attributeValue = $callback->handle($attributeValue); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// load the ID of the product that has been created recently |
58
|
|
|
$lastEntityId = $this->getPrimaryKey(); |
59
|
|
|
|
60
|
|
|
// load the ID of the attribute to create the values for |
61
|
|
|
$attributeId = $this->getAttributeId(); |
|
|
|
|
62
|
|
|
|
63
|
|
|
// load the store ID |
64
|
|
|
$storeId = $this->getRowStoreId(StoreViewCodes::ADMIN); |
|
|
|
|
65
|
|
|
|
66
|
|
|
// load the backend type of the actual attribute |
67
|
|
|
$backendType = $this->getBackendType(); |
|
|
|
|
68
|
|
|
|
69
|
|
|
// cast the value based on the backend type |
70
|
|
|
$castedValue = $this->castValueByBackendType($backendType, $attributeValue); |
|
|
|
|
71
|
|
|
|
72
|
|
|
// prepare the attribute values |
73
|
|
|
return $this->initializeEntity( |
|
|
|
|
74
|
|
|
array( |
75
|
|
|
MemberNames::ROW_ID => $lastEntityId, |
76
|
|
|
MemberNames::ATTRIBUTE_ID => $attributeId, |
77
|
|
|
MemberNames::STORE_ID => $storeId, |
78
|
|
|
MemberNames::VALUE => $castedValue |
79
|
|
|
) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Return's the PK to create the product => attribute relation. |
85
|
|
|
* |
86
|
|
|
* @return integer The PK to create the relation with |
87
|
|
|
*/ |
88
|
|
|
public function getPrimaryKey() |
89
|
|
|
{ |
90
|
|
|
return $this->getLastRowId(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return's the row ID of the product that has been created recently. |
95
|
|
|
* |
96
|
|
|
* @return string The row Id |
97
|
|
|
*/ |
98
|
|
|
public function getLastRowId() |
99
|
|
|
{ |
100
|
|
|
return $this->getSubject()->getLastRowId(); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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.