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. Gedmo\Exception\InvalidArgumentException;
  6. /**
  7. * This is an annotation mapping driver for Sluggable
  8. * behavioral extension. Used for extraction of extended
  9. * metadata from Annotations specificaly for Sluggable
  10. * extension.
  11. *
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @package Gedmo.Sluggable.Mapping.Driver
  14. * @subpackage Annotation
  15. * @link http://www.gediminasm.org
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. class Annotation implements Driver
  19. {
  20. /**
  21. * Annotation to mark field as sluggable and include it in slug building
  22. */
  23. const ANNOTATION_SLUGGABLE = 'Gedmo\Sluggable\Mapping\Sluggable';
  24. /**
  25. * Annotation to identify field as one which holds the slug
  26. * together with slug options
  27. */
  28. const ANNOTATION_SLUG = 'Gedmo\Sluggable\Mapping\Slug';
  29. /**
  30. * List of types which are valid for slug and sluggable fields
  31. *
  32. * @var array
  33. */
  34. private $_validTypes = array(
  35. 'string'
  36. );
  37. /**
  38. * {@inheritDoc}
  39. */
  40. public function validateFullMetadata($meta, array $config)
  41. {
  42. if ($config && !isset($config['fields'])) {
  43. throw new InvalidArgumentException("Unable to find any sluggable fields specified for Sluggable entity - {$meta->name}");
  44. }
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public function readExtendedMetadata($meta, array &$config) {
  50. require_once __DIR__ . '/../Annotations.php';
  51. $reader = new AnnotationReader();
  52. $reader->setAnnotationNamespaceAlias('Gedmo\Sluggable\Mapping\\', 'gedmo');
  53. $class = $meta->getReflectionClass();
  54. // property annotations
  55. foreach ($class->getProperties() as $property) {
  56. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  57. $meta->isInheritedField($property->name) ||
  58. isset($meta->associationMappings[$property->name]['inherited'])
  59. ) {
  60. continue;
  61. }
  62. // sluggable property
  63. if ($sluggable = $reader->getPropertyAnnotation($property, self::ANNOTATION_SLUGGABLE)) {
  64. $field = $property->getName();
  65. if (!$meta->hasField($field)) {
  66. throw new InvalidArgumentException("Unable to find sluggable [{$field}] as mapped property in entity - {$meta->name}");
  67. }
  68. if (!$this->_isValidField($meta, $field)) {
  69. throw new InvalidArgumentException("Cannot slug field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}");
  70. }
  71. $config['fields'][] = $field;
  72. }
  73. // slug property
  74. if ($slug = $reader->getPropertyAnnotation($property, self::ANNOTATION_SLUG)) {
  75. $field = $property->getName();
  76. if (!$meta->hasField($field)) {
  77. throw new InvalidArgumentException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}");
  78. }
  79. if (!$this->_isValidField($meta, $field)) {
  80. throw new InvalidArgumentException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
  81. }
  82. if (isset($config['slug'])) {
  83. throw new InvalidArgumentException("There cannot be two slug fields: [{$slugField}] and [{$config['slug']}], in class - {$meta->name}.");
  84. }
  85. $config['slug'] = $field;
  86. $config['style'] = $slug->style;
  87. $config['updatable'] = $slug->updatable;
  88. $config['unique'] = $slug->unique;
  89. $config['separator'] = $slug->separator;
  90. }
  91. }
  92. }
  93. /**
  94. * Checks if $field type is valid as Sluggable field
  95. *
  96. * @param ClassMetadata $meta
  97. * @param string $field
  98. * @return boolean
  99. */
  100. protected function _isValidField($meta, $field)
  101. {
  102. $mapping = $meta->getFieldMapping($field);
  103. return $mapping && in_array($mapping['type'], $this->_validTypes);
  104. }
  105. }