Yaml.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace Gedmo\Tree\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\File,
  4. Gedmo\Mapping\Driver,
  5. Doctrine\Common\Persistence\Mapping\ClassMetadata,
  6. Gedmo\Exception\InvalidMappingException;
  7. /**
  8. * This is a yaml mapping driver for Tree
  9. * behavioral extension. Used for extraction of extended
  10. * metadata from yaml specificaly for Tree
  11. * extension.
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @package Gedmo.Tree.Mapping.Driver
  15. * @subpackage Yaml
  16. * @link http://www.gediminasm.org
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. class Yaml extends File implements Driver
  20. {
  21. /**
  22. * File extension
  23. * @var string
  24. */
  25. protected $_extension = '.dcm.yml';
  26. /**
  27. * List of types which are valid for timestamp
  28. *
  29. * @var array
  30. */
  31. private $validTypes = array(
  32. 'integer',
  33. 'smallint',
  34. 'bigint'
  35. );
  36. /**
  37. * List of tree strategies available
  38. *
  39. * @var array
  40. */
  41. private $strategies = array(
  42. 'nested'
  43. );
  44. /**
  45. * {@inheritDoc}
  46. */
  47. public function validateFullMetadata(ClassMetadata $meta, array $config)
  48. {
  49. if (isset($config['strategy'])) {
  50. $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
  51. $this->$method($meta, $config);
  52. } elseif ($config) {
  53. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  54. }
  55. }
  56. /**
  57. * {@inheritDoc}
  58. */
  59. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  60. $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
  61. $mapping = $yaml[$meta->name];
  62. if (isset($mapping['gedmo'])) {
  63. $classMapping = $mapping['gedmo'];
  64. if (isset($classMapping['tree']['type'])) {
  65. $strategy = $classMapping['tree']['type'];
  66. if (!in_array($strategy, $this->strategies)) {
  67. throw new InvalidMappingException("Tree type: $strategy is not available.");
  68. }
  69. $config['strategy'] = $strategy;
  70. }
  71. }
  72. if (isset($mapping['fields'])) {
  73. foreach ($mapping['fields'] as $field => $fieldMapping) {
  74. if (isset($fieldMapping['gedmo'])) {
  75. if (in_array('treeLeft', $fieldMapping['gedmo'])) {
  76. if (!$this->isValidField($meta, $field)) {
  77. throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  78. }
  79. $config['left'] = $field;
  80. } elseif (in_array('treeRight', $fieldMapping['gedmo'])) {
  81. if (!$this->isValidField($meta, $field)) {
  82. throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  83. }
  84. $config['right'] = $field;
  85. } elseif (in_array('treeLevel', $fieldMapping['gedmo'])) {
  86. if (!$this->isValidField($meta, $field)) {
  87. throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  88. }
  89. $config['level'] = $field;
  90. } elseif (in_array('treeRoot', $fieldMapping['gedmo'])) {
  91. if (!$this->isValidField($meta, $field)) {
  92. throw new InvalidMappingException("Tree root field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  93. }
  94. $config['root'] = $field;
  95. }
  96. }
  97. }
  98. }
  99. if (isset($mapping['manyToOne'])) {
  100. foreach ($mapping['manyToOne'] as $field => $relationMapping) {
  101. if (isset($relationMapping['gedmo'])) {
  102. if (in_array('treeParent', $relationMapping['gedmo'])) {
  103. if ($relationMapping['targetEntity'] != $meta->name) {
  104. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  105. }
  106. $config['parent'] = $field;
  107. }
  108. }
  109. }
  110. }
  111. }
  112. /**
  113. * {@inheritDoc}
  114. */
  115. protected function _loadMappingFile($file)
  116. {
  117. return \Symfony\Component\Yaml\Yaml::load($file);
  118. }
  119. /**
  120. * Checks if $field type is valid
  121. *
  122. * @param ClassMetadata $meta
  123. * @param string $field
  124. * @return boolean
  125. */
  126. protected function isValidField(ClassMetadata $meta, $field)
  127. {
  128. $mapping = $meta->getFieldMapping($field);
  129. return $mapping && in_array($mapping['type'], $this->validTypes);
  130. }
  131. /**
  132. * Validates metadata for nested type tree
  133. *
  134. * @param ClassMetadata $meta
  135. * @param array $config
  136. * @throws InvalidMappingException
  137. * @return void
  138. */
  139. private function validateNestedTreeMetadata(ClassMetadata $meta, array $config)
  140. {
  141. $missingFields = array();
  142. if (!isset($config['parent'])) {
  143. $missingFields[] = 'ancestor';
  144. }
  145. if (!isset($config['left'])) {
  146. $missingFields[] = 'left';
  147. }
  148. if (!isset($config['right'])) {
  149. $missingFields[] = 'right';
  150. }
  151. if ($missingFields) {
  152. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  153. }
  154. }
  155. }