Yaml.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. 'closure'
  44. );
  45. /**
  46. * {@inheritDoc}
  47. */
  48. public function validateFullMetadata(ClassMetadata $meta, array $config)
  49. {
  50. if (isset($config['strategy'])) {
  51. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  52. throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
  53. }
  54. $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
  55. $this->$method($meta, $config);
  56. } elseif ($config) {
  57. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  58. }
  59. }
  60. /**
  61. * {@inheritDoc}
  62. */
  63. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  64. $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
  65. $mapping = $yaml[$meta->name];
  66. if (isset($mapping['gedmo'])) {
  67. $classMapping = $mapping['gedmo'];
  68. if (isset($classMapping['tree']['type'])) {
  69. $strategy = $classMapping['tree']['type'];
  70. if (!in_array($strategy, $this->strategies)) {
  71. throw new InvalidMappingException("Tree type: $strategy is not available.");
  72. }
  73. $config['strategy'] = $strategy;
  74. }
  75. if (isset($classMapping['tree']['closure'])) {
  76. $class = $classMapping['tree']['closure'];
  77. if (!class_exists($class)) {
  78. throw new InvalidMappingException("Tree closure class: {$class} does not exist.");
  79. }
  80. $config['closure'] = $class;
  81. }
  82. }
  83. if (isset($mapping['fields'])) {
  84. foreach ($mapping['fields'] as $field => $fieldMapping) {
  85. if (isset($fieldMapping['gedmo'])) {
  86. if (in_array('treeLeft', $fieldMapping['gedmo'])) {
  87. if (!$this->isValidField($meta, $field)) {
  88. throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  89. }
  90. $config['left'] = $field;
  91. } elseif (in_array('treeRight', $fieldMapping['gedmo'])) {
  92. if (!$this->isValidField($meta, $field)) {
  93. throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  94. }
  95. $config['right'] = $field;
  96. } elseif (in_array('treeLevel', $fieldMapping['gedmo'])) {
  97. if (!$this->isValidField($meta, $field)) {
  98. throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  99. }
  100. $config['level'] = $field;
  101. } elseif (in_array('treeRoot', $fieldMapping['gedmo'])) {
  102. if (!$this->isValidField($meta, $field)) {
  103. throw new InvalidMappingException("Tree root field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  104. }
  105. $config['root'] = $field;
  106. } elseif (in_array('treeChildCount', $fieldMapping['gedmo'])) {
  107. if (!$this->isValidField($meta, $field)) {
  108. throw new InvalidMappingException("Tree child count field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  109. }
  110. $config['childCount'] = $field;
  111. }
  112. }
  113. }
  114. }
  115. if (isset($mapping['manyToOne'])) {
  116. foreach ($mapping['manyToOne'] as $field => $relationMapping) {
  117. if (isset($relationMapping['gedmo'])) {
  118. if (in_array('treeParent', $relationMapping['gedmo'])) {
  119. if ($relationMapping['targetEntity'] != $meta->name) {
  120. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  121. }
  122. $config['parent'] = $field;
  123. }
  124. }
  125. }
  126. }
  127. }
  128. /**
  129. * {@inheritDoc}
  130. */
  131. protected function _loadMappingFile($file)
  132. {
  133. return \Symfony\Component\Yaml\Yaml::load($file);
  134. }
  135. /**
  136. * Checks if $field type is valid
  137. *
  138. * @param ClassMetadata $meta
  139. * @param string $field
  140. * @return boolean
  141. */
  142. protected function isValidField(ClassMetadata $meta, $field)
  143. {
  144. $mapping = $meta->getFieldMapping($field);
  145. return $mapping && in_array($mapping['type'], $this->validTypes);
  146. }
  147. /**
  148. * Validates metadata for nested type tree
  149. *
  150. * @param ClassMetadata $meta
  151. * @param array $config
  152. * @throws InvalidMappingException
  153. * @return void
  154. */
  155. private function validateNestedTreeMetadata(ClassMetadata $meta, array $config)
  156. {
  157. $missingFields = array();
  158. if (!isset($config['parent'])) {
  159. $missingFields[] = 'ancestor';
  160. }
  161. if (!isset($config['left'])) {
  162. $missingFields[] = 'left';
  163. }
  164. if (!isset($config['right'])) {
  165. $missingFields[] = 'right';
  166. }
  167. if ($missingFields) {
  168. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  169. }
  170. }
  171. /**
  172. * Validates metadata for closure type tree
  173. *
  174. * @param ClassMetadata $meta
  175. * @param array $config
  176. * @throws InvalidMappingException
  177. * @return void
  178. */
  179. private function validateClosureTreeMetadata(ClassMetadata $meta, array $config)
  180. {
  181. $missingFields = array();
  182. if (!isset($config['parent'])) {
  183. $missingFields[] = 'ancestor';
  184. }
  185. if (!isset($config['closure'])) {
  186. $missingFields[] = 'closure class';
  187. }
  188. if ($missingFields) {
  189. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  190. }
  191. }
  192. }