Annotation.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. * Annotation to specify path class
  49. */
  50. const PATH = 'Gedmo\\Mapping\\Annotation\\TreePath';
  51. /**
  52. * Annotation to specify path source class
  53. */
  54. const PATH_SOURCE = 'Gedmo\\Mapping\\Annotation\\TreePathSource';
  55. /**
  56. * List of types which are valid for tree fields
  57. *
  58. * @var array
  59. */
  60. private $validTypes = array(
  61. 'integer',
  62. 'smallint',
  63. 'bigint',
  64. 'int'
  65. );
  66. /**
  67. * List of types which are valid for the path (materialized path strategy)
  68. *
  69. * @var array
  70. */
  71. private $validPathTypes = array(
  72. 'string',
  73. 'text'
  74. );
  75. /**
  76. * List of types which are valid for the path source (materialized path strategy)
  77. *
  78. * @var array
  79. */
  80. private $validPathSourceTypes = array(
  81. 'id',
  82. 'integer',
  83. 'smallint',
  84. 'bigint',
  85. 'string',
  86. 'int',
  87. 'float'
  88. );
  89. /**
  90. * List of tree strategies available
  91. *
  92. * @var array
  93. */
  94. private $strategies = array(
  95. 'nested',
  96. 'closure',
  97. 'materializedPath'
  98. );
  99. /**
  100. * Annotation reader instance
  101. *
  102. * @var object
  103. */
  104. private $reader;
  105. /**
  106. * original driver if it is available
  107. */
  108. protected $_originalDriver = null;
  109. /**
  110. * {@inheritDoc}
  111. */
  112. public function setAnnotationReader($reader)
  113. {
  114. $this->reader = $reader;
  115. }
  116. /**
  117. * {@inheritDoc}
  118. */
  119. public function readExtendedMetadata($meta, array &$config)
  120. {
  121. $class = $meta->getReflectionClass();
  122. if (!$class) {
  123. // based on recent doctrine 2.3.0-DEV maybe will be fixed in some way
  124. // this happens when running annotation driver in combination with
  125. // static reflection services. This is not the nicest fix
  126. $class = new \ReflectionClass($meta->name);
  127. }
  128. // class annotations
  129. if ($annot = $this->reader->getClassAnnotation($class, self::TREE)) {
  130. if (!in_array($annot->type, $this->strategies)) {
  131. throw new InvalidMappingException("Tree type: {$annot->type} is not available.");
  132. }
  133. $config['strategy'] = $annot->type;
  134. }
  135. if ($annot = $this->reader->getClassAnnotation($class, self::CLOSURE)) {
  136. if (!class_exists($annot->class)) {
  137. throw new InvalidMappingException("Tree closure class: {$annot->class} does not exist.");
  138. }
  139. $config['closure'] = $annot->class;
  140. }
  141. // property annotations
  142. foreach ($class->getProperties() as $property) {
  143. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  144. $meta->isInheritedField($property->name) ||
  145. isset($meta->associationMappings[$property->name]['inherited'])
  146. ) {
  147. continue;
  148. }
  149. // left
  150. if ($this->reader->getPropertyAnnotation($property, self::LEFT)) {
  151. $field = $property->getName();
  152. if (!$meta->hasField($field)) {
  153. throw new InvalidMappingException("Unable to find 'left' - [{$field}] as mapped property in entity - {$meta->name}");
  154. }
  155. if (!$this->isValidField($meta, $field)) {
  156. throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  157. }
  158. $config['left'] = $field;
  159. }
  160. // right
  161. if ($this->reader->getPropertyAnnotation($property, self::RIGHT)) {
  162. $field = $property->getName();
  163. if (!$meta->hasField($field)) {
  164. throw new InvalidMappingException("Unable to find 'right' - [{$field}] as mapped property in entity - {$meta->name}");
  165. }
  166. if (!$this->isValidField($meta, $field)) {
  167. throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  168. }
  169. $config['right'] = $field;
  170. }
  171. // ancestor/parent
  172. if ($this->reader->getPropertyAnnotation($property, self::PARENT)) {
  173. $field = $property->getName();
  174. if (!$meta->isSingleValuedAssociation($field)) {
  175. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  176. }
  177. $config['parent'] = $field;
  178. }
  179. // root
  180. if ($this->reader->getPropertyAnnotation($property, self::ROOT)) {
  181. $field = $property->getName();
  182. if (!$meta->hasField($field)) {
  183. throw new InvalidMappingException("Unable to find 'root' - [{$field}] as mapped property in entity - {$meta->name}");
  184. }
  185. if (!$meta->getFieldMapping($field)) {
  186. throw new InvalidMappingException("Tree root field - [{$field}] type is not valid in class - {$meta->name}");
  187. }
  188. $config['root'] = $field;
  189. }
  190. // level
  191. if ($this->reader->getPropertyAnnotation($property, self::LEVEL)) {
  192. $field = $property->getName();
  193. if (!$meta->hasField($field)) {
  194. throw new InvalidMappingException("Unable to find 'level' - [{$field}] as mapped property in entity - {$meta->name}");
  195. }
  196. if (!$this->isValidField($meta, $field)) {
  197. throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  198. }
  199. $config['level'] = $field;
  200. }
  201. // path
  202. if ($pathAnnotation = $this->reader->getPropertyAnnotation($property, self::PATH)) {
  203. $field = $property->getName();
  204. if (!$meta->hasField($field)) {
  205. throw new InvalidMappingException("Unable to find 'path' - [{$field}] as mapped property in entity - {$meta->name}");
  206. }
  207. if (!$this->isValidFieldForPath($meta, $field)) {
  208. throw new InvalidMappingException("Tree Path field - [{$field}] type is not valid. It must be string or text in class - {$meta->name}");
  209. }
  210. if (strlen($pathAnnotation->separator) > 1) {
  211. throw new InvalidMappingException("Tree Path field - [{$field}] Separator {$pathAnnotation->separator} is invalid. It must be only one character long.");
  212. }
  213. $config['path'] = $field;
  214. $config['path_separator'] = $pathAnnotation->separator;
  215. }
  216. // path source
  217. if ($this->reader->getPropertyAnnotation($property, self::PATH_SOURCE)) {
  218. $field = $property->getName();
  219. if (!$meta->hasField($field)) {
  220. throw new InvalidMappingException("Unable to find 'path_source' - [{$field}] as mapped property in entity - {$meta->name}");
  221. }
  222. if (!$this->isValidFieldForPathSource($meta, $field)) {
  223. 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}");
  224. }
  225. $config['path_source'] = $field;
  226. }
  227. }
  228. if (!$meta->isMappedSuperclass && $config) {
  229. if (isset($config['strategy'])) {
  230. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  231. throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
  232. }
  233. $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
  234. $this->$method($meta, $config);
  235. } else {
  236. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  237. }
  238. }
  239. }
  240. /**
  241. * Checks if $field type is valid
  242. *
  243. * @param object $meta
  244. * @param string $field
  245. * @return boolean
  246. */
  247. protected function isValidField($meta, $field)
  248. {
  249. $mapping = $meta->getFieldMapping($field);
  250. return $mapping && in_array($mapping['type'], $this->validTypes);
  251. }
  252. /**
  253. * Checks if $field type is valid for Path field
  254. *
  255. * @param object $meta
  256. * @param string $field
  257. * @return boolean
  258. */
  259. protected function isValidFieldForPath($meta, $field)
  260. {
  261. $mapping = $meta->getFieldMapping($field);
  262. return $mapping && in_array($mapping['type'], $this->validPathTypes);
  263. }
  264. /**
  265. * Checks if $field type is valid for PathSource field
  266. *
  267. * @param object $meta
  268. * @param string $field
  269. * @return boolean
  270. */
  271. protected function isValidFieldForPathSource($meta, $field)
  272. {
  273. $mapping = $meta->getFieldMapping($field);
  274. return $mapping && in_array($mapping['type'], $this->validPathSourceTypes);
  275. }
  276. /**
  277. * Validates metadata for nested type tree
  278. *
  279. * @param object $meta
  280. * @param array $config
  281. * @throws InvalidMappingException
  282. * @return void
  283. */
  284. private function validateNestedTreeMetadata($meta, array $config)
  285. {
  286. $missingFields = array();
  287. if (!isset($config['parent'])) {
  288. $missingFields[] = 'ancestor';
  289. }
  290. if (!isset($config['left'])) {
  291. $missingFields[] = 'left';
  292. }
  293. if (!isset($config['right'])) {
  294. $missingFields[] = 'right';
  295. }
  296. if ($missingFields) {
  297. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  298. }
  299. }
  300. /**
  301. * Validates metadata for closure type tree
  302. *
  303. * @param object $meta
  304. * @param array $config
  305. * @throws InvalidMappingException
  306. * @return void
  307. */
  308. private function validateClosureTreeMetadata($meta, array $config)
  309. {
  310. $missingFields = array();
  311. if (!isset($config['parent'])) {
  312. $missingFields[] = 'ancestor';
  313. }
  314. if (!isset($config['closure'])) {
  315. $missingFields[] = 'closure class';
  316. }
  317. if ($missingFields) {
  318. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  319. }
  320. }
  321. /**
  322. * Validates metadata for materialized path type tree
  323. *
  324. * @param object $meta
  325. * @param array $config
  326. * @throws InvalidMappingException
  327. * @return void
  328. */
  329. private function validateMaterializedPathTreeMetadata($meta, array $config)
  330. {
  331. $missingFields = array();
  332. if (!isset($config['parent'])) {
  333. $missingFields[] = 'ancestor';
  334. }
  335. if (!isset($config['path'])) {
  336. $missingFields[] = 'path';
  337. }
  338. if (!isset($config['path_source'])) {
  339. $missingFields[] = 'path_source';
  340. }
  341. if ($missingFields) {
  342. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  343. }
  344. }
  345. /**
  346. * Passes in the mapping read by original driver
  347. *
  348. * @param $driver
  349. * @return void
  350. */
  351. public function setOriginalDriver($driver)
  352. {
  353. $this->_originalDriver = $driver;
  354. }
  355. }