1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MIT License |
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace SprykerEco\Zed\Econda\Business\Collector\File; |
9
|
|
|
|
10
|
|
|
use Generated\Shared\Transfer\LocaleTransfer; |
11
|
|
|
use SprykerEco\Zed\Econda\Business\Collector\AbstractDatabaseCollector; |
12
|
|
|
|
13
|
|
|
class EcondaCategoryCollector extends AbstractDatabaseCollector |
14
|
|
|
{ |
15
|
|
|
// CSV File Columns |
16
|
|
|
const ID_COLUMN = 'ID'; |
17
|
|
|
const PARENT_COLUMN = 'ParentID'; |
18
|
|
|
const NAME_COLUMN = 'Name'; |
19
|
|
|
|
20
|
|
|
// Internal Query Fields |
21
|
|
|
const ID_CATEGORY_NODE_QUERY_FIELD = 'id_category_node'; |
22
|
|
|
const PARENTS_QUERY_FIELD = 'parents'; |
23
|
|
|
const NAME_QUERY_FIELD = 'name'; |
24
|
|
|
const CHILDREN_QUERY_FIELD = 'children'; |
25
|
|
|
const FK_PARENT_CATEGORY_NODE = 'fk_parent_category_node'; |
26
|
|
|
|
27
|
|
|
const RESOURCE_TYPE = 'categories'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array $collectedSet |
31
|
|
|
* @param \Generated\Shared\Transfer\LocaleTransfer $localeTransfer |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
protected function collectData(array $collectedSet, LocaleTransfer $localeTransfer) |
36
|
|
|
{ |
37
|
|
|
$setToExport = []; |
38
|
|
|
|
39
|
|
|
foreach ($collectedSet as $collectedItemData) { |
40
|
|
|
$collectedItemData[static::CHILDREN_QUERY_FIELD] = $this->getChildren($collectedItemData, $collectedSet); |
41
|
|
|
$collectedItemData[static::PARENTS_QUERY_FIELD] = $this->getParents($collectedItemData, $collectedSet); |
42
|
|
|
|
43
|
|
|
$setToExport[] = $this->collectItem($collectedItemData); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $setToExport; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param array $collectItemData |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
protected function collectItem(array $collectItemData) |
55
|
|
|
{ |
56
|
|
|
return $this->formatCategoryNode($collectItemData); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @SuppressWarnings(PHPMD.UnusedLocalVariable) |
61
|
|
|
* |
62
|
|
|
* @param array $node |
63
|
|
|
* @param array $data |
64
|
|
|
* @param bool $nested |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
protected function getChildren(array $node, array $data, $nested = true) |
69
|
|
|
{ |
70
|
|
|
$children = array_filter($data, function ($item) use ($node) { |
71
|
|
|
return ((int)$item[static::FK_PARENT_CATEGORY_NODE] === (int)$node[static::ID_CATEGORY_NODE_QUERY_FIELD]); |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
foreach ($children as $index => $child) { |
75
|
|
|
if ($nested) { |
76
|
|
|
$children[$index][static::CHILDREN_QUERY_FIELD] = $this->getChildren($children[$index], $data); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$children[$index] = $this->formatCategoryNode($children[$index]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $children; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
protected function collectResourceType() |
89
|
|
|
{ |
90
|
|
|
return static::RESOURCE_TYPE; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param array $collectItemData |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
protected function formatCategoryNode(array $collectItemData) |
99
|
|
|
{ |
100
|
|
|
return [ |
101
|
|
|
static::ID_COLUMN => $collectItemData[static::ID_CATEGORY_NODE_QUERY_FIELD], |
102
|
|
|
static::PARENT_COLUMN => $collectItemData[static::PARENTS_QUERY_FIELD], |
103
|
|
|
static::NAME_COLUMN => $collectItemData[static::NAME_QUERY_FIELD], |
104
|
|
|
]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param array $node |
109
|
|
|
* @param array $data |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
protected function getParents(array $node, array $data) |
114
|
|
|
{ |
115
|
|
|
$parents = array_filter($data, function ($item) use ($node) { |
116
|
|
|
return ((int)$item[static::ID_CATEGORY_NODE_QUERY_FIELD] === (int)$node[static::FK_PARENT_CATEGORY_NODE]); |
117
|
|
|
}); |
118
|
|
|
|
119
|
|
|
$result = 'ROOT'; |
120
|
|
|
foreach ($parents as $parent) { |
121
|
|
|
$result = $parent[static::ID_CATEGORY_NODE_QUERY_FIELD]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $result; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|