Annotation.php 6.4 KB

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