/** * {@inheritdoc} */ public function getVariant(ProductInterface $subject) { if ($subject->getVariants()->isEmpty()) { return null; } return $subject->getVariants()->first(); }
/** * @param ProductOptionValueInterface[] $optionValues * * @return ProductVariantInterface|null */ private function matches(array $optionValues) { foreach ($this->product->getVariants() as $variant) { foreach ($optionValues as $optionValue) { if (null === $optionValue || !$variant->hasOptionValue($optionValue)) { continue 2; } } return $variant; } return null; }
/** * {@inheritdoc} */ public function checkParity(ProductVariantInterface $variant, ProductInterface $product) { foreach ($product->getVariants() as $existingVariant) { // This check is require, because this function has to look for any other different variant with same option values set if ($variant === $existingVariant || count($variant->getOptionValues()) !== count($product->getOptions())) { continue; } if ($this->matchOptions($variant, $existingVariant)) { return true; } } return false; }
/** * @param ProductVariantInterface $variant * @param ProductInterface $variable * * @return bool */ private function matches(ProductVariantInterface $variant, ProductInterface $variable) { foreach ($variable->getVariants() as $existingVariant) { if ($variant === $existingVariant || !$variant->getOptionValues()->count()) { continue; } $matches = true; foreach ($variant->getOptionValues() as $optionValue) { if (!$existingVariant->hasOptionValue($optionValue)) { $matches = false; } } if ($matches) { return true; } } return false; }
function it_does_not_reverse_transform_variable_with_variants_if_options_are_missing(ProductInterface $variable, ProductVariantInterface $variant) { $variable->getVariants()->willReturn([$variant]); $this->reverseTransform([null])->shouldReturn(null); }
function it_returns_null_if_first_variant_is_not_defined(Collection $variants, ProductInterface $product) { $product->getVariants()->willReturn($variants); $variants->isEmpty()->willReturn(true); $this->getVariant($product)->shouldReturn(null); }