Annotation.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace Gedmo\Tree\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AbstractAnnotationDriver,
  4. Gedmo\Exception\InvalidMappingException,
  5. Gedmo\Tree\Mapping\Validator;
  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 extends AbstractAnnotationDriver
  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. * Annotation to specify path class
  50. */
  51. const PATH = 'Gedmo\\Mapping\\Annotation\\TreePath';
  52. /**
  53. * Annotation to specify path source class
  54. */
  55. const PATH_SOURCE = 'Gedmo\\Mapping\\Annotation\\TreePathSource';
  56. /**
  57. * Annotation to mark the field to be used to hold the lock time
  58. */
  59. const LOCK_TIME = 'Gedmo\\Mapping\\Annotation\\TreeLockTime';
  60. /**
  61. * List of tree strategies available
  62. *
  63. * @var array
  64. */
  65. protected $strategies = array(
  66. 'nested',
  67. 'closure',
  68. 'materializedPath'
  69. );
  70. /**
  71. * {@inheritDoc}
  72. */
  73. public function readExtendedMetadata($meta, array &$config)
  74. {
  75. $validator = new Validator();
  76. $class = $this->getMetaReflectionClass($meta);
  77. // class annotations
  78. if ($annot = $this->reader->getClassAnnotation($class, self::TREE)) {
  79. if (!in_array($annot->type, $this->strategies)) {
  80. throw new InvalidMappingException("Tree type: {$annot->type} is not available.");
  81. }
  82. $config['strategy'] = $annot->type;
  83. $config['activate_locking'] = $annot->activateLocking;
  84. $config['locking_timeout'] = (int) $annot->lockingTimeout;
  85. if ($config['locking_timeout'] < 1) {
  86. throw new InvalidMappingException("Tree Locking Timeout must be at least of 1 second.");
  87. }
  88. }
  89. if ($annot = $this->reader->getClassAnnotation($class, self::CLOSURE)) {
  90. if (!class_exists($annot->class)) {
  91. throw new InvalidMappingException("Tree closure class: {$annot->class} does not exist.");
  92. }
  93. $config['closure'] = $annot->class;
  94. }
  95. // property annotations
  96. foreach ($class->getProperties() as $property) {
  97. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  98. $meta->isInheritedField($property->name) ||
  99. isset($meta->associationMappings[$property->name]['inherited'])
  100. ) {
  101. continue;
  102. }
  103. // left
  104. if ($this->reader->getPropertyAnnotation($property, self::LEFT)) {
  105. $field = $property->getName();
  106. if (!$meta->hasField($field)) {
  107. throw new InvalidMappingException("Unable to find 'left' - [{$field}] as mapped property in entity - {$meta->name}");
  108. }
  109. if (!$validator->isValidField($meta, $field)) {
  110. throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  111. }
  112. $config['left'] = $field;
  113. }
  114. // right
  115. if ($this->reader->getPropertyAnnotation($property, self::RIGHT)) {
  116. $field = $property->getName();
  117. if (!$meta->hasField($field)) {
  118. throw new InvalidMappingException("Unable to find 'right' - [{$field}] as mapped property in entity - {$meta->name}");
  119. }
  120. if (!$validator->isValidField($meta, $field)) {
  121. throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  122. }
  123. $config['right'] = $field;
  124. }
  125. // ancestor/parent
  126. if ($this->reader->getPropertyAnnotation($property, self::PARENT)) {
  127. $field = $property->getName();
  128. if (!$meta->isSingleValuedAssociation($field)) {
  129. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  130. }
  131. $config['parent'] = $field;
  132. }
  133. // root
  134. if ($this->reader->getPropertyAnnotation($property, self::ROOT)) {
  135. $field = $property->getName();
  136. if (!$meta->hasField($field)) {
  137. throw new InvalidMappingException("Unable to find 'root' - [{$field}] as mapped property in entity - {$meta->name}");
  138. }
  139. if (!$validator->isValidFieldForRoot($meta, $field)) {
  140. throw new InvalidMappingException("Tree root field - [{$field}] type is not valid and must be any of the 'integer' types or 'string' in class - {$meta->name}");
  141. }
  142. $config['root'] = $field;
  143. }
  144. // level
  145. if ($this->reader->getPropertyAnnotation($property, self::LEVEL)) {
  146. $field = $property->getName();
  147. if (!$meta->hasField($field)) {
  148. throw new InvalidMappingException("Unable to find 'level' - [{$field}] as mapped property in entity - {$meta->name}");
  149. }
  150. if (!$validator->isValidField($meta, $field)) {
  151. throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  152. }
  153. $config['level'] = $field;
  154. }
  155. // path
  156. if ($pathAnnotation = $this->reader->getPropertyAnnotation($property, self::PATH)) {
  157. $field = $property->getName();
  158. if (!$meta->hasField($field)) {
  159. throw new InvalidMappingException("Unable to find 'path' - [{$field}] as mapped property in entity - {$meta->name}");
  160. }
  161. if (!$validator->isValidFieldForPath($meta, $field)) {
  162. throw new InvalidMappingException("Tree Path field - [{$field}] type is not valid. It must be string or text in class - {$meta->name}");
  163. }
  164. if (strlen($pathAnnotation->separator) > 1) {
  165. throw new InvalidMappingException("Tree Path field - [{$field}] Separator {$pathAnnotation->separator} is invalid. It must be only one character long.");
  166. }
  167. $config['path'] = $field;
  168. $config['path_separator'] = $pathAnnotation->separator;
  169. }
  170. // path source
  171. if ($this->reader->getPropertyAnnotation($property, self::PATH_SOURCE)) {
  172. $field = $property->getName();
  173. if (!$meta->hasField($field)) {
  174. throw new InvalidMappingException("Unable to find 'path_source' - [{$field}] as mapped property in entity - {$meta->name}");
  175. }
  176. if (!$validator->isValidFieldForPathSource($meta, $field)) {
  177. throw new InvalidMappingException("Tree PathSource field - [{$field}] type is not valid. It can be any of the integer variants, double, float or string in class - {$meta->name}");
  178. }
  179. $config['path_source'] = $field;
  180. }
  181. // lock time
  182. if ($this->reader->getPropertyAnnotation($property, self::LOCK_TIME)) {
  183. $field = $property->getName();
  184. if (!$meta->hasField($field)) {
  185. throw new InvalidMappingException("Unable to find 'lock_time' - [{$field}] as mapped property in entity - {$meta->name}");
  186. }
  187. if (!$validator->isValidFieldForLockTime($meta, $field)) {
  188. throw new InvalidMappingException("Tree PathSource field - [{$field}] type is not valid. It must be \"date\" in class - {$meta->name}");
  189. }
  190. $config['lock_time'] = $field;
  191. }
  192. }
  193. if (isset($config['activate_locking']) && $config['activate_locking'] && !isset($config['lock_time'])) {
  194. throw new InvalidMappingException("You need to map a date field as the tree lock time field to activate locking support.");
  195. }
  196. if (!$meta->isMappedSuperclass && $config) {
  197. if (isset($config['strategy'])) {
  198. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  199. throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
  200. }
  201. $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
  202. $validator->$method($meta, $config);
  203. } else {
  204. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  205. }
  206. }
  207. }
  208. }