Annotation.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. Gedmo\Exception\InvalidMappingException;
  8. /**
  9. * This is an annotation mapping driver for Sluggable
  10. * behavioral extension. Used for extraction of extended
  11. * metadata from Annotations specificaly for Sluggable
  12. * extension.
  13. *
  14. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  15. * @package Gedmo.Sluggable.Mapping.Driver
  16. * @subpackage Annotation
  17. * @link http://www.gediminasm.org
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. class Annotation implements AnnotationDriverInterface
  21. {
  22. /**
  23. * Annotation to identify field as one which holds the slug
  24. * together with slug options
  25. */
  26. const SLUG = 'Gedmo\\Mapping\\Annotation\\Slug';
  27. /**
  28. * SlugHandler extension annotation
  29. */
  30. const HANDLER = 'Gedmo\\Mapping\\Annotation\\SlugHandler';
  31. /**
  32. * SlugHandler option annotation
  33. */
  34. const HANDLER_OPTION ='Gedmo\\Mapping\\Annotation\\SlugHandlerOption';
  35. /**
  36. * List of types which are valid for slug and sluggable fields
  37. *
  38. * @var array
  39. */
  40. private $validTypes = array(
  41. 'string',
  42. 'text',
  43. 'integer'
  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 readExtendedMetadata($meta, array &$config) {
  66. $class = $meta->getReflectionClass();
  67. if (!$class) {
  68. // based on recent doctrine 2.3.0-DEV maybe will be fixed in some way
  69. // this happens when running annotation driver in combination with
  70. // static reflection services. This is not the nicest fix
  71. $class = new \ReflectionClass($meta->name);
  72. }
  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. if (!is_bool($slug->updatable)) {
  127. throw new InvalidMappingException("Slug annotation [updatable], type is not valid and must be 'boolean' in class - {$meta->name}");
  128. }
  129. if (!is_bool($slug->unique)) {
  130. throw new InvalidMappingException("Slug annotation [unique], type is not valid and must be 'boolean' in class - {$meta->name}");
  131. }
  132. if ($meta->isIdentifier($field) && !(bool)$slug->unique) {
  133. throw new InvalidMappingException("Identifier field - [{$field}] slug must be unique in order to maintain primary key in class - {$meta->name}");
  134. }
  135. // set all options
  136. $config['slugs'][$field] = array(
  137. 'fields' => $slug->fields,
  138. 'slug' => $field,
  139. 'style' => $slug->style,
  140. 'updatable' => $slug->updatable,
  141. 'unique' => $slug->unique,
  142. 'separator' => $slug->separator,
  143. 'handlers' => $handlers
  144. );
  145. }
  146. }
  147. }
  148. /**
  149. * Checks if $field type is valid as Sluggable field
  150. *
  151. * @param object $meta
  152. * @param string $field
  153. * @return boolean
  154. */
  155. protected function isValidField($meta, $field)
  156. {
  157. $mapping = $meta->getFieldMapping($field);
  158. return $mapping && in_array($mapping['type'], $this->validTypes);
  159. }
  160. /**
  161. * Passes in the mapping read by original driver
  162. *
  163. * @param $driver
  164. * @return void
  165. */
  166. public function setOriginalDriver($driver)
  167. {
  168. $this->_originalDriver = $driver;
  169. }
  170. }