Annotation.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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) {
  62. if (!isset($config['fields'])) {
  63. throw new InvalidMappingException("Unable to find any sluggable fields specified for Sluggable entity - {$meta->name}");
  64. }
  65. foreach ($config['fields'] as $slugField => $fields) {
  66. if (!isset($config['slugFields'][$slugField])) {
  67. throw new InvalidMappingException("Unable to find {$slugField} slugField specified for Sluggable entity - {$meta->name}, you should specify slugField annotation property");
  68. }
  69. }
  70. }
  71. }
  72. /**
  73. * {@inheritDoc}
  74. */
  75. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  76. $class = $meta->getReflectionClass();
  77. // property annotations
  78. foreach ($class->getProperties() as $property) {
  79. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  80. $meta->isInheritedField($property->name) ||
  81. isset($meta->associationMappings[$property->name]['inherited'])
  82. ) {
  83. continue;
  84. }
  85. // sluggable property
  86. if ($sluggable = $this->reader->getPropertyAnnotation($property, self::SLUGGABLE)) {
  87. $field = $property->getName();
  88. if (!$meta->hasField($field)) {
  89. throw new InvalidMappingException("Unable to find sluggable [{$field}] as mapped property in entity - {$meta->name}");
  90. }
  91. if (!$this->isValidField($meta, $field)) {
  92. throw new InvalidMappingException("Cannot slug field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}");
  93. }
  94. if (!is_null($sluggable->slugField) and !$meta->hasField($sluggable->slugField)) {
  95. throw new InvalidMappingException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}");
  96. }
  97. $config['fields'][$sluggable->slugField][] = array('field' => $field, 'position' => $sluggable->position, 'slugField' => $sluggable->slugField);
  98. }
  99. // slug property
  100. if ($slug = $this->reader->getPropertyAnnotation($property, self::SLUG)) {
  101. $field = $property->getName();
  102. if (!$meta->hasField($field)) {
  103. throw new InvalidMappingException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}");
  104. }
  105. if (!$this->isValidField($meta, $field)) {
  106. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
  107. }
  108. $config['slugFields'][$field]['slug'] = $field;
  109. $config['slugFields'][$field]['style'] = $slug->style;
  110. $config['slugFields'][$field]['updatable'] = $slug->updatable;
  111. $config['slugFields'][$field]['unique'] = $slug->unique;
  112. $config['slugFields'][$field]['separator'] = $slug->separator;
  113. }
  114. }
  115. }
  116. /**
  117. * Checks if $field type is valid as Sluggable field
  118. *
  119. * @param ClassMetadata $meta
  120. * @param string $field
  121. * @return boolean
  122. */
  123. protected function isValidField(ClassMetadata $meta, $field)
  124. {
  125. $mapping = $meta->getFieldMapping($field);
  126. return $mapping && in_array($mapping['type'], $this->validTypes);
  127. }
  128. /**
  129. * Passes in the mapping read by original driver
  130. *
  131. * @param $driver
  132. * @return void
  133. */
  134. public function setOriginalDriver($driver)
  135. {
  136. $this->_originalDriver = $driver;
  137. }
  138. }