Annotation.php 8.6 KB

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