Annotation.php 8.7 KB

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