Annotation.php 3.9 KB

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