Annotation.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. * Annotation to mark field as child count
  51. */
  52. const ANNOTATION_CHILD_COUNT = 'Gedmo\Tree\Mapping\TreeChildCount';
  53. /**
  54. * List of types which are valid for tree fields
  55. *
  56. * @var array
  57. */
  58. private $validTypes = array(
  59. 'integer',
  60. 'smallint',
  61. 'bigint'
  62. );
  63. /**
  64. * List of tree strategies available
  65. *
  66. * @var array
  67. */
  68. private $strategies = array(
  69. 'nested',
  70. 'closure'
  71. );
  72. /**
  73. * {@inheritDoc}
  74. */
  75. public function validateFullMetadata(ClassMetadata $meta, array $config)
  76. {
  77. if (isset($config['strategy'])) {
  78. $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
  79. $this->$method($meta, $config);
  80. } elseif ($config) {
  81. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  82. }
  83. }
  84. /**
  85. * {@inheritDoc}
  86. */
  87. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  88. require_once __DIR__ . '/../Annotations.php';
  89. $reader = new AnnotationReader();
  90. $reader->setAnnotationNamespaceAlias('Gedmo\Tree\Mapping\\', 'gedmo');
  91. $class = $meta->getReflectionClass();
  92. // class annotations
  93. $classAnnotations = $reader->getClassAnnotations($class);
  94. if (isset($classAnnotations[self::ANNOTATION_TREE])) {
  95. $annot = $classAnnotations[self::ANNOTATION_TREE];
  96. if (!in_array($annot->type, $this->strategies)) {
  97. throw new InvalidMappingException("Tree type: {$annot->type} is not available.");
  98. }
  99. $config['strategy'] = $annot->type;
  100. }
  101. if (isset($classAnnotations[self::ANNOTATION_CLOSURE])) {
  102. $annot = $classAnnotations[self::ANNOTATION_CLOSURE];
  103. if (!class_exists($annot->class)) {
  104. throw new InvalidMappingException("Tree closure class: {$annot->class} does not exist.");
  105. }
  106. $config['closure'] = $annot->class;
  107. }
  108. // property annotations
  109. foreach ($class->getProperties() as $property) {
  110. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  111. $meta->isInheritedField($property->name) ||
  112. isset($meta->associationMappings[$property->name]['inherited'])
  113. ) {
  114. continue;
  115. }
  116. // left
  117. if ($left = $reader->getPropertyAnnotation($property, self::ANNOTATION_LEFT)) {
  118. $field = $property->getName();
  119. if (!$meta->hasField($field)) {
  120. throw new InvalidMappingException("Unable to find 'left' - [{$field}] as mapped property in entity - {$meta->name}");
  121. }
  122. if (!$this->isValidField($meta, $field)) {
  123. throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  124. }
  125. $config['left'] = $field;
  126. }
  127. // right
  128. if ($right = $reader->getPropertyAnnotation($property, self::ANNOTATION_RIGHT)) {
  129. $field = $property->getName();
  130. if (!$meta->hasField($field)) {
  131. throw new InvalidMappingException("Unable to find 'right' - [{$field}] as mapped property in entity - {$meta->name}");
  132. }
  133. if (!$this->isValidField($meta, $field)) {
  134. throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  135. }
  136. $config['right'] = $field;
  137. }
  138. // ancestor/parent
  139. if ($parent = $reader->getPropertyAnnotation($property, self::ANNOTATION_PARENT)) {
  140. $field = $property->getName();
  141. if (!$meta->isSingleValuedAssociation($field)) {
  142. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  143. }
  144. $config['parent'] = $field;
  145. }
  146. // root
  147. if ($root = $reader->getPropertyAnnotation($property, self::ANNOTATION_ROOT)) {
  148. $field = $property->getName();
  149. if (!$meta->hasField($field)) {
  150. throw new InvalidMappingException("Unable to find 'root' - [{$field}] as mapped property in entity - {$meta->name}");
  151. }
  152. if (!$this->isValidField($meta, $field)) {
  153. throw new InvalidMappingException("Tree root field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  154. }
  155. $config['root'] = $field;
  156. }
  157. // level
  158. if ($parent = $reader->getPropertyAnnotation($property, self::ANNOTATION_LEVEL)) {
  159. $field = $property->getName();
  160. if (!$meta->hasField($field)) {
  161. throw new InvalidMappingException("Unable to find 'level' - [{$field}] as mapped property in entity - {$meta->name}");
  162. }
  163. if (!$this->isValidField($meta, $field)) {
  164. throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  165. }
  166. $config['level'] = $field;
  167. }
  168. // child count
  169. if ($childCount = $reader->getPropertyAnnotation($property, self::ANNOTATION_CHILD_COUNT)) {
  170. $field = $property->getName();
  171. if (!$meta->hasField($field)) {
  172. throw new InvalidMappingException("Unable to find 'childCount' - [{$field}] as mapped property in entity - {$meta->name}");
  173. }
  174. if (!$this->isValidField($meta, $field)) {
  175. throw new InvalidMappingException("Tree childCount field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  176. }
  177. $config['childCount'] = $field;
  178. }
  179. }
  180. }
  181. /**
  182. * Checks if $field type is valid
  183. *
  184. * @param ClassMetadata $meta
  185. * @param string $field
  186. * @return boolean
  187. */
  188. protected function isValidField(ClassMetadata $meta, $field)
  189. {
  190. $mapping = $meta->getFieldMapping($field);
  191. return $mapping && in_array($mapping['type'], $this->validTypes);
  192. }
  193. /**
  194. * Validates metadata for nested type tree
  195. *
  196. * @param ClassMetadata $meta
  197. * @param array $config
  198. * @throws InvalidMappingException
  199. * @return void
  200. */
  201. private function validateNestedTreeMetadata(ClassMetadata $meta, array $config)
  202. {
  203. $missingFields = array();
  204. if (!isset($config['parent'])) {
  205. $missingFields[] = 'ancestor';
  206. }
  207. if (!isset($config['left'])) {
  208. $missingFields[] = 'left';
  209. }
  210. if (!isset($config['right'])) {
  211. $missingFields[] = 'right';
  212. }
  213. if ($missingFields) {
  214. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  215. }
  216. }
  217. /**
  218. * Validates metadata for closure type tree
  219. *
  220. * @param ClassMetadata $meta
  221. * @param array $config
  222. * @throws InvalidMappingException
  223. * @return void
  224. */
  225. private function validateClosureTreeMetadata(ClassMetadata $meta, array $config)
  226. {
  227. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  228. throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
  229. }
  230. $missingFields = array();
  231. if (!isset($config['parent'])) {
  232. $missingFields[] = 'ancestor';
  233. }
  234. if (!isset($config['closure'])) {
  235. $missingFields[] = 'closure class';
  236. }
  237. if ($missingFields) {
  238. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  239. }
  240. }
  241. }