You are here

class XmlFileLoader in Zircon Profile 8.0

Same name in this branch
  1. 8.0 vendor/symfony/routing/Loader/XmlFileLoader.php \Symfony\Component\Routing\Loader\XmlFileLoader
  2. 8.0 vendor/symfony/dependency-injection/Loader/XmlFileLoader.php \Symfony\Component\DependencyInjection\Loader\XmlFileLoader
  3. 8.0 vendor/symfony/validator/Mapping/Loader/XmlFileLoader.php \Symfony\Component\Validator\Mapping\Loader\XmlFileLoader
  4. 8.0 vendor/symfony/serializer/Mapping/Loader/XmlFileLoader.php \Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader
Same name and namespace in other branches
  1. 8 vendor/symfony/serializer/Mapping/Loader/XmlFileLoader.php \Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader

Loads XML mapping files.

@author Kévin Dunglas <dunglas@gmail.com>

Hierarchy

Expanded class hierarchy of XmlFileLoader

1 file declares its use of XmlFileLoader
XmlFileLoaderTest.php in vendor/symfony/serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php

File

vendor/symfony/serializer/Mapping/Loader/XmlFileLoader.php, line 24

Namespace

Symfony\Component\Serializer\Mapping\Loader
View source
class XmlFileLoader extends FileLoader {

  /**
   * An array of {@class \SimpleXMLElement} instances.
   *
   * @var \SimpleXMLElement[]|null
   */
  private $classes;

  /**
   * {@inheritdoc}
   */
  public function loadClassMetadata(ClassMetadataInterface $classMetadata) {
    if (null === $this->classes) {
      $this->classes = array();
      $xml = $this
        ->parseFile($this->file);
      foreach ($xml->class as $class) {
        $this->classes[(string) $class['name']] = $class;
      }
    }
    $attributesMetadata = $classMetadata
      ->getAttributesMetadata();
    if (isset($this->classes[$classMetadata
      ->getName()])) {
      $xml = $this->classes[$classMetadata
        ->getName()];
      foreach ($xml->attribute as $attribute) {
        $attributeName = (string) $attribute['name'];
        if (isset($attributesMetadata[$attributeName])) {
          $attributeMetadata = $attributesMetadata[$attributeName];
        }
        else {
          $attributeMetadata = new AttributeMetadata($attributeName);
          $classMetadata
            ->addAttributeMetadata($attributeMetadata);
        }
        foreach ($attribute->group as $group) {
          $attributeMetadata
            ->addGroup((string) $group);
        }
      }
      return true;
    }
    return false;
  }

  /**
   * Parses a XML File.
   *
   * @param string $file Path of file
   *
   * @return \SimpleXMLElement
   *
   * @throws MappingException
   */
  private function parseFile($file) {
    try {
      $dom = XmlUtils::loadFile($file, __DIR__ . '/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd');
    } catch (\Exception $e) {
      throw new MappingException($e
        ->getMessage(), $e
        ->getCode(), $e);
    }
    return simplexml_import_dom($dom);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileLoader::$file protected property
FileLoader::__construct public function Constructor.
XmlFileLoader::$classes private property An array of {@class \SimpleXMLElement} instances.
XmlFileLoader::loadClassMetadata public function Load class metadata. Overrides LoaderInterface::loadClassMetadata
XmlFileLoader::parseFile private function Parses a XML File.