Annotation.php 3.9 KB

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