Annotation.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. 'text'
  38. );
  39. /**
  40. * Annotation reader instance
  41. *
  42. * @var object
  43. */
  44. private $reader;
  45. /**
  46. * original driver if it is available
  47. */
  48. protected $_originalDriver = null;
  49. /**
  50. * {@inheritDoc}
  51. */
  52. public function setAnnotationReader($reader)
  53. {
  54. $this->reader = $reader;
  55. }
  56. /**
  57. * {@inheritDoc}
  58. */
  59. public function validateFullMetadata(ClassMetadata $meta, array $config)
  60. {
  61. if ($config && !isset($config['fields'])) {
  62. throw new InvalidMappingException("Unable to find any sluggable fields specified for Sluggable entity - {$meta->name}");
  63. }
  64. }
  65. /**
  66. * {@inheritDoc}
  67. */
  68. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  69. $class = $meta->getReflectionClass();
  70. // property annotations
  71. foreach ($class->getProperties() as $property) {
  72. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  73. $meta->isInheritedField($property->name) ||
  74. isset($meta->associationMappings[$property->name]['inherited'])
  75. ) {
  76. continue;
  77. }
  78. // sluggable property
  79. if ($sluggable = $this->reader->getPropertyAnnotation($property, self::SLUGGABLE)) {
  80. $field = $property->getName();
  81. if (!$meta->hasField($field)) {
  82. throw new InvalidMappingException("Unable to find sluggable [{$field}] as mapped property in entity - {$meta->name}");
  83. }
  84. if (!$this->isValidField($meta, $field)) {
  85. throw new InvalidMappingException("Cannot slug field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}");
  86. }
  87. if (!is_null($sluggable->slugField) and !$meta->hasField($sluggable->slugField)) {
  88. throw new InvalidMappingException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}");
  89. }
  90. $config['fields'][$sluggable->slugField][] = array('field' => $field, 'position' => $sluggable->position, 'slugField' => $sluggable->slugField);
  91. }
  92. // slug property
  93. if ($slug = $this->reader->getPropertyAnnotation($property, self::SLUG)) {
  94. $field = $property->getName();
  95. if (!$meta->hasField($field)) {
  96. throw new InvalidMappingException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}");
  97. }
  98. if (!$this->isValidField($meta, $field)) {
  99. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
  100. }
  101. $config['slugFields'][$field]['slug'] = $field;
  102. $config['slugFields'][$field]['style'] = $slug->style;
  103. $config['slugFields'][$field]['updatable'] = $slug->updatable;
  104. $config['slugFields'][$field]['unique'] = $slug->unique;
  105. $config['slugFields'][$field]['separator'] = $slug->separator;
  106. }
  107. }
  108. }
  109. /**
  110. * Checks if $field type is valid as Sluggable field
  111. *
  112. * @param ClassMetadata $meta
  113. * @param string $field
  114. * @return boolean
  115. */
  116. protected function isValidField(ClassMetadata $meta, $field)
  117. {
  118. $mapping = $meta->getFieldMapping($field);
  119. return $mapping && in_array($mapping['type'], $this->validTypes);
  120. }
  121. /**
  122. * Passes in the mapping read by original driver
  123. *
  124. * @param $driver
  125. * @return void
  126. */
  127. public function setOriginalDriver($driver)
  128. {
  129. $this->_originalDriver = $driver;
  130. }
  131. }