/** * Check that collection is in right order. * * For increasing collection values must be in ascending order. * For decreasing collection value must be in descending order. * * @param PersistentCollection $value * @param CategoriesConstraint $constraint */ protected function validateOrder(PersistentCollection $value, CategoriesConstraint $constraint) { if ($value->isEmpty()) { return; } $orderedByIndex = $value->matching(new Criteria(null, ['categoryIndex' => Criteria::ASC])); $isIncreasing = $this->isIncreasing($orderedByIndex); if ($isIncreasing) { $criteria = Criteria::ASC; } else { $criteria = Criteria::DESC; } $orderedByValue = $value->matching(new Criteria(null, ['minValue' => $criteria])); if ($orderedByValue->toArray() !== $orderedByIndex->toArray()) { $this->context->addViolationAt($constraint->getType(), $constraint->message, ['%order%' => $criteria]); return; } if (!$isIncreasing) { return; } $invalidItems = $orderedByValue->filter(function (RFMMetricCategory $category) { $maxValue = $category->getMaxValue(); if (!$maxValue) { return false; } return $category->getMinValue() >= $maxValue; }); if (!$invalidItems->isEmpty()) { $this->context->addViolationAt($constraint->getType(), $constraint->message, ['%order%' => $criteria]); } }
/** * @param string $type * * @return CategoriesConstraint */ protected function getConstraint($type) { $constraint = new CategoriesConstraint(); $constraint->setType($type); return $constraint; }
/** * @param FormInterface $form * @param array $categories */ protected function addRFMTypes(FormInterface $form, array $categories) { foreach (RFMMetricCategory::$types as $type) { $typeCategories = array_filter($categories, function (RFMMetricCategory $category) use($type) { return $category->getCategoryType() === $type; }); $collection = new PersistentCollection($this->doctrineHelper->getEntityManager($this->rfmCategoryClass), $this->doctrineHelper->getEntityMetadata($this->rfmCategoryClass), new ArrayCollection($typeCategories)); $collection->takeSnapshot(); $constraint = new CategoriesConstraint(); $constraint->setType($type); $form->add($type, RFMCategorySettingsType::NAME, [RFMCategorySettingsType::TYPE_OPTION => $type, 'label' => sprintf('orocrm.analytics.form.%s.label', $type), 'tooltip' => sprintf('orocrm.analytics.%s.tooltip', $type), 'mapped' => false, 'required' => false, 'error_bubbling' => false, 'is_increasing' => $type === RFMMetricCategory::TYPE_RECENCY, 'constraints' => [$constraint], 'data' => $collection]); } }