YamlFileLoader.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Mapping\Loader;
  11. use Symfony\Component\Validator\Mapping\ClassMetadata;
  12. use Symfony\Component\Yaml\Yaml;
  13. class YamlFileLoader extends FileLoader
  14. {
  15. /**
  16. * An array of YAML class descriptions
  17. * @val array
  18. */
  19. protected $classes = null;
  20. /**
  21. * {@inheritDoc}
  22. */
  23. public function loadClassMetadata(ClassMetadata $metadata)
  24. {
  25. if (null === $this->classes) {
  26. $this->classes = Yaml::parse($this->file);
  27. // empty file
  28. if (null === $this->classes) {
  29. return false;
  30. }
  31. // not an array
  32. if (!is_array($this->classes)) {
  33. throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $this->file));
  34. }
  35. if (isset($this->classes['namespaces'])) {
  36. foreach ($this->classes['namespaces'] as $prefix => $namespace) {
  37. $this->namespaces[$prefix] = $namespace;
  38. }
  39. unset($this->classes['namespaces']);
  40. }
  41. }
  42. // TODO validation
  43. if (isset($this->classes[$metadata->getClassName()])) {
  44. $yaml = $this->classes[$metadata->getClassName()];
  45. if (isset($yaml['group_sequence'])) {
  46. $metadata->setGroupSequence($yaml['group_sequence']);
  47. }
  48. if (isset($yaml['constraints'])) {
  49. foreach ($this->parseNodes($yaml['constraints']) as $constraint) {
  50. $metadata->addConstraint($constraint);
  51. }
  52. }
  53. if (isset($yaml['properties'])) {
  54. foreach ($yaml['properties'] as $property => $constraints) {
  55. foreach ($this->parseNodes($constraints) as $constraint) {
  56. $metadata->addPropertyConstraint($property, $constraint);
  57. }
  58. }
  59. }
  60. if (isset($yaml['getters'])) {
  61. foreach ($yaml['getters'] as $getter => $constraints) {
  62. foreach ($this->parseNodes($constraints) as $constraint) {
  63. $metadata->addGetterConstraint($getter, $constraint);
  64. }
  65. }
  66. }
  67. return true;
  68. }
  69. return false;
  70. }
  71. /**
  72. * Parses a collection of YAML nodes
  73. *
  74. * @param array $nodes The YAML nodes
  75. *
  76. * @return array An array of values or Constraint instances
  77. */
  78. protected function parseNodes(array $nodes)
  79. {
  80. $values = array();
  81. foreach ($nodes as $name => $childNodes) {
  82. if (is_numeric($name) && is_array($childNodes) && count($childNodes) == 1) {
  83. $options = current($childNodes);
  84. if (is_array($options)) {
  85. $options = $this->parseNodes($options);
  86. }
  87. $values[] = $this->newConstraint(key($childNodes), $options);
  88. } else {
  89. if (is_array($childNodes)) {
  90. $childNodes = $this->parseNodes($childNodes);
  91. }
  92. $values[$name] = $childNodes;
  93. }
  94. }
  95. return $values;
  96. }
  97. }