/** * {@inheritdoc} */ public function loadMetadataFromFile(\ReflectionClass $class, $path) { $classMetadata = new ClassMetadata($class->getName()); $use = libxml_use_internal_errors(true); $dom = new \DOMDocument('1.0'); $dom->load($path); if (!$dom->schemaValidate(__DIR__ . '/../../../schema/mapping.xsd')) { $message = array_reduce(libxml_get_errors(), function ($foo, $error) { return $error->message; }); throw new \InvalidArgumentException(sprintf('Could not validate XML mapping at "%s": %s', $path, $message)); } libxml_use_internal_errors($use); $xpath = new \DOMXpath($dom); $xpath->registerNamespace('psict', self::XML_NAMESPACE); foreach ($xpath->query('//psict:class') as $classEl) { $classAttr = $classEl->getAttribute('name'); if ($classAttr !== $class->getName()) { throw new \InvalidArgumentException(sprintf('Expected class name to be "%s" but it is mapped as "%s"', $class->getName(), $classAttr)); } foreach ($xpath->query('./psict:field', $classEl) as $fieldEl) { $shared = $this->extractOptionSet($xpath, $fieldEl, 'shared-options'); $form = $this->extractOptionSet($xpath, $fieldEl, 'form-options'); $view = $this->extractOptionSet($xpath, $fieldEl, 'view-options'); $storage = $this->extractOptionSet($xpath, $fieldEl, 'storage-options'); $propertyMetadata = new PropertyMetadata($class->getName(), $fieldEl->getAttribute('name'), $fieldEl->getAttribute('type'), $fieldEl->getAttribute('role'), $fieldEl->getAttribute('group'), ['shared' => $shared, 'form' => $form, 'view' => $view, 'storage' => $storage]); $classMetadata->addPropertyMetadata($propertyMetadata); } } return $classMetadata; }
/** * {@inheritdoc} */ public function loadMetadataForClass(\ReflectionClass $class) { if (!isset($this->config[$class->getName()])) { return; } $config = array_merge(['fields' => []], $this->config[$class->getName()]); $classMetadata = new ClassMetadata($class->getName()); $defaults = ['type' => null, 'role' => null, 'group' => null, 'shared' => [], 'form' => [], 'view' => [], 'storage' => []]; foreach ($config['fields'] as $fieldName => $fieldConfig) { if ($diff = array_diff(array_keys($fieldConfig), array_keys($defaults))) { throw new \InvalidArgumentException(sprintf('Invalid configuration key(s) "%s" for field "%s" on class "%s", valid keys: "%s"', implode('", "', $diff), $fieldName, $class->getName(), implode('", "', array_keys($defaults)))); } $fieldConfig = array_merge($defaults, $fieldConfig); $propertyMetadata = new PropertyMetadata($class->getName(), $fieldName, $fieldConfig['type'], $fieldConfig['role'], $fieldConfig['group'], array_intersect_key($fieldConfig, array_flip(['view', 'storage', 'form', 'shared']))); $classMetadata->addPropertyMetadata($propertyMetadata); } return $classMetadata; }
/** * {@inheritdoc} */ public function loadMetadataForClass(\ReflectionClass $class) { $metadata = new ClassMetadata($class->getName()); $propertyMetadata = []; foreach ($class->getProperties() as $reflProperty) { $annotations = $this->reader->getPropertyAnnotations($reflProperty); foreach ($annotations as $annotation) { if ($annotation instanceof Annotations\Field) { $propertyMetadata = new PropertyMetadata($class->getName(), $reflProperty->getName(), $annotation->type, $annotation->role, $annotation->group, ['shared' => $annotation->shared, 'form' => $annotation->form, 'view' => $annotation->view, 'storage' => $annotation->storage]); $propertyMetadatas[] = $propertyMetadata; } } } if (empty($propertyMetadatas)) { return; } foreach ($propertyMetadatas as $propertyMetadata) { $metadata->addPropertyMetadata($propertyMetadata); } return $metadata; }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $grouped = []; $ungrouped = []; foreach ($this->classMetadata->getPropertyMetadata() as $propertyMetadata) { if (null === ($group = $propertyMetadata->getGroup())) { $ungrouped[] = $propertyMetadata; continue; } if (!isset($grouped[$group])) { $grouped[$group] = []; } $grouped[$group][] = $propertyMetadata; } $this->addPropertyMetadatas($builder, $ungrouped); foreach ($grouped as $group => $propertyMetadatas) { if (false === $builder->has($group)) { $builder->add($builder->create($group, FormType::class, ['inherit_data' => true])); } $this->addPropertyMetadatas($builder->get($group), $propertyMetadatas); } }