/** * {@inheritdoc} */ public function generate(VariableInterface $variable) { if (!$variable->hasOptions()) { throw new \InvalidArgumentException('Cannot generate variants for an object without options.'); } $optionSet = array(); $optionMap = array(); foreach ($variable->getOptions() as $k => $option) { foreach ($option->getValues() as $value) { $optionSet[$k][] = $value->getId(); $optionMap[$value->getId()] = $value; } } $permutations = $this->setBuilder->build($optionSet); foreach ($permutations as $permutation) { $variant = $this->variantFactory->createNew(); $variant->setObject($variable); $variant->setDefaults($variable->getMasterVariant()); if (is_array($permutation)) { foreach ($permutation as $id) { $variant->addOption($optionMap[$id]); } } else { $variant->addOption($optionMap[$permutation]); } $variable->addVariant($variant); $this->process($variable, $variant); } }
/** * {@inheritdoc} */ public function generate(VariableInterface $variable) { if (!$variable->hasOptions()) { throw new \InvalidArgumentException('Cannot generate variants for an object without options.'); } $optionSet = []; $optionMap = []; foreach ($variable->getOptions() as $key => $option) { foreach ($option->getValues() as $value) { $optionSet[$key][] = $value->getId(); $optionMap[$value->getId()] = $value; } } $permutations = $this->setBuilder->build($optionSet); foreach ($permutations as $permutation) { $variant = $this->createVariant($variable, $optionMap, $permutation); $variable->addVariant($variant); } }
function it_generates_variants_for_every_possible_permutation_of_an_objects_options_and_option_values(FactoryInterface $variantFactory, OptionInterface $colorOption, OptionInterface $sizeOption, OptionValue $blackColor, OptionValue $largeSize, OptionValue $mediumSize, OptionValue $redColor, OptionValue $smallSize, OptionValue $whiteColor, SetBuilderInterface $setBuilder, VariableInterface $productVariable, VariantInterface $permutationVariant) { $productVariable->hasOptions()->willReturn(true); $productVariable->getOptions()->willReturn([$colorOption, $sizeOption]); $colorOption->getValues()->willReturn([$blackColor, $whiteColor, $redColor]); $sizeOption->getValues()->willReturn([$smallSize, $mediumSize, $largeSize]); $blackColor->getId()->willReturn('black1'); $whiteColor->getId()->willReturn('white2'); $redColor->getId()->willReturn('red3'); $smallSize->getId()->willReturn('small4'); $mediumSize->getId()->willReturn('medium5'); $largeSize->getId()->willReturn('large6'); $setBuilder->build([['black1', 'white2', 'red3'], ['small4', 'medium5', 'large6']])->willReturn([['black1', 'small4'], ['black1', 'medium5'], ['black1', 'large6'], ['white2', 'small4'], ['white2', 'medium5'], ['white2', 'large6'], ['red3', 'small4'], ['red3', 'medium5'], ['red3', 'large6']]); $variantFactory->createNew()->willReturn($permutationVariant); $permutationVariant->setObject($productVariable)->shouldBeCalled(); $permutationVariant->addOption(Argument::type('Sylius\\Component\\Variation\\Model\\OptionValue'))->shouldBeCalled(); $productVariable->addVariant($permutationVariant)->shouldBeCalled(); $this->generate($productVariable); }
function it_generates_variants_for_every_possible_permutation_of_an_objects_options_and_option_values( VariableInterface $productVariable, RepositoryInterface $variantRepository, SetBuilderInterface $setBuilder, OptionInterface $colorOption, OptionInterface $sizeOption, OptionValue $blackColor, OptionValue $whiteColor, OptionValue $redColor, OptionValue $smallSize, OptionValue $mediumSize, OptionValue $largeSize, VariantInterface $masterVariant, VariantInterface $permutationVariant ) { $productVariable->hasOptions()->willReturn(true); $productVariable->getOptions()->willReturn(array($colorOption, $sizeOption)); $colorOption->getValues()->willReturn(array($blackColor, $whiteColor, $redColor)); $sizeOption->getValues()->willReturn(array($smallSize, $mediumSize, $largeSize)); $blackColor->getId()->willReturn('black1'); $whiteColor->getId()->willReturn('white2'); $redColor->getId()->willReturn('red3'); $smallSize->getId()->willReturn('small4'); $mediumSize->getId()->willReturn('medium5'); $largeSize->getId()->willReturn('large6'); $setBuilder->build(array( array('black1', 'white2', 'red3'), array('small4', 'medium5', 'large6') ))->willReturn(array( array('black1', 'small4'), array('black1', 'medium5'), array('black1', 'large6'), array('white2', 'small4'), array('white2', 'medium5'), array('white2', 'large6'), array('red3', 'small4'), array('red3', 'medium5'), array('red3', 'large6'), )); $productVariable->getMasterVariant()->willReturn($masterVariant); $variantRepository->createNew()->willReturn($permutationVariant); $permutationVariant->setObject($productVariable)->shouldBeCalled(); $permutationVariant->setDefaults($masterVariant)->shouldBeCalled(); $permutationVariant->addOption(Argument::type('Sylius\Component\Variation\Model\OptionValue'))->shouldBeCalled(); $productVariable->addVariant($permutationVariant)->shouldBeCalled(); $this->generate($productVariable); }