Annotation.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace Gedmo\Sluggable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AnnotationDriverInterface,
  4. Doctrine\Common\Annotations\AnnotationReader,
  5. Doctrine\Common\Persistence\Mapping\ClassMetadata,
  6. Gedmo\Exception\InvalidMappingException;
  7. /**
  8. * This is an annotation mapping driver for Sluggable
  9. * behavioral extension. Used for extraction of extended
  10. * metadata from Annotations specificaly for Sluggable
  11. * extension.
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @package Gedmo.Sluggable.Mapping.Driver
  15. * @subpackage Annotation
  16. * @link http://www.gediminasm.org
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. class Annotation implements AnnotationDriverInterface
  20. {
  21. /**
  22. * Annotation to mark field as sluggable and include it in slug building
  23. */
  24. const SLUGGABLE = 'Gedmo\\Mapping\\Annotation\\Sluggable';
  25. /**
  26. * Annotation to identify field as one which holds the slug
  27. * together with slug options
  28. */
  29. const SLUG = 'Gedmo\\Mapping\\Annotation\\Slug';
  30. /**
  31. * List of types which are valid for slug and sluggable fields
  32. *
  33. * @var array
  34. */
  35. private $validTypes = array(
  36. 'string',
  37. 'text'
  38. );
  39. /**
  40. * Annotation reader instance
  41. *
  42. * @var object
  43. */
  44. private $reader;
  45. /**
  46. * original driver if it is available
  47. */
  48. protected $_originalDriver = null;
  49. /**
  50. * {@inheritDoc}
  51. */
  52. public function setAnnotationReader($reader)
  53. {
  54. $this->reader = $reader;
  55. }
  56. /**
  57. * {@inheritDoc}
  58. */
  59. public function validateFullMetadata(ClassMetadata $meta, array $config)
  60. {
  61. if ($config && !isset($config['fields'])) {
  62. throw new InvalidMappingException("Unable to find any sluggable fields specified for Sluggable entity - {$meta->name}");
  63. }
  64. }
  65. /**
  66. * {@inheritDoc}
  67. */
  68. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  69. $class = $meta->getReflectionClass();
  70. // property annotations
  71. foreach ($class->getProperties() as $property) {
  72. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  73. $meta->isInheritedField($property->name) ||
  74. isset($meta->associationMappings[$property->name]['inherited'])
  75. ) {
  76. continue;
  77. }
  78. // sluggable property
  79. if ($sluggable = $this->reader->getPropertyAnnotation($property, self::SLUGGABLE)) {
  80. $field = $property->getName();
  81. if (!$meta->hasField($field)) {
  82. throw new InvalidMappingException("Unable to find sluggable [{$field}] as mapped property in entity - {$meta->name}");
  83. }
  84. if (!$this->isValidField($meta, $field)) {
  85. throw new InvalidMappingException("Cannot slug field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}");
  86. }
  87. $config['fields'][] = array('field' => $field, 'position' => $sluggable->position);
  88. }
  89. // slug property
  90. if ($slug = $this->reader->getPropertyAnnotation($property, self::SLUG)) {
  91. $field = $property->getName();
  92. if (!$meta->hasField($field)) {
  93. throw new InvalidMappingException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}");
  94. }
  95. if (!$this->isValidField($meta, $field)) {
  96. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
  97. }
  98. <<<<<<< HEAD
  99. if (isset($config['slug'])) {
  100. throw new InvalidMappingException("There cannot be two slug fields: [{$slugField}] and [{$config['slug']}], in class - {$meta->name}.");
  101. }
  102. $config['slug'] = $field;
  103. $config['style'] = $slug->style;
  104. $config['updatable'] = $slug->updatable;
  105. $config['unique'] = $slug->unique;
  106. $config['separator'] = $slug->separator;
  107. =======
  108. $config['slugFields'][$field]['slug'] = $field;
  109. $config['slugFields'][$field]['style'] = $slug->style;
  110. $config['slugFields'][$field]['updatable'] = $slug->updatable;
  111. $config['slugFields'][$field]['unique'] = $slug->unique;
  112. $config['slugFields'][$field]['separator'] = $slug->separator;
  113. >>>>>>> a6dd4fd... Fixed coding standard problems
  114. }
  115. }
  116. }
  117. /**
  118. * Checks if $field type is valid as Sluggable field
  119. *
  120. * @param ClassMetadata $meta
  121. * @param string $field
  122. * @return boolean
  123. */
  124. protected function isValidField(ClassMetadata $meta, $field)
  125. {
  126. $mapping = $meta->getFieldMapping($field);
  127. return $mapping && in_array($mapping['type'], $this->validTypes);
  128. }
  129. /**
  130. * Passes in the mapping read by original driver
  131. *
  132. * @param $driver
  133. * @return void
  134. */
  135. public function setOriginalDriver($driver)
  136. {
  137. $this->_originalDriver = $driver;
  138. }
  139. }