Annotation.php 8.7 KB

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