Annotation.php 4.0 KB

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