Annotation.php 9.6 KB

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