/**
  * Submit event handler.
  *
  * @param \Symfony\Component\Form\FormEvent $event
  * @throws \Symfony\Component\Form\Exception\UnexpectedTypeException
  * @throws \RuntimeException
  */
 public function onSubmit(FormEvent $event)
 {
     $data = $event->getData();
     if (null === $data) {
         $data = [];
     }
     if (!is_array($data) && !($data instanceof \Traversable && $data instanceof \ArrayAccess)) {
         throw new UnexpectedTypeException($data, 'array or (\\Traversable and \\ArrayAccess)');
     }
     foreach ($this->schema->getGroups() as $schemaGroup) {
         foreach ($schemaGroup->getDefinitions() as $schemaDefinition) {
             if (true === $schemaDefinition->getVirtual()) {
                 continue;
             }
             $identifier = $schemaDefinition->getIdentifier();
             if ($data->offsetExists($identifier)) {
                 /** @var CharacteristicInterface $characteristic */
                 $characteristic = $data->offsetGet($identifier);
                 if ($this->isNullOrEqualsParentData($characteristic)) {
                     $data->offsetUnset($identifier);
                     $this->em->remove($characteristic);
                 } else {
                     $characteristic->setIdentifier($identifier);
                 }
             } else {
                 throw new \RuntimeException(sprintf('Missing [%s] characteristic data.', $identifier));
             }
         }
     }
     $event->setData($data);
 }
예제 #2
0
 /**
  * Creates and returns a Schema from the given configuration array.
  *
  * @param array $configuration
  * @return Schema[]
  */
 protected function createSchemas(array $configuration)
 {
     $processor = new Processor();
     $processedConfiguration = $processor->processConfiguration(new SchemaConfiguration(), $configuration);
     $schemas = [];
     foreach ($processedConfiguration as $schemaName => $schemaConfig) {
         $schema = new Schema($schemaName, $schemaConfig['title']);
         foreach ($schemaConfig['groups'] as $groupName => $groupConfig) {
             $group = new Group($groupName, $groupConfig['title']);
             foreach ($groupConfig['characteristics'] as $characteristicName => $characteristicConfig) {
                 $fullName = implode(':', [$schemaName, $groupName, $characteristicName]);
                 $this->validateDefinitionConfig($fullName, $characteristicConfig);
                 $definition = new Definition();
                 $definition->setName($characteristicName)->setFullName($fullName)->setType($characteristicConfig['type'])->setTitle($characteristicConfig['title'])->setShared($characteristicConfig['shared'])->setVirtual($characteristicConfig['virtual'])->setPropertyPaths($characteristicConfig['property_paths'])->setFormat($characteristicConfig['format'])->setDisplayGroups($characteristicConfig['display_groups']);
                 $group->addDefinition($definition);
             }
             $schema->addGroup($group);
         }
         $schemas[] = $schema;
     }
     return $schemas;
 }
예제 #3
0
 /**
  * Adds a schema.
  *
  * @param Schema $schema
  * @throws \Exception
  * @return SchemaRegistry
  */
 private function addSchema(Schema $schema)
 {
     if (array_key_exists($schema->getName(), $this->schemas)) {
         throw new \Exception(sprintf('Schema "%s" is allready registered.', $schema->getName()));
     }
     $this->schemas[$schema->getName()] = $schema;
     return $this;
 }