/**
  * {@inheritdoc}
  */
 public function validate($attribute, Constraint $constraint)
 {
     $referenceDataName = $attribute->getProperty('reference_data_name');
     if (null !== $this->registry && in_array($attribute->getAttributeType(), $this->referenceDataType) && !$this->registry->has($referenceDataName)) {
         $references = array_keys($this->registry->all());
         $this->context->buildViolation($constraint->message)->setParameter('%reference_data_name%', $referenceDataName)->setParameter('%references%', implode(', ', $references))->addViolation();
     }
 }
 /**
  * @return array
  */
 protected function getReferenceDataTypeChoices()
 {
     $choices = [];
     foreach ($this->referenceDataRegistry->all() as $configuration) {
         if (ConfigurationInterface::TYPE_SIMPLE === $configuration->getType()) {
             $choices[$configuration->getName()] = $configuration->getName();
         }
     }
     return $choices;
 }
 /**
  * {@inheritdoc}
  */
 public function transform($value, array $options = [])
 {
     if (null === $this->registry) {
         return null;
     }
     $value = trim($value);
     if (empty($value)) {
         return null;
     }
     if (!$this->registry->has($value)) {
         $references = array_keys($this->registry->all());
         throw new \InvalidArgumentException(sprintf('Reference data "%s" does not exist. Allowed values are: %s', $value, implode(', ', $references)));
     }
     return $value;
 }
 /**
  * {@inheritdoc}
  */
 public function getEligibleProductIdsForVariantGroup($variantGroupId)
 {
     $sql = 'SELECT v.entity_id AS product_id, gp.group_id, ga.group_id ' . 'FROM pim_catalog_group g ' . 'INNER JOIN pim_catalog_group_attribute ga ON ga.group_id = g.id ' . 'INNER JOIN %product_value_table% v ON v.attribute_id = ga.attribute_id ' . 'LEFT JOIN pim_catalog_group_product gp ON (v.entity_id = gp.product_id) ' . 'INNER JOIN pim_catalog_group_type gt on gt.id = g.type_id ' . 'WHERE ga.group_id = :groupId AND gt.code = "VARIANT" ' . 'AND (v.option_id IS NOT NULL';
     if (null !== $this->referenceDataRegistry) {
         $references = $this->referenceDataRegistry->all();
         if (!empty($references)) {
             $valueMetadata = QueryBuilderUtility::getProductValueMetadata($this->_em, $this->_entityName);
             foreach ($references as $code => $referenceData) {
                 if (ConfigurationInterface::TYPE_SIMPLE === $referenceData->getType()) {
                     if ($valueMetadata->isAssociationWithSingleJoinColumn($code)) {
                         $sql .= sprintf(' OR v.%s IS NOT NULL', $valueMetadata->getSingleAssociationJoinColumnName($code));
                     }
                 }
             }
         }
     }
     $sql .= ') ' . 'GROUP BY v.entity_id ' . 'HAVING (gp.group_id IS NULL OR gp.group_id = ga.group_id) ' . 'AND COUNT(ga.attribute_id) = (SELECT COUNT(*) FROM pim_catalog_group_attribute WHERE group_id = :groupId)';
     $sql = QueryBuilderUtility::prepareDBALQuery($this->_em, $this->_entityName, $sql);
     $stmt = $this->getEntityManager()->getConnection()->prepare($sql);
     $stmt->bindValue('groupId', $variantGroupId);
     $stmt->execute();
     $results = $stmt->fetchAll();
     $productIds = array_map(function ($row) {
         return $row['product_id'];
     }, $results);
     return $productIds;
 }
 /**
  * @param string $value
  *
  * @throws \InvalidArgumentException
  */
 protected function checkIfReferenceDataExists($value)
 {
     if (in_array($value['attributeType'], $this->referenceDataType)) {
         if (!$this->registry->has($value['reference_data_name'])) {
             $references = array_keys($this->registry->all());
             throw new \InvalidArgumentException(sprintf('Reference data "%s" does not exist. Allowed values are: %s', $value['reference_data_name'], implode(', ', $references)));
         }
     }
 }
 /**
  * Get the reference data configuration
  *
  * @return JsonResponse
  */
 public function indexAction()
 {
     $referenceConfig = $this->registry->all();
     return new JsonResponse($this->normalizer->normalize($referenceConfig, 'internal_api'));
 }