Yaml.php 6.9 KB

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