<commit_before><?php

namespace Furniture\ProductBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\PersistentCollection;
use Furniture\ProductBundle\Entity\Pattern\AbstractProductVariantsPattern;
use Furniture\SkuOptionBundle\Entity\SkuOptionVariant;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
 * Class ProductVariantsPattern
 * @package Furniture\ProductBundle\Entity
 * @Assert\Callback(callback="validate")
 */
class ProductVariantsPattern extends AbstractProductVariantsPattern
{
    /**
     * @var int
     *
     * @Assert\NotBlank()
     * @Assert\Range(min = 0.01)
     */
    private $price;

    /**
     * @var string
     */
    protected $factoryCode;
    
    /**
     * Set price
     *
     * @param int $price
     *
     * @return ProductVariantsPattern
     */
    public function setPrice($price)
    {
        $this->price = $price;

        return $this;
    }

    /**
     * Get price
     *
     * @return int
     */
    public function getPrice()
    {
        return $this->price;
    }
    
    /**
     * Get factory code
     *
     * @return string
     */
    public function getFactoryCode()
    {
        return $this->factoryCode;
    }

    /**
     * Set factory code
     *
     * @param string $code
     *
     * @return ProductVariantsPattern
     */
    public function setFactoryCode($code)
    {
        $this->factoryCode = $code;

        return $this;
    }
    
    /**
     * Get avtive active product code for this pattern
     * 
     * @return type
     */
    public function getActiveFactoryCode(){
        return $this->getFactoryCode() ? $this->getFactoryCode() : $this->getProduct()->getFactoryCode();
    }

    /**
     * Callback validation method.
     */
    public function validate(ExecutionContextInterface $context)
    {
        // Validate Product parts
        $scheme = $this->getScheme();
        if ($scheme && $scheme->getProductParts()->count()) {
            $selectionArray = [];
            /** @var PersistentCollection $variantSelections */
            $variantSelections = $this->getPartPatternVariantSelections();
            /** @var PersistentCollection $schemeParts */
            $schemeParts = $this->getScheme()->getProductParts();
            /** @var ProductPartPatternVariantSelection $selection */
            foreach ($variantSelections as $selection) {
                if ($schemeParts->contains($selection->getProductPart())) {
                    $selectionArray[$selection->getProductPart()->getId()] = true;
                }
            }

            if ($scheme->getProductParts()->count() > count($selectionArray)) {
                $context->buildViolation('You should select at least one variant for each parts')
                    ->addViolation();
            }
        }

        // Validate SKU options.
        $productSkuOptions = $this->getProduct()->getSkuOptionVariantsGrouped();
        if (!empty($productSkuOptions)) {
            $selectionArray = [];
            /** @var ArrayCollection $optionSelections */
            $optionSelections = $this->getSkuOptionValues();
            /** @var SkuOptionVariant $selection */
            foreach ($optionSelections as $selection) {
                if (array_key_exists($selection->getSkuOptionType()->getId(), $productSkuOptions)) {
                    $selectionArray[$selection->getSkuOptionType()->getId()] = true;
                }
            }

            if (count($productSkuOptions) > count($selectionArray)) {
                $context->buildViolation('You should select at least one SKU option for each groups')
                    ->addViolation();
            }
        }
    }
}
<commit_msg>#2391 product patterns fix<commit_after><?php

namespace Furniture\ProductBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\PersistentCollection;
use Furniture\ProductBundle\Entity\Pattern\AbstractProductVariantsPattern;
use Furniture\SkuOptionBundle\Entity\SkuOptionVariant;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
 * Class ProductVariantsPattern
 * @package Furniture\ProductBundle\Entity
 * @Assert\Callback(callback="validate")
 */
class ProductVariantsPattern extends AbstractProductVariantsPattern
{
    /**
     * @var int
     *
     * @Assert\NotBlank()
     * @Assert\Range(min = 0.01)
     */
    private $price;

    /**
     * @var string
     */
    protected $factoryCode;
    
    /**
     * Set price
     *
     * @param int $price
     *
     * @return ProductVariantsPattern
     */
    public function setPrice($price)
    {
        $this->price = $price;

        return $this;
    }

    /**
     * Get price
     *
     * @return int
     */
    public function getPrice()
    {
        return $this->price;
    }
    
    /**
     * Get factory code
     *
     * @return string
     */
    public function getFactoryCode()
    {
        return $this->factoryCode;
    }

    /**
     * Set factory code
     *
     * @param string $code
     *
     * @return ProductVariantsPattern
     */
    public function setFactoryCode($code)
    {
        $this->factoryCode = $code;

        return $this;
    }
    
    /**
     * Get avtive active product code for this pattern
     * 
     * @return type
     */
    public function getActiveFactoryCode(){
        return $this->getFactoryCode() ? $this->getFactoryCode() : $this->getProduct()->getFactoryCode();
    }

    /**
     * Callback validation method.
     */
    public function validate(ExecutionContextInterface $context)
    {
        // Validate Product parts
        $error = false;
        $product = $this->getProduct();

        $productParts = new ArrayCollection();

        if ($product->isSimpleProductType()) {
            $productParts = $product->getProductParts();
        } elseif ($product->isSchematicProductType()) {
            $productParts = $this->getScheme()->getProductParts();
        }

        if ($productParts->count()) {
            $selectionArray = [];
            /** @var PersistentCollection $variantSelections */
            $variantSelections = $this->getPartPatternVariantSelections();
            /** @var ProductPartPatternVariantSelection $selection */
            foreach ($variantSelections as $selection) {
                if ($productParts->contains($selection->getProductPart())) {
                    $selectionArray[$selection->getProductPart()->getId()] = true;
                }
            }

            if ($productParts->count() > count($selectionArray)) {
                $error = true;
            }
        }

        // Validate SKU options.
        $productSkuOptions = $product->getSkuOptionVariantsGrouped();
        if (!empty($productSkuOptions)) {
            $selectionArray = [];
            /** @var ArrayCollection $optionSelections */
            $optionSelections = $this->getSkuOptionValues();
            /** @var SkuOptionVariant $selection */
            foreach ($optionSelections as $selection) {
                if (array_key_exists($selection->getSkuOptionType()->getId(), $productSkuOptions)) {
                    $selectionArray[$selection->getSkuOptionType()->getId()] = true;
                }
            }

            if (count($productSkuOptions) > count($selectionArray)) {
                $error = true;
            }
        }

        if ($error) {
            $context->buildViolation('You need to select at least one value for each option')
                ->addViolation();
        }
    }
}

{'fraction_non_alphanumeric': 0.12158681092220505, 'fraction_numerical': 0.001287995878413189, 'mean_word_length': 2.1260064412238324, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6719051', 'n_tokens_mistral': 2062, 'n_tokens_neox': 2018, 'n_words': 395}