Yaml.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. {
  65. $mapping = $this->_getMapping($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. }
  107. }
  108. }
  109. }
  110. if (isset($mapping['manyToOne'])) {
  111. foreach ($mapping['manyToOne'] as $field => $relationMapping) {
  112. if (isset($relationMapping['gedmo'])) {
  113. if (in_array('treeParent', $relationMapping['gedmo'])) {
  114. if ($relationMapping['targetEntity'] != $meta->name) {
  115. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  116. }
  117. $config['parent'] = $field;
  118. }
  119. }
  120. }
  121. }
  122. }
  123. /**
  124. * {@inheritDoc}
  125. */
  126. protected function _loadMappingFile($file)
  127. {
  128. return \Symfony\Component\Yaml\Yaml::load($file);
  129. }
  130. /**
  131. * Checks if $field type is valid
  132. *
  133. * @param ClassMetadata $meta
  134. * @param string $field
  135. * @return boolean
  136. */
  137. protected function isValidField(ClassMetadata $meta, $field)
  138. {
  139. $mapping = $meta->getFieldMapping($field);
  140. return $mapping && in_array($mapping['type'], $this->validTypes);
  141. }
  142. /**
  143. * Validates metadata for nested type tree
  144. *
  145. * @param ClassMetadata $meta
  146. * @param array $config
  147. * @throws InvalidMappingException
  148. * @return void
  149. */
  150. private function validateNestedTreeMetadata(ClassMetadata $meta, array $config)
  151. {
  152. $missingFields = array();
  153. if (!isset($config['parent'])) {
  154. $missingFields[] = 'ancestor';
  155. }
  156. if (!isset($config['left'])) {
  157. $missingFields[] = 'left';
  158. }
  159. if (!isset($config['right'])) {
  160. $missingFields[] = 'right';
  161. }
  162. if ($missingFields) {
  163. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  164. }
  165. }
  166. /**
  167. * Validates metadata for closure type tree
  168. *
  169. * @param ClassMetadata $meta
  170. * @param array $config
  171. * @throws InvalidMappingException
  172. * @return void
  173. */
  174. private function validateClosureTreeMetadata(ClassMetadata $meta, array $config)
  175. {
  176. $missingFields = array();
  177. if (!isset($config['parent'])) {
  178. $missingFields[] = 'ancestor';
  179. }
  180. if (!isset($config['closure'])) {
  181. $missingFields[] = 'closure class';
  182. }
  183. if ($missingFields) {
  184. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  185. }
  186. }
  187. }