Annotation.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace Gedmo\Tree\Mapping\Driver;
  3. use Gedmo\Mapping\Driver,
  4. Doctrine\Common\Annotations\AnnotationReader,
  5. Gedmo\Exception\InvalidArgumentException;
  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 mark field as one which will store left value
  22. */
  23. const ANNOTATION_LEFT = 'Gedmo\Tree\Mapping\TreeLeft';
  24. /**
  25. * Annotation to mark field as one which will store right value
  26. */
  27. const ANNOTATION_RIGHT = 'Gedmo\Tree\Mapping\TreeRight';
  28. /**
  29. * Annotation to mark relative parent field
  30. */
  31. const ANNOTATION_PARENT = 'Gedmo\Tree\Mapping\TreeParent';
  32. /**
  33. * Annotation to mark node level
  34. */
  35. const ANNOTATION_LEVEL = 'Gedmo\Tree\Mapping\TreeLevel';
  36. /**
  37. * List of types which are valid for timestamp
  38. *
  39. * @var array
  40. */
  41. private $_validTypes = array(
  42. 'integer',
  43. 'smallint',
  44. 'bigint'
  45. );
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public function validateFullMetadata($meta, array $config)
  50. {
  51. if ($config) {
  52. $missingFields = array();
  53. if (!isset($config['parent'])) {
  54. $missingFields[] = 'ancestor';
  55. }
  56. if (!isset($config['left'])) {
  57. $missingFields[] = 'left';
  58. }
  59. if (!isset($config['right'])) {
  60. $missingFields[] = 'right';
  61. }
  62. if ($missingFields) {
  63. throw new InvalidArgumentException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  64. }
  65. }
  66. }
  67. /**
  68. * {@inheritDoc}
  69. */
  70. public function readExtendedMetadata($meta, array &$config) {
  71. require_once __DIR__ . '/../Annotations.php';
  72. $reader = new AnnotationReader();
  73. $reader->setAnnotationNamespaceAlias('Gedmo\Tree\Mapping\\', 'gedmo');
  74. $class = $meta->getReflectionClass();
  75. // property annotations
  76. foreach ($class->getProperties() as $property) {
  77. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  78. $meta->isInheritedField($property->name) ||
  79. isset($meta->associationMappings[$property->name]['inherited'])
  80. ) {
  81. continue;
  82. }
  83. // left
  84. if ($left = $reader->getPropertyAnnotation($property, self::ANNOTATION_LEFT)) {
  85. $field = $property->getName();
  86. if (!$meta->hasField($field)) {
  87. throw new InvalidArgumentException("Unable to find 'left' - [{$field}] as mapped property in entity - {$meta->name}");
  88. }
  89. if (!$this->_isValidField($meta, $field)) {
  90. throw new InvalidArgumentException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  91. }
  92. $config['left'] = $field;
  93. }
  94. // right
  95. if ($right = $reader->getPropertyAnnotation($property, self::ANNOTATION_RIGHT)) {
  96. $field = $property->getName();
  97. if (!$meta->hasField($field)) {
  98. throw new InvalidArgumentException("Unable to find 'right' - [{$field}] as mapped property in entity - {$meta->name}");
  99. }
  100. if (!$this->_isValidField($meta, $field)) {
  101. throw new InvalidArgumentException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  102. }
  103. $config['right'] = $field;
  104. }
  105. // ancestor/parent
  106. if ($parent = $reader->getPropertyAnnotation($property, self::ANNOTATION_PARENT)) {
  107. $field = $property->getName();
  108. if (!$meta->isSingleValuedAssociation($field)) {
  109. throw new InvalidArgumentException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  110. }
  111. $config['parent'] = $field;
  112. }
  113. // level
  114. if ($parent = $reader->getPropertyAnnotation($property, self::ANNOTATION_LEVEL)) {
  115. $field = $property->getName();
  116. if (!$meta->hasField($field)) {
  117. throw new InvalidArgumentException("Unable to find 'level' - [{$field}] as mapped property in entity - {$meta->name}");
  118. }
  119. if (!$this->_isValidField($meta, $field)) {
  120. throw new InvalidArgumentException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  121. }
  122. $config['level'] = $field;
  123. }
  124. }
  125. }
  126. /**
  127. * Checks if $field type is valid
  128. *
  129. * @param ClassMetadataInfo $meta
  130. * @param string $field
  131. * @return boolean
  132. */
  133. protected function _isValidField($meta, $field)
  134. {
  135. $mapping = $meta->getFieldMapping($field);
  136. return $mapping && in_array($mapping['type'], $this->_validTypes);
  137. }
  138. }