Annotation.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace Gedmo\Tree\Mapping\Driver;
  3. use Gedmo\Mapping\Driver,
  4. Doctrine\Common\Annotations\AnnotationReader,
  5. Doctrine\Common\Persistence\Mapping\ClassMetadata,
  6. Gedmo\Exception\InvalidMappingException;
  7. /**
  8. * This is an annotation mapping driver for Tree
  9. * behavioral extension. Used for extraction of extended
  10. * metadata from Annotations specificaly for Tree
  11. * extension.
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @package Gedmo.Tree.Mapping.Driver
  15. * @subpackage Annotation
  16. * @link http://www.gediminasm.org
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. class Annotation implements Driver
  20. {
  21. /**
  22. * Annotation to define the tree type
  23. */
  24. const ANNOTATION_TREE = 'Gedmo\Tree\Mapping\Tree';
  25. /**
  26. * Annotation to mark field as one which will store left value
  27. */
  28. const ANNOTATION_LEFT = 'Gedmo\Tree\Mapping\TreeLeft';
  29. /**
  30. * Annotation to mark field as one which will store right value
  31. */
  32. const ANNOTATION_RIGHT = 'Gedmo\Tree\Mapping\TreeRight';
  33. /**
  34. * Annotation to mark relative parent field
  35. */
  36. const ANNOTATION_PARENT = 'Gedmo\Tree\Mapping\TreeParent';
  37. /**
  38. * Annotation to mark node level
  39. */
  40. const ANNOTATION_LEVEL = 'Gedmo\Tree\Mapping\TreeLevel';
  41. /**
  42. * Annotation to mark field as tree root
  43. */
  44. const ANNOTATION_ROOT = 'Gedmo\Tree\Mapping\TreeRoot';
  45. /**
  46. * Annotation to specify closure tree class
  47. */
  48. const ANNOTATION_CLOSURE = 'Gedmo\Tree\Mapping\TreeClosure';
  49. /**
  50. * List of types which are valid for tree fields
  51. *
  52. * @var array
  53. */
  54. private $validTypes = array(
  55. 'integer',
  56. 'smallint',
  57. 'bigint'
  58. );
  59. /**
  60. * List of tree strategies available
  61. *
  62. * @var array
  63. */
  64. private $strategies = array(
  65. 'nested',
  66. 'closure'
  67. );
  68. /**
  69. * {@inheritDoc}
  70. */
  71. public function validateFullMetadata(ClassMetadata $meta, array $config)
  72. {
  73. if (isset($config['strategy'])) {
  74. $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
  75. $this->$method($meta, $config);
  76. } elseif ($config) {
  77. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  78. }
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  84. require_once __DIR__ . '/../Annotations.php';
  85. $reader = new AnnotationReader();
  86. $reader->setAnnotationNamespaceAlias('Gedmo\Tree\Mapping\\', 'gedmo');
  87. $class = $meta->getReflectionClass();
  88. // class annotations
  89. $classAnnotations = $reader->getClassAnnotations($class);
  90. if (isset($classAnnotations[self::ANNOTATION_TREE])) {
  91. $annot = $classAnnotations[self::ANNOTATION_TREE];
  92. if (!in_array($annot->type, $this->strategies)) {
  93. throw new InvalidMappingException("Tree type: {$annot->type} is not available.");
  94. }
  95. $config['strategy'] = $annot->type;
  96. }
  97. if (isset($classAnnotations[self::ANNOTATION_CLOSURE])) {
  98. $annot = $classAnnotations[self::ANNOTATION_CLOSURE];
  99. if (!class_exists($annot->class)) {
  100. throw new InvalidMappingException("Tree closure class: {$annot->class} does not exist.");
  101. }
  102. $config['closure'] = $annot->class;
  103. }
  104. // property annotations
  105. foreach ($class->getProperties() as $property) {
  106. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  107. $meta->isInheritedField($property->name) ||
  108. isset($meta->associationMappings[$property->name]['inherited'])
  109. ) {
  110. continue;
  111. }
  112. // left
  113. if ($left = $reader->getPropertyAnnotation($property, self::ANNOTATION_LEFT)) {
  114. $field = $property->getName();
  115. if (!$meta->hasField($field)) {
  116. throw new InvalidMappingException("Unable to find 'left' - [{$field}] as mapped property in entity - {$meta->name}");
  117. }
  118. if (!$this->isValidField($meta, $field)) {
  119. throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  120. }
  121. $config['left'] = $field;
  122. }
  123. // right
  124. if ($right = $reader->getPropertyAnnotation($property, self::ANNOTATION_RIGHT)) {
  125. $field = $property->getName();
  126. if (!$meta->hasField($field)) {
  127. throw new InvalidMappingException("Unable to find 'right' - [{$field}] as mapped property in entity - {$meta->name}");
  128. }
  129. if (!$this->isValidField($meta, $field)) {
  130. throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  131. }
  132. $config['right'] = $field;
  133. }
  134. // ancestor/parent
  135. if ($parent = $reader->getPropertyAnnotation($property, self::ANNOTATION_PARENT)) {
  136. $field = $property->getName();
  137. if (!$meta->isSingleValuedAssociation($field)) {
  138. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  139. }
  140. $config['parent'] = $field;
  141. }
  142. // root
  143. if ($root = $reader->getPropertyAnnotation($property, self::ANNOTATION_ROOT)) {
  144. $field = $property->getName();
  145. if (!$meta->hasField($field)) {
  146. throw new InvalidMappingException("Unable to find 'root' - [{$field}] as mapped property in entity - {$meta->name}");
  147. }
  148. if (!$this->isValidField($meta, $field)) {
  149. throw new InvalidMappingException("Tree root field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  150. }
  151. $config['root'] = $field;
  152. }
  153. // level
  154. if ($parent = $reader->getPropertyAnnotation($property, self::ANNOTATION_LEVEL)) {
  155. $field = $property->getName();
  156. if (!$meta->hasField($field)) {
  157. throw new InvalidMappingException("Unable to find 'level' - [{$field}] as mapped property in entity - {$meta->name}");
  158. }
  159. if (!$this->isValidField($meta, $field)) {
  160. throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  161. }
  162. $config['level'] = $field;
  163. }
  164. }
  165. }
  166. /**
  167. * Checks if $field type is valid
  168. *
  169. * @param ClassMetadata $meta
  170. * @param string $field
  171. * @return boolean
  172. */
  173. protected function isValidField(ClassMetadata $meta, $field)
  174. {
  175. $mapping = $meta->getFieldMapping($field);
  176. return $mapping && in_array($mapping['type'], $this->validTypes);
  177. }
  178. /**
  179. * Validates metadata for nested type tree
  180. *
  181. * @param ClassMetadata $meta
  182. * @param array $config
  183. * @throws InvalidMappingException
  184. * @return void
  185. */
  186. private function validateNestedTreeMetadata(ClassMetadata $meta, array $config)
  187. {
  188. $missingFields = array();
  189. if (!isset($config['parent'])) {
  190. $missingFields[] = 'ancestor';
  191. }
  192. if (!isset($config['left'])) {
  193. $missingFields[] = 'left';
  194. }
  195. if (!isset($config['right'])) {
  196. $missingFields[] = 'right';
  197. }
  198. if ($missingFields) {
  199. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  200. }
  201. }
  202. /**
  203. * Validates metadata for closure type tree
  204. *
  205. * @param ClassMetadata $meta
  206. * @param array $config
  207. * @throws InvalidMappingException
  208. * @return void
  209. */
  210. private function validateClosureTreeMetadata(ClassMetadata $meta, array $config)
  211. {
  212. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  213. throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
  214. }
  215. $missingFields = array();
  216. if (!isset($config['parent'])) {
  217. $missingFields[] = 'ancestor';
  218. }
  219. if (!isset($config['closure'])) {
  220. $missingFields[] = 'closure class';
  221. }
  222. if ($missingFields) {
  223. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  224. }
  225. }
  226. }