Annotation.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace Gedmo\Sluggable\Mapping\Driver;
  3. use Gedmo\Mapping\Annotation\SlugHandler;
  4. use Gedmo\Mapping\Annotation\SlugHandlerOption;
  5. use Gedmo\Mapping\Driver\AnnotationDriverInterface,
  6. Doctrine\Common\Annotations\AnnotationReader,
  7. Doctrine\Common\Persistence\Mapping\ClassMetadata,
  8. Gedmo\Exception\InvalidMappingException;
  9. /**
  10. * This is an annotation mapping driver for Sluggable
  11. * behavioral extension. Used for extraction of extended
  12. * metadata from Annotations specificaly for Sluggable
  13. * extension.
  14. *
  15. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  16. * @package Gedmo.Sluggable.Mapping.Driver
  17. * @subpackage Annotation
  18. * @link http://www.gediminasm.org
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. class Annotation implements AnnotationDriverInterface
  22. {
  23. /**
  24. * Annotation to identify field as one which holds the slug
  25. * together with slug options
  26. */
  27. const SLUG = 'Gedmo\\Mapping\\Annotation\\Slug';
  28. /**
  29. * SlugHandler extension annotation
  30. */
  31. const HANDLER = 'Gedmo\\Mapping\\Annotation\\SlugHandler';
  32. /**
  33. * SlugHandler option annotation
  34. */
  35. const HANDLER_OPTION ='Gedmo\\Mapping\\Annotation\\SlugHandlerOption';
  36. /**
  37. * List of types which are valid for slug and sluggable fields
  38. *
  39. * @var array
  40. */
  41. private $validTypes = array(
  42. 'string',
  43. 'text'
  44. );
  45. /**
  46. * Annotation reader instance
  47. *
  48. * @var object
  49. */
  50. private $reader;
  51. /**
  52. * original driver if it is available
  53. */
  54. protected $_originalDriver = null;
  55. /**
  56. * {@inheritDoc}
  57. */
  58. public function setAnnotationReader($reader)
  59. {
  60. $this->reader = $reader;
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. public function validateFullMetadata(ClassMetadata $meta, array $config)
  66. {}
  67. /**
  68. * {@inheritDoc}
  69. */
  70. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  71. $class = $meta->getReflectionClass();
  72. // property annotations
  73. foreach ($class->getProperties() as $property) {
  74. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  75. $meta->isInheritedField($property->name) ||
  76. isset($meta->associationMappings[$property->name]['inherited'])
  77. ) {
  78. continue;
  79. }
  80. // slug property
  81. if ($slug = $this->reader->getPropertyAnnotation($property, self::SLUG)) {
  82. $field = $property->getName();
  83. if (!$meta->hasField($field)) {
  84. throw new InvalidMappingException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}");
  85. }
  86. if (!$this->isValidField($meta, $field)) {
  87. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
  88. }
  89. // process slug handlers
  90. $handlers = array();
  91. if (is_array($slug->handlers) && $slug->handlers) {
  92. foreach ($slug->handlers as $handler) {
  93. if (!$handler instanceof SlugHandler) {
  94. throw new InvalidMappingException("SlugHandler: {$handler} should be instance of SlugHandler annotation in entity - {$meta->name}");
  95. }
  96. if (!strlen($handler->class)) {
  97. throw new InvalidMappingException("SlugHandler class: {$handler->class} should be a valid class name in entity - {$meta->name}");
  98. }
  99. $class = $handler->class;
  100. $handlers[$class] = array();
  101. foreach ((array)$handler->options as $option) {
  102. if (!$option instanceof SlugHandlerOption) {
  103. throw new InvalidMappingException("SlugHandlerOption: {$option} should be instance of SlugHandlerOption annotation in entity - {$meta->name}");
  104. }
  105. if (!strlen($option->name)) {
  106. throw new InvalidMappingException("SlugHandlerOption name: {$option->name} should be valid name in entity - {$meta->name}");
  107. }
  108. $handlers[$class][$option->name] = $option->value;
  109. }
  110. $class::validate($handlers[$class], $meta);
  111. }
  112. }
  113. // process slug fields
  114. if (empty($slug->fields) || !is_array($slug->fields)) {
  115. throw new InvalidMappingException("Slug must contain at least one field for slug generation in class - {$meta->name}");
  116. }
  117. foreach ($slug->fields as $slugField) {
  118. if (!$meta->hasField($slugField)) {
  119. throw new InvalidMappingException("Unable to find slug [{$slugField}] as mapped property in entity - {$meta->name}");
  120. }
  121. if (!$this->isValidField($meta, $slugField)) {
  122. throw new InvalidMappingException("Cannot use field - [{$slugField}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
  123. }
  124. }
  125. // set all options
  126. $config['slugs'][$field] = array(
  127. 'fields' => $slug->fields,
  128. 'slug' => $field,
  129. 'style' => $slug->style,
  130. 'updatable' => $slug->updatable,
  131. 'unique' => $slug->unique,
  132. 'separator' => $slug->separator,
  133. 'handlers' => $handlers
  134. );
  135. }
  136. }
  137. }
  138. /**
  139. * Checks if $field type is valid as Sluggable field
  140. *
  141. * @param ClassMetadata $meta
  142. * @param string $field
  143. * @return boolean
  144. */
  145. protected function isValidField(ClassMetadata $meta, $field)
  146. {
  147. $mapping = $meta->getFieldMapping($field);
  148. return $mapping && in_array($mapping['type'], $this->validTypes);
  149. }
  150. /**
  151. * Passes in the mapping read by original driver
  152. *
  153. * @param $driver
  154. * @return void
  155. */
  156. public function setOriginalDriver($driver)
  157. {
  158. $this->_originalDriver = $driver;
  159. }
  160. }