Annotation.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. * List of types which are valid for slug and sluggable fields
  29. *
  30. * @var array
  31. */
  32. private $validTypes = array(
  33. 'string',
  34. 'text',
  35. 'integer'
  36. );
  37. /**
  38. * Annotation reader instance
  39. *
  40. * @var object
  41. */
  42. private $reader;
  43. /**
  44. * original driver if it is available
  45. */
  46. protected $_originalDriver = null;
  47. /**
  48. * {@inheritDoc}
  49. */
  50. public function setAnnotationReader($reader)
  51. {
  52. $this->reader = $reader;
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function readExtendedMetadata($meta, array &$config) {
  58. $class = $meta->getReflectionClass();
  59. if (!$class) {
  60. // based on recent doctrine 2.3.0-DEV maybe will be fixed in some way
  61. // this happens when running annotation driver in combination with
  62. // static reflection services. This is not the nicest fix
  63. $class = new \ReflectionClass($meta->name);
  64. }
  65. // property annotations
  66. foreach ($class->getProperties() as $property) {
  67. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  68. $meta->isInheritedField($property->name) ||
  69. isset($meta->associationMappings[$property->name]['inherited'])
  70. ) {
  71. continue;
  72. }
  73. // slug property
  74. if ($slug = $this->reader->getPropertyAnnotation($property, self::SLUG)) {
  75. $field = $property->getName();
  76. if (!$meta->hasField($field)) {
  77. throw new InvalidMappingException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}");
  78. }
  79. if (!$this->isValidField($meta, $field)) {
  80. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
  81. }
  82. // process slug fields
  83. if (empty($slug->fields) || !is_array($slug->fields)) {
  84. throw new InvalidMappingException("Slug must contain at least one field for slug generation in class - {$meta->name}");
  85. }
  86. foreach ($slug->fields as $slugField) {
  87. if (!$meta->hasField($slugField)) {
  88. throw new InvalidMappingException("Unable to find slug [{$slugField}] as mapped property in entity - {$meta->name}");
  89. }
  90. if (!$this->isValidField($meta, $slugField)) {
  91. throw new InvalidMappingException("Cannot use field - [{$slugField}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
  92. }
  93. }
  94. if (!is_bool($slug->updatable)) {
  95. throw new InvalidMappingException("Slug annotation [updatable], type is not valid and must be 'boolean' in class - {$meta->name}");
  96. }
  97. if (!is_bool($slug->unique)) {
  98. throw new InvalidMappingException("Slug annotation [unique], type is not valid and must be 'boolean' in class - {$meta->name}");
  99. }
  100. if ($meta->isIdentifier($field) && !(bool)$slug->unique) {
  101. throw new InvalidMappingException("Identifier field - [{$field}] slug must be unique in order to maintain primary key in class - {$meta->name}");
  102. }
  103. // set all options
  104. $config['slugs'][$field] = array(
  105. 'fields' => $slug->fields,
  106. 'slug' => $field,
  107. 'style' => $slug->style,
  108. 'updatable' => $slug->updatable,
  109. 'unique' => $slug->unique,
  110. 'separator' => $slug->separator,
  111. );
  112. }
  113. }
  114. }
  115. /**
  116. * Checks if $field type is valid as Sluggable field
  117. *
  118. * @param object $meta
  119. * @param string $field
  120. * @return boolean
  121. */
  122. protected function isValidField($meta, $field)
  123. {
  124. $mapping = $meta->getFieldMapping($field);
  125. return $mapping && in_array($mapping['type'], $this->validTypes);
  126. }
  127. /**
  128. * Passes in the mapping read by original driver
  129. *
  130. * @param $driver
  131. * @return void
  132. */
  133. public function setOriginalDriver($driver)
  134. {
  135. $this->_originalDriver = $driver;
  136. }
  137. }