2015-08-18 00:00:26 +00: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\Serializer\Exception\MappingException ;
use Symfony\Component\Serializer\Mapping\AttributeMetadata ;
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface ;
use Symfony\Component\Yaml\Parser ;
/**
* YAML File Loader .
*
* @ author Kévin Dunglas < dunglas @ gmail . com >
*/
class YamlFileLoader extends FileLoader
{
private $yamlParser ;
/**
2015-10-08 18:40:12 +00:00
* An array of YAML class descriptions .
2015-08-18 00:00:26 +00:00
*
* @ var array
*/
2018-11-23 12:29:20 +00:00
private $classes ;
2015-08-18 00:00:26 +00:00
/**
* { @ inheritdoc }
*/
public function loadClassMetadata ( ClassMetadataInterface $classMetadata )
{
if ( null === $this -> classes ) {
2018-11-23 12:29:20 +00:00
$this -> classes = $this -> getClassesFromYaml ();
}
2015-08-18 00:00:26 +00:00
2018-11-23 12:29:20 +00:00
if ( ! $this -> classes ) {
return false ;
2015-08-18 00:00:26 +00:00
}
2017-04-13 14:53:35 +00:00
if ( ! isset ( $this -> classes [ $classMetadata -> getName ()])) {
return false ;
}
2015-08-18 00:00:26 +00:00
2017-04-13 14:53:35 +00:00
$yaml = $this -> classes [ $classMetadata -> getName ()];
2016-04-20 16:56:34 +00:00
2018-11-23 12:29:20 +00:00
if ( isset ( $yaml [ 'attributes' ]) && \is_array ( $yaml [ 'attributes' ])) {
2017-04-13 14:53:35 +00:00
$attributesMetadata = $classMetadata -> getAttributesMetadata ();
foreach ( $yaml [ 'attributes' ] as $attribute => $data ) {
if ( isset ( $attributesMetadata [ $attribute ])) {
$attributeMetadata = $attributesMetadata [ $attribute ];
} else {
$attributeMetadata = new AttributeMetadata ( $attribute );
$classMetadata -> addAttributeMetadata ( $attributeMetadata );
}
2016-04-20 16:56:34 +00:00
2017-04-13 14:53:35 +00:00
if ( isset ( $data [ 'groups' ])) {
2018-11-23 12:29:20 +00:00
if ( ! \is_array ( $data [ 'groups' ])) {
2017-04-13 14:53:35 +00:00
throw new MappingException ( sprintf ( 'The "groups" key must be an array of strings in "%s" for the attribute "%s" of the class "%s".' , $this -> file , $attribute , $classMetadata -> getName ()));
}
foreach ( $data [ 'groups' ] as $group ) {
2018-11-23 12:29:20 +00:00
if ( ! \is_string ( $group )) {
2017-04-13 14:53:35 +00:00
throw new MappingException ( sprintf ( 'Group names must be strings in "%s" for the attribute "%s" of the class "%s".' , $this -> file , $attribute , $classMetadata -> getName ()));
2015-08-18 00:00:26 +00:00
}
2017-04-13 14:53:35 +00:00
$attributeMetadata -> addGroup ( $group );
2015-08-18 00:00:26 +00:00
}
}
2018-11-23 12:29:20 +00:00
if ( isset ( $data [ 'max_depth' ])) {
if ( ! \is_int ( $data [ 'max_depth' ])) {
throw new MappingException ( sprintf ( 'The "max_depth" value must be an integer in "%s" for the attribute "%s" of the class "%s".' , $this -> file , $attribute , $classMetadata -> getName ()));
}
$attributeMetadata -> setMaxDepth ( $data [ 'max_depth' ]);
}
2015-08-18 00:00:26 +00:00
}
}
2017-04-13 14:53:35 +00:00
return true ;
2015-08-18 00:00:26 +00:00
}
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 -> getClassesFromYaml ();
}
return array_keys ( $this -> classes );
}
private function getClassesFromYaml ()
{
if ( ! stream_is_local ( $this -> file )) {
throw new MappingException ( sprintf ( 'This is not a local file "%s".' , $this -> file ));
}
if ( null === $this -> yamlParser ) {
$this -> yamlParser = new Parser ();
}
$classes = $this -> yamlParser -> parseFile ( $this -> file );
if ( empty ( $classes )) {
return array ();
}
if ( ! \is_array ( $classes )) {
throw new MappingException ( sprintf ( 'The file "%s" must contain a YAML array.' , $this -> file ));
}
return $classes ;
}
2015-08-18 00:00:26 +00:00
}