Annotation.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 validateFullMetadata(ClassMetadata $meta, array $config)
  59. {
  60. if ($config && !isset($config['position'])) {
  61. throw new InvalidMappingException("Missing property: 'position' in class - {$meta->name}");
  62. }
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  68. $class = $meta->getReflectionClass();
  69. // property annotations
  70. foreach ($class->getProperties() as $property) {
  71. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  72. $meta->isInheritedField($property->name) ||
  73. isset($meta->associationMappings[$property->name]['inherited'])
  74. ) {
  75. continue;
  76. }
  77. // position
  78. if ($position = $this->reader->getPropertyAnnotation($property, self::POSITION)) {
  79. $field = $property->getName();
  80. if (!$meta->hasField($field)) {
  81. throw new InvalidMappingException("Unable to find 'position' - [{$field}] as mapped property in entity - {$meta->name}");
  82. }
  83. if (!$this->isValidField($meta, $field)) {
  84. throw new InvalidMappingException("Sortable position field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  85. }
  86. $config['position'] = $field;
  87. }
  88. // group
  89. if ($group = $this->reader->getPropertyAnnotation($property, self::GROUP)) {
  90. $field = $property->getName();
  91. if (!$meta->hasField($field) && !$meta->hasAssociation($field)) {
  92. throw new InvalidMappingException("Unable to find 'group' - [{$field}] as mapped property in entity - {$meta->name}");
  93. }
  94. if (!isset($config['groups'])) {
  95. $config['groups'] = array();
  96. }
  97. $config['groups'][] = $field;
  98. }
  99. }
  100. }
  101. /**
  102. * Checks if $field type is valid
  103. *
  104. * @param ClassMetadata $meta
  105. * @param string $field
  106. * @return boolean
  107. */
  108. protected function isValidField($meta, $field)
  109. {
  110. $mapping = $meta->getFieldMapping($field);
  111. return $mapping && in_array($mapping['type'], $this->validTypes);
  112. }
  113. /**
  114. * Passes in the mapping read by original driver
  115. *
  116. * @param $driver
  117. * @return void
  118. */
  119. public function setOriginalDriver($driver)
  120. {
  121. $this->_originalDriver = $driver;
  122. }
  123. }