Annotation.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 mark field as sluggable and include it in slug building
  25. */
  26. const SLUGGABLE = 'Gedmo\\Mapping\\Annotation\\Sluggable';
  27. /**
  28. * Annotation to identify field as one which holds the slug
  29. * together with slug options
  30. */
  31. const SLUG = 'Gedmo\\Mapping\\Annotation\\Slug';
  32. /**
  33. * SlugHandler extension annotation
  34. */
  35. const HANDLER = 'Gedmo\\Mapping\\Annotation\\SlugHandler';
  36. /**
  37. * SlugHandler option annotation
  38. */
  39. const HANDLER_OPTION ='Gedmo\\Mapping\\Annotation\\SlugHandlerOption';
  40. /**
  41. * List of types which are valid for slug and sluggable fields
  42. *
  43. * @var array
  44. */
  45. private $validTypes = array(
  46. 'string',
  47. 'text'
  48. );
  49. /**
  50. * Annotation reader instance
  51. *
  52. * @var object
  53. */
  54. private $reader;
  55. /**
  56. * original driver if it is available
  57. */
  58. protected $_originalDriver = null;
  59. /**
  60. * {@inheritDoc}
  61. */
  62. public function setAnnotationReader($reader)
  63. {
  64. $this->reader = $reader;
  65. }
  66. /**
  67. * {@inheritDoc}
  68. */
  69. public function validateFullMetadata(ClassMetadata $meta, array $config)
  70. {
  71. if ($config) {
  72. if (!isset($config['fields'])) {
  73. throw new InvalidMappingException("Unable to find any sluggable fields specified for Sluggable entity - {$meta->name}");
  74. }
  75. foreach ($config['fields'] as $slugField => $fields) {
  76. if (!isset($config['slugFields'][$slugField])) {
  77. throw new InvalidMappingException("Unable to find {$slugField} slugField specified for Sluggable entity - {$meta->name}, you should specify slugField annotation property");
  78. }
  79. }
  80. }
  81. }
  82. /**
  83. * {@inheritDoc}
  84. */
  85. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  86. $class = $meta->getReflectionClass();
  87. // property annotations
  88. foreach ($class->getProperties() as $property) {
  89. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  90. $meta->isInheritedField($property->name) ||
  91. isset($meta->associationMappings[$property->name]['inherited'])
  92. ) {
  93. continue;
  94. }
  95. // sluggable property
  96. if ($sluggable = $this->reader->getPropertyAnnotation($property, self::SLUGGABLE)) {
  97. $field = $property->getName();
  98. if (!$meta->hasField($field)) {
  99. throw new InvalidMappingException("Unable to find sluggable [{$field}] as mapped property in entity - {$meta->name}");
  100. }
  101. if (!$this->isValidField($meta, $field)) {
  102. throw new InvalidMappingException("Cannot slug field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}");
  103. }
  104. if (!is_null($sluggable->slugField) and !$meta->hasField($sluggable->slugField)) {
  105. throw new InvalidMappingException(sprintf('The "%s" property - which is defined as the "slugField" for the "%s" property - does not exist or is not mapped to Doctrine in "%s"', $sluggable->slugField, $field, $meta->name));
  106. }
  107. $config['fields'][$sluggable->slugField][] = array('field' => $field, 'position' => $sluggable->position, 'slugField' => $sluggable->slugField);
  108. }
  109. // slug property
  110. if ($slug = $this->reader->getPropertyAnnotation($property, self::SLUG)) {
  111. $field = $property->getName();
  112. if (!$meta->hasField($field)) {
  113. throw new InvalidMappingException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}");
  114. }
  115. if (!$this->isValidField($meta, $field)) {
  116. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
  117. }
  118. // process slug handlers
  119. if (is_array($slug->handlers) && $slug->handlers) {
  120. foreach ($slug->handlers as $handler) {
  121. if (!$handler instanceof SlugHandler) {
  122. throw new InvalidMappingException("SlugHandler: {$handler} should be instance of SlugHandler annotation in entity - {$meta->name}");
  123. }
  124. if (!strlen($handler->class)) {
  125. throw new InvalidMappingException("SlugHandler class: {$handler->class} should be a valid class name in entity - {$meta->name}");
  126. }
  127. $class = $handler->class;
  128. $config['handlers'][$class] = array();
  129. foreach ((array)$handler->options as $option) {
  130. if (!$option instanceof SlugHandlerOption) {
  131. throw new InvalidMappingException("SlugHandlerOption: {$option} should be instance of SlugHandlerOption annotation in entity - {$meta->name}");
  132. }
  133. if (!strlen($option->name)) {
  134. throw new InvalidMappingException("SlugHandlerOption name: {$option->name} should be valid name in entity - {$meta->name}");
  135. }
  136. $config['handlers'][$class][$option->name] = $option->value;
  137. }
  138. $class::validate($config['handlers'][$class], $meta);
  139. }
  140. }
  141. $config['slugFields'][$field]['slug'] = $field;
  142. $config['slugFields'][$field]['style'] = $slug->style;
  143. $config['slugFields'][$field]['updatable'] = $slug->updatable;
  144. $config['slugFields'][$field]['unique'] = $slug->unique;
  145. $config['slugFields'][$field]['separator'] = $slug->separator;
  146. }
  147. }
  148. }
  149. /**
  150. * Checks if $field type is valid as Sluggable field
  151. *
  152. * @param ClassMetadata $meta
  153. * @param string $field
  154. * @return boolean
  155. */
  156. protected function isValidField(ClassMetadata $meta, $field)
  157. {
  158. $mapping = $meta->getFieldMapping($field);
  159. return $mapping && in_array($mapping['type'], $this->validTypes);
  160. }
  161. /**
  162. * Passes in the mapping read by original driver
  163. *
  164. * @param $driver
  165. * @return void
  166. */
  167. public function setOriginalDriver($driver)
  168. {
  169. $this->_originalDriver = $driver;
  170. }
  171. }