Annotation.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace Gedmo\Sortable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AnnotationDriverInterface,
  4. Gedmo\Exception\InvalidMappingException;
  5. /**
  6. * This is an annotation mapping driver for Sortable
  7. * behavioral extension. Used for extraction of extended
  8. * metadata from Annotations specificaly for Sortable
  9. * extension.
  10. *
  11. * @author Lukas Botsch <lukas.botsch@gmail.com>
  12. * @package Gedmo.Sortable.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 mark field as one which will store node position
  21. */
  22. const POSITION = 'Gedmo\\Mapping\\Annotation\\SortablePosition';
  23. /**
  24. * Annotation to mark field as sorting group
  25. */
  26. const GROUP = 'Gedmo\\Mapping\\Annotation\\SortableGroup';
  27. /**
  28. * List of types which are valid for position fields
  29. *
  30. * @var array
  31. */
  32. private $validTypes = array(
  33. 'integer',
  34. 'smallint',
  35. 'bigint'
  36. );
  37. /**
  38. * Annotation reader instance
  39. *
  40. * @var object
  41. */
  42. private $reader;
  43. /**
  44. * original driver if it is available
  45. */
  46. protected $_originalDriver = null;
  47. /**
  48. * {@inheritDoc}
  49. */
  50. public function setAnnotationReader($reader)
  51. {
  52. $this->reader = $reader;
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function readExtendedMetadata($meta, array &$config)
  58. {
  59. $class = $meta->getReflectionClass();
  60. if (!$class) {
  61. // based on recent doctrine 2.3.0-DEV maybe will be fixed in some way
  62. // this happens when running annotation driver in combination with
  63. // static reflection services. This is not the nicest fix
  64. $class = new \ReflectionClass($meta->name);
  65. }
  66. // property annotations
  67. foreach ($class->getProperties() as $property) {
  68. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  69. $meta->isInheritedField($property->name) ||
  70. isset($meta->associationMappings[$property->name]['inherited'])
  71. ) {
  72. continue;
  73. }
  74. // position
  75. if ($position = $this->reader->getPropertyAnnotation($property, self::POSITION)) {
  76. $field = $property->getName();
  77. if (!$meta->hasField($field)) {
  78. throw new InvalidMappingException("Unable to find 'position' - [{$field}] as mapped property in entity - {$meta->name}");
  79. }
  80. if (!$this->isValidField($meta, $field)) {
  81. throw new InvalidMappingException("Sortable position field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  82. }
  83. $config['position'] = $field;
  84. }
  85. // group
  86. if ($group = $this->reader->getPropertyAnnotation($property, self::GROUP)) {
  87. $field = $property->getName();
  88. if (!$meta->hasField($field) && !$meta->hasAssociation($field)) {
  89. throw new InvalidMappingException("Unable to find 'group' - [{$field}] as mapped property in entity - {$meta->name}");
  90. }
  91. if (!isset($config['groups'])) {
  92. $config['groups'] = array();
  93. }
  94. $config['groups'][] = $field;
  95. }
  96. }
  97. if (!$meta->isMappedSuperclass && $config) {
  98. if (!isset($config['position'])) {
  99. throw new InvalidMappingException("Missing property: 'position' in class - {$meta->name}");
  100. }
  101. }
  102. }
  103. /**
  104. * Checks if $field type is valid
  105. *
  106. * @param object $meta
  107. * @param string $field
  108. * @return boolean
  109. */
  110. protected function isValidField($meta, $field)
  111. {
  112. $mapping = $meta->getFieldMapping($field);
  113. return $mapping && in_array($mapping['type'], $this->validTypes);
  114. }
  115. /**
  116. * Passes in the mapping read by original driver
  117. *
  118. * @param $driver
  119. * @return void
  120. */
  121. public function setOriginalDriver($driver)
  122. {
  123. $this->_originalDriver = $driver;
  124. }
  125. }