protected function checkAssertsSet(OptionSet $entity) { $this->assertNull($entity->getId()); $this->assertEquals('test', $entity->getLabel()); $this->assertEquals('test', $entity->getValue()); $this->assertFalse($entity->getIsDefault()); $this->assertEquals(10, $entity->getPriority()); }
/** * @param FormEvent $event */ public function postSubmitData(FormEvent $event) { $form = $event->getForm(); $data = $event->getData(); /** @var FieldConfigModel $configModel */ $configModel = $form->getRoot()->getConfig()->getOptions()['config_model']; if (count($data)) { $em = $this->configManager->getEntityManager(); $optionValues = $oldOptions = $configModel->getOptions()->getValues(); $newOptions = []; array_walk_recursive($oldOptions, function (&$oldOption) { $oldOption = $oldOption->getId(); }); foreach ($data as $option) { if (is_array($option)) { $optionSet = new OptionSet(); $optionSet->setField($configModel); $optionSet->setData($option['id'], $option['priority'], $option['label'], (bool) $option['default']); } elseif (!$option->getId()) { $optionSet = $option; $optionSet->setField($configModel); } else { $optionSet = $option; } if ($optionSet->getLabel() != null) { $newOptions[] = $optionSet->getId(); } if (!in_array($optionSet, $optionValues) && $optionSet->getLabel() != null) { $em->persist($optionSet); } } $delOptions = array_diff($oldOptions, $newOptions); foreach ($delOptions as $key => $delOption) { $em->remove($configModel->getOptions()->getValues()[$key]); } $em->flush(); } }