Annotation.php 5.3 KB

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