getMetadataFor() public method

Gets the class metadata descriptor for a class.
public getMetadataFor ( string $className ) : ClassMetadata
$className string The name of the class.
return ClassMetadata
Beispiel #1
0
 /**
  * INTERNAL: Performance sensitive method
  *
  * @throws MarshallerException
  * @param object $mappedObject
  * @param WriterHelper $writer
  * @return void
  */
 private function doMarshal($mappedObject, WriterHelper $writer)
 {
     $className = get_class($mappedObject);
     $classMetadata = $this->classMetadataFactory->getMetadataFor($className);
     if (!$this->classMetadataFactory->hasMetadataFor($className)) {
         throw MarshallerException::mappingNotFoundForClass($className);
     }
     // PreMarshall Hook
     if ($classMetadata->hasLifecycleCallbacks(Events::preMarshal)) {
         $classMetadata->invokeLifecycleCallbacks(Events::preMarshal, $mappedObject);
     }
     $writer->startElement($classMetadata->getXmlName());
     $namespaces = $classMetadata->getXmlNamespaces();
     if (!empty($namespaces)) {
         foreach ($namespaces as $namespace) {
             $writer->writeNamespace($namespace['url'], $namespace['prefix']);
         }
     }
     // build ordered field mappings for this class
     $fieldMappings = $classMetadata->getFieldMappings();
     $orderedMap = array();
     if (!empty($fieldMappings)) {
         foreach ($fieldMappings as $fieldMapping) {
             $orderedMap[$fieldMapping['node']][] = $fieldMapping;
         }
     }
     // do attributes
     if (array_key_exists(ClassMetadata::XML_ATTRIBUTE, $orderedMap)) {
         foreach ($orderedMap[ClassMetadata::XML_ATTRIBUTE] as $fieldMapping) {
             $fieldName = $fieldMapping['fieldName'];
             $fieldValue = $classMetadata->getFieldValue($mappedObject, $fieldName);
             if ($classMetadata->isRequired($fieldName) && $fieldValue === null) {
                 throw MarshallerException::fieldRequired($className, $fieldName);
             }
             if ($fieldValue !== null || $classMetadata->isNillable($fieldName)) {
                 $this->_writeAttribute($writer, $classMetadata, $fieldName, $fieldValue);
             }
         }
     }
     // do text
     if (array_key_exists(ClassMetadata::XML_TEXT, $orderedMap)) {
         foreach ($orderedMap[ClassMetadata::XML_TEXT] as $fieldMapping) {
             $fieldName = $fieldMapping['fieldName'];
             $fieldValue = $classMetadata->getFieldValue($mappedObject, $fieldName);
             if ($classMetadata->isRequired($fieldName) && $fieldValue === null) {
                 throw MarshallerException::fieldRequired($className, $fieldName);
             }
             if ($fieldValue !== null || $classMetadata->isNillable($fieldName)) {
                 $this->_writeText($writer, $classMetadata, $fieldName, $fieldValue);
             }
         }
     }
     // do elements
     if (array_key_exists(ClassMetadata::XML_ELEMENT, $orderedMap)) {
         foreach ($orderedMap[ClassMetadata::XML_ELEMENT] as $fieldMapping) {
             $fieldName = $fieldMapping['fieldName'];
             $fieldValue = $classMetadata->getFieldValue($mappedObject, $fieldName);
             if ($classMetadata->isRequired($fieldName) && $fieldValue === null) {
                 throw MarshallerException::fieldRequired($className, $fieldName);
             }
             if ($fieldValue !== null || $classMetadata->isNillable($fieldName)) {
                 $this->_writeElement($writer, $classMetadata, $fieldName, $fieldValue);
             }
         }
     }
     // PostMarshal hook
     if ($classMetadata->hasLifecycleCallbacks(Events::postMarshal)) {
         $classMetadata->invokeLifecycleCallbacks(Events::postMarshal, $mappedObject);
     }
     $writer->endElement();
 }
 /**
  * Returns the OXM mapping descriptor for a class.
  *
  * The class name must be the fully-qualified class name without a leading backslash
  * (as it is returned by get_class($obj)) or an aliased class name.
  *
  * Examples:
  * MyProject\Domain\User
  * sales:PriceRequest
  *
  * @return \Doctrine\OXM\Mapping\ClassMetadata
  * @internal Performance-sensitive method.
  */
 public function getClassMetadata($className)
 {
     return $this->metadataFactory->getMetadataFor($className);
 }
 /**
  * INTERNAL: Performance sensitive method
  *
  * @throws MarshallerException
  * @param object $mappedObject
  * @param WriterHelper $writer
  * @return void
  */
 private function doMarshal($mappedObject, WriterHelper $writer, $fieldMapping = NULL)
 {
     $className = get_class($mappedObject);
     try {
         $classMetadata = $this->classMetadataFactory->getMetadataFor($className);
     } catch (\Doctrine\OXM\Mapping\MappingException $e) {
         $classMetadata = NULL;
     }
     if (!$classMetadata) {
         if (!$this->allowUnknownElements) {
             throw MarshallerException::mappingNotFoundForClass($className);
         }
     } else {
         // PreMarshall Hook
         if ($classMetadata->hasLifecycleCallbacks(Events::preMarshal)) {
             $classMetadata->invokeLifecycleCallbacks(Events::preMarshal, $mappedObject);
         }
     }
     if (isset($this->visited[spl_object_hash($mappedObject)])) {
         return;
     }
     $this->visited[spl_object_hash($mappedObject)] = true;
     if (!$classMetadata) {
         return $this->doMarshalUnknownElement($mappedObject, $writer, $fieldMapping);
     }
     $xmlName = $classMetadata->getXmlName();
     if (isset($fieldMapping['forceName']) && $fieldMapping['forceName'] && isset($fieldMapping['name']) && $fieldMapping['name'] != '*') {
         $xmlName = $fieldMapping['name'];
     }
     $writer->startElement($xmlName);
     $namespaces = $classMetadata->getXmlNamespaces();
     if (!empty($namespaces)) {
         foreach ($namespaces as $namespace) {
             $writer->writeNamespace($namespace['url'], $namespace['prefix']);
         }
     }
     // build ordered field mappings for this class
     $fieldMappings = $classMetadata->getFieldMappings();
     $orderedMap = array();
     if (!empty($fieldMappings)) {
         foreach ($fieldMappings as $fieldMapping) {
             $orderedMap[$fieldMapping['node']][] = $fieldMapping;
         }
     }
     if ($this->allowUnknownElements) {
         $doneFields = array();
     }
     // do attributes
     if (array_key_exists(ClassMetadata::XML_ATTRIBUTE, $orderedMap)) {
         foreach ($orderedMap[ClassMetadata::XML_ATTRIBUTE] as $fieldMapping) {
             $fieldName = $fieldMapping['fieldName'];
             $fieldValue = $classMetadata->getFieldValue($mappedObject, $fieldName);
             if ($classMetadata->isRequired($fieldName) && $fieldValue === null) {
                 throw MarshallerException::fieldRequired($className, $fieldName);
             }
             if ($fieldValue !== null || $classMetadata->isNullable($fieldName)) {
                 $this->writeAttribute($writer, $classMetadata, $fieldName, $fieldValue);
             }
             if ($this->allowUnknownElements) {
                 $doneFields[$fieldName] = TRUE;
             }
         }
     }
     // do others
     if (!empty($fieldMappings)) {
         foreach ($fieldMappings as $fieldMapping) {
             if ($fieldMapping['node'] == ClassMetadata::XML_ATTRIBUTE) {
                 // Already done
                 continue;
             }
             $fieldName = $fieldMapping['fieldName'];
             $fieldValue = $classMetadata->getFieldValue($mappedObject, $fieldName);
             if ($classMetadata->isRequired($fieldName) && $fieldValue === null) {
                 throw MarshallerException::fieldRequired($className, $fieldName);
             }
             if ($fieldValue !== null || $classMetadata->isNullable($fieldName)) {
                 if ($fieldMapping['node'] == ClassMetadata::XML_VALUE) {
                     $type = $classMetadata->getTypeOfField($fieldName);
                     $writer->writeValue(Type::getType($type)->convertToXmlValue($fieldValue));
                 } else {
                     if ($fieldMapping['node'] == ClassMetadata::XML_TEXT) {
                         $this->writeText($writer, $classMetadata, $fieldName, $fieldValue);
                     } else {
                         $this->writeElement($writer, $classMetadata, $fieldName, $fieldValue);
                     }
                 }
             }
             if ($this->allowUnknownElements) {
                 $doneFields[$fieldName] = TRUE;
             }
         }
     }
     // Do anything that still remains
     if ($this->allowUnknownElements) {
         foreach (get_object_vars($mappedObject) as $thisPropertyName => $thisProperty) {
             if (isset($doneFields[$thisPropertyName])) {
                 continue;
             }
             $this->doMarshal($thisProperty, $writer);
         }
     }
     // PostMarshal hook
     if ($classMetadata->hasLifecycleCallbacks(Events::postMarshal)) {
         $classMetadata->invokeLifecycleCallbacks(Events::postMarshal, $mappedObject);
     }
     $writer->endElement();
 }