Annotation.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Gedmo\Sluggable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AnnotationDriverInterface,
  4. Doctrine\Common\Annotations\AnnotationReader,
  5. Doctrine\Common\Persistence\Mapping\ClassMetadata,
  6. Gedmo\Exception\InvalidMappingException;
  7. /**
  8. * This is an annotation mapping driver for Sluggable
  9. * behavioral extension. Used for extraction of extended
  10. * metadata from Annotations specificaly for Sluggable
  11. * extension.
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @package Gedmo.Sluggable.Mapping.Driver
  15. * @subpackage Annotation
  16. * @link http://www.gediminasm.org
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. class Annotation implements AnnotationDriverInterface
  20. {
  21. /**
  22. * Annotation to mark field as sluggable and include it in slug building
  23. */
  24. const SLUGGABLE = 'Gedmo\\Mapping\\Annotation\\Sluggable';
  25. /**
  26. * Annotation to identify field as one which holds the slug
  27. * together with slug options
  28. */
  29. const SLUG = 'Gedmo\\Mapping\\Annotation\\Slug';
  30. /**
  31. * List of types which are valid for slug and sluggable fields
  32. *
  33. * @var array
  34. */
  35. private $validTypes = array(
  36. 'string'
  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['fields'])) {
  61. throw new InvalidMappingException("Unable to find any sluggable fields specified for Sluggable entity - {$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. // sluggable property
  78. if ($sluggable = $this->reader->getPropertyAnnotation($property, self::SLUGGABLE)) {
  79. $field = $property->getName();
  80. if (!$meta->hasField($field)) {
  81. throw new InvalidMappingException("Unable to find sluggable [{$field}] as mapped property in entity - {$meta->name}");
  82. }
  83. if (!$this->isValidField($meta, $field)) {
  84. throw new InvalidMappingException("Cannot slug field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}");
  85. }
  86. $config['fields'][] = array('field' => $field, 'position' => $sluggable->position);
  87. }
  88. // slug property
  89. if ($slug = $this->reader->getPropertyAnnotation($property, self::SLUG)) {
  90. $field = $property->getName();
  91. if (!$meta->hasField($field)) {
  92. throw new InvalidMappingException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}");
  93. }
  94. if (!$this->isValidField($meta, $field)) {
  95. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
  96. }
  97. if (isset($config['slug'])) {
  98. throw new InvalidMappingException("There cannot be two slug fields: [{$slugField}] and [{$config['slug']}], in class - {$meta->name}.");
  99. }
  100. $config['slug'] = $field;
  101. $config['style'] = $slug->style;
  102. $config['updatable'] = $slug->updatable;
  103. $config['unique'] = $slug->unique;
  104. $config['separator'] = $slug->separator;
  105. }
  106. }
  107. }
  108. /**
  109. * Checks if $field type is valid as Sluggable field
  110. *
  111. * @param ClassMetadata $meta
  112. * @param string $field
  113. * @return boolean
  114. */
  115. protected function isValidField(ClassMetadata $meta, $field)
  116. {
  117. $mapping = $meta->getFieldMapping($field);
  118. return $mapping && in_array($mapping['type'], $this->validTypes);
  119. }
  120. /**
  121. * Passes in the mapping read by original driver
  122. *
  123. * @param $driver
  124. * @return void
  125. */
  126. public function setOriginalDriver($driver)
  127. {
  128. $this->_originalDriver = $driver;
  129. }
  130. }