Annotation.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace Gedmo\Tree\Mapping\Driver;
  3. use Gedmo\Mapping\Driver,
  4. Doctrine\Common\Annotations\AnnotationReader,
  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 Driver
  19. {
  20. /**
  21. * Annotation to define the tree type
  22. */
  23. const ANNOTATION_TREE = 'Gedmo\Tree\Mapping\Tree';
  24. /**
  25. * Annotation to mark field as one which will store left value
  26. */
  27. const ANNOTATION_LEFT = 'Gedmo\Tree\Mapping\TreeLeft';
  28. /**
  29. * Annotation to mark field as one which will store right value
  30. */
  31. const ANNOTATION_RIGHT = 'Gedmo\Tree\Mapping\TreeRight';
  32. /**
  33. * Annotation to mark relative parent field
  34. */
  35. const ANNOTATION_PARENT = 'Gedmo\Tree\Mapping\TreeParent';
  36. /**
  37. * Annotation to mark node level
  38. */
  39. const ANNOTATION_LEVEL = 'Gedmo\Tree\Mapping\TreeLevel';
  40. /**
  41. * Annotation to mark field as tree root
  42. */
  43. const ANNOTATION_ROOT = 'Gedmo\Tree\Mapping\TreeRoot';
  44. /**
  45. * List of types which are valid for tree fields
  46. *
  47. * @var array
  48. */
  49. private $validTypes = array(
  50. 'integer',
  51. 'smallint',
  52. 'bigint'
  53. );
  54. /**
  55. * List of tree strategies available
  56. *
  57. * @var array
  58. */
  59. private $strategies = array(
  60. 'nested'
  61. );
  62. /**
  63. * {@inheritDoc}
  64. */
  65. public function validateFullMetadata($meta, array $config)
  66. {
  67. if (isset($config['strategy'])) {
  68. $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
  69. $this->$method($meta, $config);
  70. } elseif ($config) {
  71. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  72. }
  73. }
  74. /**
  75. * {@inheritDoc}
  76. */
  77. public function readExtendedMetadata($meta, array &$config) {
  78. require_once __DIR__ . '/../Annotations.php';
  79. $reader = new AnnotationReader();
  80. $reader->setAnnotationNamespaceAlias('Gedmo\Tree\Mapping\\', 'gedmo');
  81. $class = $meta->getReflectionClass();
  82. // class annotations
  83. $classAnnotations = $reader->getClassAnnotations($class);
  84. if (isset($classAnnotations[self::ANNOTATION_TREE])) {
  85. $annot = $classAnnotations[self::ANNOTATION_TREE];
  86. if (!in_array($annot->type, $this->strategies)) {
  87. throw new InvalidMappingException("Tree type: {$annot->type} is not available.");
  88. }
  89. $config['strategy'] = $annot->type;
  90. }
  91. // property annotations
  92. foreach ($class->getProperties() as $property) {
  93. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  94. $meta->isInheritedField($property->name) ||
  95. isset($meta->associationMappings[$property->name]['inherited'])
  96. ) {
  97. continue;
  98. }
  99. // left
  100. if ($left = $reader->getPropertyAnnotation($property, self::ANNOTATION_LEFT)) {
  101. $field = $property->getName();
  102. if (!$meta->hasField($field)) {
  103. throw new InvalidMappingException("Unable to find 'left' - [{$field}] as mapped property in entity - {$meta->name}");
  104. }
  105. if (!$this->isValidField($meta, $field)) {
  106. throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  107. }
  108. $config['left'] = $field;
  109. }
  110. // right
  111. if ($right = $reader->getPropertyAnnotation($property, self::ANNOTATION_RIGHT)) {
  112. $field = $property->getName();
  113. if (!$meta->hasField($field)) {
  114. throw new InvalidMappingException("Unable to find 'right' - [{$field}] as mapped property in entity - {$meta->name}");
  115. }
  116. if (!$this->isValidField($meta, $field)) {
  117. throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  118. }
  119. $config['right'] = $field;
  120. }
  121. // ancestor/parent
  122. if ($parent = $reader->getPropertyAnnotation($property, self::ANNOTATION_PARENT)) {
  123. $field = $property->getName();
  124. if (!$meta->isSingleValuedAssociation($field)) {
  125. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  126. }
  127. $config['parent'] = $field;
  128. }
  129. // root
  130. if ($root = $reader->getPropertyAnnotation($property, self::ANNOTATION_ROOT)) {
  131. $field = $property->getName();
  132. if (!$meta->hasField($field)) {
  133. throw new InvalidMappingException("Unable to find 'root' - [{$field}] as mapped property in entity - {$meta->name}");
  134. }
  135. if (!$this->isValidField($meta, $field)) {
  136. throw new InvalidMappingException("Tree root field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  137. }
  138. $config['root'] = $field;
  139. }
  140. // level
  141. if ($parent = $reader->getPropertyAnnotation($property, self::ANNOTATION_LEVEL)) {
  142. $field = $property->getName();
  143. if (!$meta->hasField($field)) {
  144. throw new InvalidMappingException("Unable to find 'level' - [{$field}] as mapped property in entity - {$meta->name}");
  145. }
  146. if (!$this->isValidField($meta, $field)) {
  147. throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  148. }
  149. $config['level'] = $field;
  150. }
  151. }
  152. }
  153. /**
  154. * Checks if $field type is valid
  155. *
  156. * @param ClassMetadataInfo $meta
  157. * @param string $field
  158. * @return boolean
  159. */
  160. protected function isValidField($meta, $field)
  161. {
  162. $mapping = $meta->getFieldMapping($field);
  163. return $mapping && in_array($mapping['type'], $this->validTypes);
  164. }
  165. /**
  166. * Validates metadata for nested type tree
  167. *
  168. * @param ClassMetadataInfo $meta
  169. * @param array $config
  170. * @throws InvalidMappingException
  171. * @return void
  172. */
  173. private function validateNestedTreeMetadata($meta, array $config)
  174. {
  175. $missingFields = array();
  176. if (!isset($config['parent'])) {
  177. $missingFields[] = 'ancestor';
  178. }
  179. if (!isset($config['left'])) {
  180. $missingFields[] = 'left';
  181. }
  182. if (!isset($config['right'])) {
  183. $missingFields[] = 'right';
  184. }
  185. if ($missingFields) {
  186. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  187. }
  188. }
  189. }