2015-08-17 17:00:26 -07:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\Serializer\Mapping\Loader ;
use Symfony\Component\Config\Util\XmlUtils ;
use Symfony\Component\Serializer\Exception\MappingException ;
use Symfony\Component\Serializer\Mapping\AttributeMetadata ;
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface ;
/**
* Loads XML mapping files .
*
* @ author Kévin Dunglas < dunglas @ gmail . com >
*/
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 ) {
2018-11-23 12:29:20 +00:00
$this -> classes = $this -> getClassesFromXml ();
}
2015-08-17 17:00:26 -07:00
2018-11-23 12:29:20 +00:00
if ( ! $this -> classes ) {
return false ;
2015-08-17 17:00:26 -07:00
}
$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 );
}
2018-11-23 12:29:20 +00:00
if ( isset ( $attribute [ 'max-depth' ])) {
$attributeMetadata -> setMaxDepth (( int ) $attribute [ 'max-depth' ]);
}
2015-08-17 17:00:26 -07:00
}
return true ;
}
return false ;
}
2018-11-23 12:29:20 +00:00
/**
* Return the names of the classes mapped in this file .
*
* @ return string [] The classes names
*/
public function getMappedClasses ()
{
if ( null === $this -> classes ) {
$this -> classes = $this -> getClassesFromXml ();
}
return array_keys ( $this -> classes );
}
2015-08-17 17:00:26 -07:00
/**
* 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 );
}
2018-11-23 12:29:20 +00:00
private function getClassesFromXml ()
{
$xml = $this -> parseFile ( $this -> file );
$classes = array ();
foreach ( $xml -> class as $class ) {
$classes [( string ) $class [ 'name' ]] = $class ;
}
return $classes ;
}
2015-08-17 17:00:26 -07:00
}