Annotation.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Gedmo\Sluggable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver,
  4. Doctrine\Common\Annotations\AnnotationReader,
  5. Doctrine\ORM\Mapping\ClassMetadataInfo,
  6. Gedmo\Exception\InvalidArgumentException;
  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 Driver
  20. {
  21. /**
  22. * Annotation to mark field as sluggable and include it in slug building
  23. */
  24. const ANNOTATION_SLUGGABLE = 'Gedmo\Sluggable\Mapping\Sluggable';
  25. /**
  26. * Annotation to identify field as one which holds the slug
  27. * together with slug options
  28. */
  29. const ANNOTATION_SLUG = 'Gedmo\Sluggable\Mapping\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. );
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public function validateFullMetadata(ClassMetadataInfo $meta, array $config)
  42. {
  43. if ($config && !isset($config['fields'])) {
  44. throw new InvalidArgumentException("Unable to find any sluggable fields specified for Sluggable entity - {$meta->name}");
  45. }
  46. }
  47. /**
  48. * {@inheritDoc}
  49. */
  50. public function readExtendedMetadata(ClassMetadataInfo $meta, array &$config) {
  51. require_once __DIR__ . '/../Annotations.php';
  52. $reader = new AnnotationReader();
  53. $reader->setAnnotationNamespaceAlias('Gedmo\Sluggable\Mapping\\', 'gedmo');
  54. $class = $meta->getReflectionClass();
  55. // property annotations
  56. foreach ($class->getProperties() as $property) {
  57. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  58. $meta->isInheritedField($property->name) ||
  59. $meta->isInheritedAssociation($property->name)
  60. ) {
  61. continue;
  62. }
  63. // sluggable property
  64. if ($sluggable = $reader->getPropertyAnnotation($property, self::ANNOTATION_SLUGGABLE)) {
  65. $field = $property->getName();
  66. if (!$meta->hasField($field)) {
  67. throw new InvalidArgumentException("Unable to find sluggable [{$field}] as mapped property in entity - {$meta->name}");
  68. }
  69. if (!$this->_isValidField($meta, $field)) {
  70. throw new InvalidArgumentException("Cannot slug field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}");
  71. }
  72. $config['fields'][] = $field;
  73. }
  74. // slug property
  75. if ($slug = $reader->getPropertyAnnotation($property, self::ANNOTATION_SLUG)) {
  76. $field = $property->getName();
  77. if (!$meta->hasField($field)) {
  78. throw new InvalidArgumentException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}");
  79. }
  80. if (!$this->_isValidField($meta, $field)) {
  81. throw new InvalidArgumentException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
  82. }
  83. if (isset($config['slug'])) {
  84. throw new InvalidArgumentException("There cannot be two slug fields: [{$slugField}] and [{$config['slug']}], in class - {$meta->name}.");
  85. }
  86. $config['slug'] = $field;
  87. $config['style'] = $slug->style;
  88. $config['updatable'] = $slug->updatable;
  89. $config['unique'] = $slug->unique;
  90. $config['separator'] = $slug->separator;
  91. }
  92. }
  93. }
  94. /**
  95. * Checks if $field type is valid as Sluggable field
  96. *
  97. * @param ClassMetadataInfo $meta
  98. * @param string $field
  99. * @return boolean
  100. */
  101. protected function _isValidField(ClassMetadataInfo $meta, $field)
  102. {
  103. return in_array($meta->getTypeOfField($field), $this->_validTypes);
  104. }
  105. }