-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVariantGroupDataProvider.php
121 lines (104 loc) · 3.69 KB
/
VariantGroupDataProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace Pim\Bundle\DataGeneratorBundle;
use Doctrine\Common\Collections\ArrayCollection;
use Pim\Component\Catalog\Model\AttributeInterface;
use Pim\Component\Catalog\Model\AttributeOptionInterface;
use Pim\Component\Catalog\Model\GroupInterface;
use Symfony\Component\Config\Definition\Exception\Exception;
/**
* The VariantGroupDataProvider provides data for the product generator.
* It provides a different values combination for every product belonging to a variant group.
*
* @author Pierre Allard <[email protected]>
* @copyright 2016 Akeneo SAS (http://www.akeneo.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class VariantGroupDataProvider
{
/** @var GroupInterface */
protected $variantGroup;
/** @var int */
protected $remainingCount;
/** @var array */
protected $attributeValues;
/**
* @param GroupInterface $variantGroup
* @param int $remainingCount
*
* @throws Exception
*/
public function __construct(GroupInterface $variantGroup, $remainingCount)
{
$this->variantGroup = $variantGroup;
$this->remainingCount = $remainingCount;
$this->attributeValues = [];
$availableCombinations = 1;
/** @var AttributeInterface $attribute */
foreach ($variantGroup->getAxisAttributes() as $attribute) {
$attributeCode = $attribute->getCode();
$this->attributeValues[$attributeCode] = [];
/** @var AttributeOptionInterface $option */
foreach ($attribute->getOptions() as $option) {
$this->attributeValues[$attributeCode][] = $option->getCode();
}
$availableCombinations *= count($this->attributeValues[$attributeCode]);
}
if ($availableCombinations < $remainingCount) {
throw new Exception(sprintf(
'Variant group %s have only %s value combinations, %s are needed.',
$variantGroup->getLabel(),
$availableCombinations,
$remainingCount
));
}
}
/**
* Returns the data for every attribute from the current index.
* For example, with 3 attributes, each having 2 attribute values (0 and 1), it returns:
*
* +-------+---------+---------+---------+
* | index | index_1 | index_2 | index_3 |
* +-------+---------+---------+---------+
* | 0 | 0 | 0 | 0 |
* | 1 | 1 | 0 | 0 |
* | 2 | 0 | 1 | 0 |
* | 3 | 1 | 1 | 0 |
* | 4 | 0 | 0 | 1 ... |
* +-------+---------+---------+---------+
*
* @return array
*/
public function getData()
{
$attributeCodes = array_keys($this->attributeValues);
$data = [];
$multiplier = 1;
for ($i = 0; $i < count($attributeCodes); $i++) {
$attributeCode = $attributeCodes[$i];
$valuesCount = count($this->attributeValues[$attributeCode]);
$valueIndex = floor($this->remainingCount/$multiplier) % $valuesCount;
$data[$attributeCode] = $this->attributeValues[$attributeCode][$valueIndex];
$multiplier *= $valuesCount;
}
$this->remainingCount--;
return $data;
}
public function isLastUsage()
{
return ($this->remainingCount <= 1);
}
/**
* @return string
*/
public function getCode()
{
return $this->variantGroup->getCode();
}
/**
* @return ArrayCollection
*/
public function getAttributes()
{
return $this->variantGroup->getAxisAttributes();
}
}