Yaml.php 7.4 KB

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