Annotation.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 TREE = 'Gedmo\\Mapping\\Annotation\\Tree';
  25. /**
  26. * Annotation to mark field as one which will store left value
  27. */
  28. const LEFT = 'Gedmo\\Mapping\\Annotation\\TreeLeft';
  29. /**
  30. * Annotation to mark field as one which will store right value
  31. */
  32. const RIGHT = 'Gedmo\\Mapping\\Annotation\\TreeRight';
  33. /**
  34. * Annotation to mark relative parent field
  35. */
  36. const PARENT = 'Gedmo\\Mapping\\Annotation\\TreeParent';
  37. /**
  38. * Annotation to mark node level
  39. */
  40. const LEVEL = 'Gedmo\\Mapping\\Annotation\\TreeLevel';
  41. /**
  42. * Annotation to mark field as tree root
  43. */
  44. const ROOT = 'Gedmo\\Mapping\\Annotation\\TreeRoot';
  45. /**
  46. * Annotation to specify closure tree class
  47. */
  48. const CLOSURE = 'Gedmo\\Mapping\\Annotation\\TreeClosure';
  49. /**
  50. * Annotation to mark field as child count
  51. */
  52. const CHILD_COUNT = 'Gedmo\\Mapping\\Annotation\\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. $reader = new AnnotationReader();
  92. $reader->setAnnotationNamespaceAlias('Gedmo\\Mapping\\Annotation\\', 'gedmo');
  93. $reader->setAutoloadAnnotations(true);
  94. $class = $meta->getReflectionClass();
  95. // class annotations
  96. $classAnnotations = $reader->getClassAnnotations($class);
  97. if (isset($classAnnotations[self::TREE])) {
  98. $annot = $classAnnotations[self::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::CLOSURE])) {
  105. $annot = $classAnnotations[self::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::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::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::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::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. $mapping = $meta->getFieldMapping($field);
  159. if (!$mapping['nullable']) {
  160. throw new InvalidMappingException("Tree root field - [{$field}] must be nullable in class - {$meta->name}");
  161. }
  162. $config['root'] = $field;
  163. }
  164. // level
  165. if ($parent = $reader->getPropertyAnnotation($property, self::LEVEL)) {
  166. $field = $property->getName();
  167. if (!$meta->hasField($field)) {
  168. throw new InvalidMappingException("Unable to find 'level' - [{$field}] as mapped property in entity - {$meta->name}");
  169. }
  170. if (!$this->isValidField($meta, $field)) {
  171. throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  172. }
  173. $config['level'] = $field;
  174. }
  175. // child count
  176. if ($childCount = $reader->getPropertyAnnotation($property, self::CHILD_COUNT)) {
  177. $field = $property->getName();
  178. if (!$meta->hasField($field)) {
  179. throw new InvalidMappingException("Unable to find 'childCount' - [{$field}] as mapped property in entity - {$meta->name}");
  180. }
  181. if (!$this->isValidField($meta, $field)) {
  182. throw new InvalidMappingException("Tree childCount field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  183. }
  184. $config['childCount'] = $field;
  185. }
  186. }
  187. }
  188. /**
  189. * Checks if $field type is valid
  190. *
  191. * @param ClassMetadata $meta
  192. * @param string $field
  193. * @return boolean
  194. */
  195. protected function isValidField(ClassMetadata $meta, $field)
  196. {
  197. $mapping = $meta->getFieldMapping($field);
  198. return $mapping && in_array($mapping['type'], $this->validTypes);
  199. }
  200. /**
  201. * Validates metadata for nested type tree
  202. *
  203. * @param ClassMetadata $meta
  204. * @param array $config
  205. * @throws InvalidMappingException
  206. * @return void
  207. */
  208. private function validateNestedTreeMetadata(ClassMetadata $meta, array $config)
  209. {
  210. $missingFields = array();
  211. if (!isset($config['parent'])) {
  212. $missingFields[] = 'ancestor';
  213. }
  214. if (!isset($config['left'])) {
  215. $missingFields[] = 'left';
  216. }
  217. if (!isset($config['right'])) {
  218. $missingFields[] = 'right';
  219. }
  220. if ($missingFields) {
  221. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  222. }
  223. }
  224. /**
  225. * Validates metadata for closure type tree
  226. *
  227. * @param ClassMetadata $meta
  228. * @param array $config
  229. * @throws InvalidMappingException
  230. * @return void
  231. */
  232. private function validateClosureTreeMetadata(ClassMetadata $meta, array $config)
  233. {
  234. $missingFields = array();
  235. if (!isset($config['parent'])) {
  236. $missingFields[] = 'ancestor';
  237. }
  238. if (!isset($config['closure'])) {
  239. $missingFields[] = 'closure class';
  240. }
  241. if ($missingFields) {
  242. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  243. }
  244. }
  245. }