Annotation.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. 'integer'
  45. );
  46. /**
  47. * Annotation reader instance
  48. *
  49. * @var object
  50. */
  51. private $reader;
  52. /**
  53. * original driver if it is available
  54. */
  55. protected $_originalDriver = null;
  56. /**
  57. * {@inheritDoc}
  58. */
  59. public function setAnnotationReader($reader)
  60. {
  61. $this->reader = $reader;
  62. }
  63. /**
  64. * {@inheritDoc}
  65. */
  66. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  67. $class = $meta->getReflectionClass();
  68. // property annotations
  69. foreach ($class->getProperties() as $property) {
  70. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  71. $meta->isInheritedField($property->name) ||
  72. isset($meta->associationMappings[$property->name]['inherited'])
  73. ) {
  74. continue;
  75. }
  76. // slug property
  77. if ($slug = $this->reader->getPropertyAnnotation($property, self::SLUG)) {
  78. $field = $property->getName();
  79. if (!$meta->hasField($field)) {
  80. throw new InvalidMappingException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}");
  81. }
  82. if (!$this->isValidField($meta, $field)) {
  83. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
  84. }
  85. // process slug handlers
  86. $handlers = array();
  87. if (is_array($slug->handlers) && $slug->handlers) {
  88. foreach ($slug->handlers as $handler) {
  89. if (!$handler instanceof SlugHandler) {
  90. throw new InvalidMappingException("SlugHandler: {$handler} should be instance of SlugHandler annotation in entity - {$meta->name}");
  91. }
  92. if (!strlen($handler->class)) {
  93. throw new InvalidMappingException("SlugHandler class: {$handler->class} should be a valid class name in entity - {$meta->name}");
  94. }
  95. $class = $handler->class;
  96. $handlers[$class] = array();
  97. foreach ((array)$handler->options as $option) {
  98. if (!$option instanceof SlugHandlerOption) {
  99. throw new InvalidMappingException("SlugHandlerOption: {$option} should be instance of SlugHandlerOption annotation in entity - {$meta->name}");
  100. }
  101. if (!strlen($option->name)) {
  102. throw new InvalidMappingException("SlugHandlerOption name: {$option->name} should be valid name in entity - {$meta->name}");
  103. }
  104. $handlers[$class][$option->name] = $option->value;
  105. }
  106. $class::validate($handlers[$class], $meta);
  107. }
  108. }
  109. // process slug fields
  110. if (empty($slug->fields) || !is_array($slug->fields)) {
  111. throw new InvalidMappingException("Slug must contain at least one field for slug generation in class - {$meta->name}");
  112. }
  113. foreach ($slug->fields as $slugField) {
  114. if (!$meta->hasField($slugField)) {
  115. throw new InvalidMappingException("Unable to find slug [{$slugField}] as mapped property in entity - {$meta->name}");
  116. }
  117. if (!$this->isValidField($meta, $slugField)) {
  118. throw new InvalidMappingException("Cannot use field - [{$slugField}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
  119. }
  120. }
  121. // set all options
  122. $config['slugs'][$field] = array(
  123. 'fields' => $slug->fields,
  124. 'slug' => $field,
  125. 'style' => $slug->style,
  126. 'updatable' => $slug->updatable,
  127. 'unique' => $slug->unique,
  128. 'separator' => $slug->separator,
  129. 'handlers' => $handlers
  130. );
  131. }
  132. }
  133. }
  134. /**
  135. * Checks if $field type is valid as Sluggable field
  136. *
  137. * @param ClassMetadata $meta
  138. * @param string $field
  139. * @return boolean
  140. */
  141. protected function isValidField(ClassMetadata $meta, $field)
  142. {
  143. $mapping = $meta->getFieldMapping($field);
  144. return $mapping && in_array($mapping['type'], $this->validTypes);
  145. }
  146. /**
  147. * Passes in the mapping read by original driver
  148. *
  149. * @param $driver
  150. * @return void
  151. */
  152. public function setOriginalDriver($driver)
  153. {
  154. $this->_originalDriver = $driver;
  155. }
  156. }