/**
  * {@inheritdoc}
  */
 public function getVariant(VariableInterface $subject)
 {
     if ($subject->getVariants()->isEmpty()) {
         return null;
     }
     return $subject->getVariants()->first();
 }
Example #2
0
 /**
  * {@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->getPermutations($optionSet);
     foreach ($permutations as $permutation) {
         $variant = $this->variantRepository->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);
     }
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function process(VariableInterface $variable, VariantInterface $variant)
 {
     if (0 < count($this->validator->validate($variant, array('sylius')))) {
         $variable->removeVariant($variant);
     } else {
         $this->eventDispatcher->dispatch('sylius.variant.pre_create', new GenericEvent($variant));
     }
 }
 function it_builds_a_form(FormBuilderInterface $builder, VariableInterface $variable, OptionInterface $option)
 {
     $variable->getOptions()->shouldBeCalled()->willReturn([$option]);
     $option->getName()->shouldBeCalled()->willReturn('option_name');
     $builder->add('option-name', 'sylius_varibale_name_option_value_choice', ['label' => 'option_name', 'option' => $option, 'property_path' => '[0]'])->shouldBeCalled();
     $builder->addModelTransformer(Argument::type(VariantToCombinationTransformer::class))->shouldBeCalled();
     $this->buildForm($builder, ['variable' => $variable]);
 }
 function it_builds_a_form(FormBuilderInterface $builder, VariableInterface $variable, OptionInterface $option)
 {
     $variable->getOptions()->shouldBeCalled()->willReturn(array($option));
     $option->getName()->shouldBeCalled()->willReturn('option_name');
     $option->getPresentation()->shouldBeCalled()->willReturn('option_presentation');
     $builder->add('option-name', 'sylius_varibale_name_option_value_choice', array('label' => 'option_presentation', 'option' => $option, 'property_path' => '[0]'))->shouldBeCalled();
     $builder->addModelTransformer(Argument::type('Sylius\\Bundle\\VariationBundle\\Form\\DataTransformer\\VariantToCombinationTransformer'))->shouldBeCalled();
     $this->buildForm($builder, array('variable' => $variable));
 }
 /**
  * @param OptionValueInterface[] $value
  *
  * @return null|VariantInterface
  */
 private function matches($value)
 {
     foreach ($this->variable->getVariants() as $variant) {
         foreach ($value as $option) {
             if (null === $option || !$variant->hasOption($option)) {
                 continue 2;
             }
         }
         return $variant;
     }
     return;
 }
 /**
  * @param OptionValueInterface[] $value
  *
  * @return null|VariantInterface
  */
 private function matches($value)
 {
     foreach ($this->variable->getVariants() as $variant) {
         $matches = true;
         foreach ($value as $option) {
             if (!$variant->hasOption($option)) {
                 $matches = false;
             }
         }
         if ($matches) {
             return $variant;
         }
     }
     return null;
 }
 /**
  * @param VariantInterface  $variant
  * @param VariableInterface $variable
  *
  * @return bool
  */
 private function matches(VariantInterface $variant, VariableInterface $variable)
 {
     foreach ($variable->getVariants() as $existingVariant) {
         if ($variant === $existingVariant) {
             continue;
         }
         $matches = true;
         foreach ($variant->getOptions() as $option) {
             if (!$existingVariant->hasOption($option)) {
                 $matches = false;
             }
         }
         if ($matches) {
             return true;
         }
     }
     return false;
 }
Example #9
0
 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);
 }
Example #10
0
 /**
  * {@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 let(VariableInterface $variable, VariantInterface $variant)
 {
     $variable->getVariants()->shouldBeCalled()->willReturn([$variant]);
     $this->beConstructedWith($variable);
 }
 function it_returns_null_if_first_variant_is_not_definied(VariableInterface $product, Collection $variants)
 {
     $product->getVariants()->willReturn($variants);
     $variants->isEmpty()->willReturn(true);
     $this->getVariant($product)->shouldReturn(null);
 }
Example #13
0
 /**
  * @param ArchetypeInterface $archetype
  * @param VariableInterface  $subject
  */
 private function createAndAssignOptions(ArchetypeInterface $archetype, VariableInterface $subject)
 {
     foreach ($archetype->getOptions() as $option) {
         $subject->addOption($option);
     }
 }
 /**
  * @param VariableInterface $variable
  */
 public function __construct(VariableInterface $variable)
 {
     parent::__construct($variable->getVariants(), 'name', [], null, 'id');
 }
    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);
    }
 function it_should_not_reverse_transform_variable_with_variants_if_options_are_missing(VariableInterface $variable, VariantInterface $variant)
 {
     $variable->getVariants()->willReturn([$variant]);
     $this->reverseTransform([null])->shouldReturn(null);
 }
 function it_should_not_reverse_transform_variable_with_variants_if_options_not_match(VariableInterface $variable, VariantInterface $variant, OptionValueInterface $optionValue)
 {
     $variable->getVariants()->willReturn(array($variant));
     $variant->hasOption($optionValue)->willReturn(false);
     $this->reverseTransform(array($optionValue))->shouldReturn(null);
 }
Example #18
0
 /**
  * Constructor.
  *
  * @param VariableInterface $variable
  */
 public function __construct(VariableInterface $variable)
 {
     parent::__construct($variable->getVariants(), 'presentation', array(), null, 'id');
 }