Annotation.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Gedmo\Sortable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AbstractAnnotationDriver,
  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 extends AbstractAnnotationDriver
  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. protected $validTypes = array(
  33. 'integer',
  34. 'smallint',
  35. 'bigint'
  36. );
  37. /**
  38. * {@inheritDoc}
  39. */
  40. public function readExtendedMetadata($meta, array &$config)
  41. {
  42. $class = $this->getMetaReflectionClass($meta);
  43. // property annotations
  44. foreach ($class->getProperties() as $property) {
  45. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  46. $meta->isInheritedField($property->name) ||
  47. isset($meta->associationMappings[$property->name]['inherited'])
  48. ) {
  49. continue;
  50. }
  51. // position
  52. if ($position = $this->reader->getPropertyAnnotation($property, self::POSITION)) {
  53. $field = $property->getName();
  54. if (!$meta->hasField($field)) {
  55. throw new InvalidMappingException("Unable to find 'position' - [{$field}] as mapped property in entity - {$meta->name}");
  56. }
  57. if (!$this->isValidField($meta, $field)) {
  58. throw new InvalidMappingException("Sortable position field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  59. }
  60. $config['position'] = $field;
  61. }
  62. // group
  63. if ($group = $this->reader->getPropertyAnnotation($property, self::GROUP)) {
  64. $field = $property->getName();
  65. if (!$meta->hasField($field) && !$meta->hasAssociation($field)) {
  66. throw new InvalidMappingException("Unable to find 'group' - [{$field}] as mapped property in entity - {$meta->name}");
  67. }
  68. if (!isset($config['groups'])) {
  69. $config['groups'] = array();
  70. }
  71. $config['groups'][] = $field;
  72. }
  73. }
  74. if (!$meta->isMappedSuperclass && $config) {
  75. if (!isset($config['position'])) {
  76. throw new InvalidMappingException("Missing property: 'position' in class - {$meta->name}");
  77. }
  78. }
  79. }
  80. }