/** * Constraint is applied on ProductValue data property. * That's why we use the current property path to guess the code * of the attribute to which the data belongs to. * @param object $rawValue * @param Constraint $constraint * * @see Pim\Bundle\CatalogBundle\Validator\ConstraintGuesser\UniqueValueGuesser */ public function validate($rawValue, Constraint $constraint) { if (empty($rawValue)) { return; } $value = $this->getProductValue(); if ($value instanceof ProductValueInterface && $this->productManager->valueExists($value)) { $this->context->addViolation($constraint->message); } }
/** * Due to constraint guesser, the constraint is applied on : * - ProductValueInterface data when applied through form * - ProductValueInterface when applied directly through validator * * The constraint guesser should be re-worked in a future version to avoid such behavior * * @param ProductValueInterface|mixed $data * @param Constraint $constraint * * @see Pim\Bundle\CatalogBundle\Validator\ConstraintGuesser\UniqueValueGuesser */ public function validate($data, Constraint $constraint) { if (empty($data)) { return; } if (is_object($data) && $data instanceof ProductValueInterface) { $productValue = $data; } else { $productValue = $this->getProductValueFromForm(); } if ($productValue instanceof ProductValueInterface && $this->productManager->valueExists($productValue)) { if ($productValue->getData() !== null && $productValue->getData() !== '') { $this->context->addViolation($constraint->message); } } }
/** * Checks the uniqueness of product values that should be unique * As the uniqueness check is normally executed against the database * and imported products have not been persisted yet, this effectively * checks that the items in the current batch don't contain duplicate values * for unique attributes. * * @param object $entity * @param array $columnsInfo * @param array $data * * @throws DuplicateProductValueException When duplicate values are encountered */ protected function checkUniqueValues($entity, array $columnsInfo, array $data) { foreach ($columnsInfo as $columnInfo) { if ($columnInfo->getAttribute()) { $value = $this->getProductValue($entity, $columnInfo); if ($value->getAttribute()->isUnique()) { $code = $value->getAttribute()->getCode(); $valueData = (string) $value; if ($valueData !== '') { if ($this->productManager->valueExists($value)) { throw new DuplicateProductValueException($code, $valueData, $data); } $this->uniqueValues[$code] = isset($this->uniqueValues[$code]) ? $this->uniqueValues[$code] : []; if (isset($this->uniqueValues[$code][$valueData])) { throw new DuplicateProductValueException($code, $valueData, $data); } else { $this->uniqueValues[$code][$valueData] = ""; } } } } } }
function it_does_not_validate_with_empty_value(ProductManager $productManager, ProductValueInterface $value, ExecutionContextInterface $context, Constraint $constraint) { $productManager->valueExists($value)->shouldNotBeCalled(); $context->addViolation(Argument::any())->shouldNotBeCalled(); $this->validate("", $constraint)->shouldReturn(null); }