/**
  * @param ExecutionContextInterface $context
  * @param AttributeInterface        $attribute
  * @param DataInterface             $data
  *
  * @throws Exception
  */
 protected function checkUnique(ExecutionContextInterface $context, AttributeInterface $attribute, DataInterface $data)
 {
     $valueData = $data->get($attribute->getCode());
     if ($attribute->getType() instanceof IdentifierAttributeType) {
         /** @var DataRepository $repo */
         $repo = $this->doctrine->getRepository($data->getFamily()->getDataClass());
         $result = $repo->findByIdentifier($data->getFamily(), $valueData);
         if ($result && $result->getId() !== $data->getId()) {
             $this->buildAttributeViolation($context, $attribute, 'unique', $valueData);
         }
         return;
     }
     /** @var ValueRepository $repo */
     $repo = $this->doctrine->getRepository($data->getFamily()->getValueClass());
     $values = $repo->findBy(['attributeCode' => $attribute->getCode(), $attribute->getType()->getDatabaseType() => $valueData]);
     /** @var ValueInterface $value */
     foreach ($values as $value) {
         if (!$value->getData()) {
             continue;
             // @warning this should not occur ! Log an error
         }
         if ($value->getData()->getId() !== $data->getId()) {
             $this->buildAttributeViolation($context, $attribute, 'unique', $valueData);
             return;
         }
     }
 }
 /**
  * @param AttributeInterface $value
  * @return AttributeInterface
  * @throws UnexpectedValueException
  */
 public static function normalizeVariantAttribute($value)
 {
     if (!$value instanceof AttributeInterface) {
         throw new UnexpectedValueException('"attribute" option must be an AttributeInterface');
     }
     /** @var AttributeInterface $value */
     if (!$value->getType() instanceof VariantAttributeType) {
         throw new UnexpectedValueException("Attribute's type must be a VariantAttributeType");
     }
     return $value;
 }
Ejemplo n.º 3
0
 /**
  * Set the value's data of a given attribute
  *
  * @param AttributeInterface $attribute
  * @param mixed              $dataValue
  * @param array              $context
  *
  * @throws UnexpectedValueException
  * @throws AccessException
  * @throws InvalidArgumentException
  * @throws UnexpectedTypeException
  *
  * @return DataInterface
  */
 public function setValueData(AttributeInterface $attribute, $dataValue, array $context = null)
 {
     if ($attribute->getType() instanceof IdentifierAttributeType) {
         $value = $this;
     } else {
         $value = $this->getValue($attribute, $context);
         if (!$value) {
             $value = $this->createValue($attribute, $context);
         }
     }
     $accessor = PropertyAccess::createPropertyAccessor();
     $accessor->setValue($value, $attribute->getType()->getDatabaseType(), $dataValue);
     return $this;
 }
 /**
  * @param FamilyInterface    $parentFamily
  * @param AttributeInterface $attribute
  * @return string
  */
 protected function getTargetClass(FamilyInterface $parentFamily, AttributeInterface $attribute)
 {
     /** @var \Doctrine\ORM\Mapping\ClassMetadata $classMetadata */
     $classMetadata = $this->manager->getClassMetadata($parentFamily->getValueClass());
     try {
         $mapping = $classMetadata->getAssociationMapping($attribute->getType()->getDatabaseType());
     } catch (MappingException $e) {
         return null;
     }
     if (empty($mapping['targetEntity'])) {
         return null;
     }
     $type = $mapping['targetEntity'];
     if ($attribute->isMultiple()) {
         $type .= '[]';
     }
     return '\\' . $type;
 }
 /**
  * @return string
  */
 public function getColumn()
 {
     return $this->joinAlias . '.' . $this->attribute->getType()->getDatabaseType();
 }