Annotation.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\Sluggable\Mapping\MappingException;
  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. * (non-PHPdoc)
  40. * @see Gedmo\Mapping.Driver::validateFullMetadata()
  41. */
  42. public function validateFullMetadata(ClassMetadataInfo $meta, array $config)
  43. {
  44. if ($config && !isset($config['fields'])) {
  45. throw MappingException::noFieldsToSlug($meta->name);
  46. }
  47. }
  48. /**
  49. * (non-PHPdoc)
  50. * @see Gedmo\Mapping.Driver::readExtendedMetadata()
  51. */
  52. public function readExtendedMetadata(ClassMetadataInfo $meta, array &$config) {
  53. require_once __DIR__ . '/../Annotations.php';
  54. $reader = new AnnotationReader();
  55. $reader->setAnnotationNamespaceAlias('Gedmo\Sluggable\Mapping\\', 'gedmo');
  56. $class = $meta->getReflectionClass();
  57. // property annotations
  58. foreach ($class->getProperties() as $property) {
  59. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  60. $meta->isInheritedField($property->name) ||
  61. $meta->isInheritedAssociation($property->name)
  62. ) {
  63. continue;
  64. }
  65. // sluggable property
  66. if ($sluggable = $reader->getPropertyAnnotation($property, self::ANNOTATION_SLUGGABLE)) {
  67. $field = $property->getName();
  68. if (!$meta->hasField($field)) {
  69. throw MappingException::fieldMustBeMapped($field, $meta->name);
  70. }
  71. if (!$this->_isValidField($meta, $field)) {
  72. throw MappingException::notValidFieldType($field, $meta->name);
  73. }
  74. $config['fields'][] = $field;
  75. }
  76. // slug property
  77. if ($slug = $reader->getPropertyAnnotation($property, self::ANNOTATION_SLUG)) {
  78. $field = $property->getName();
  79. if (!$meta->hasField($field)) {
  80. throw MappingException::slugFieldMustBeMapped($field, $meta->name);
  81. }
  82. if (!$this->_isValidField($meta, $field)) {
  83. throw MappingException::notValidFieldType($field, $meta->name);
  84. }
  85. if (isset($config['slug'])) {
  86. throw MappingException::slugFieldIsDuplicate($field, $meta->name);
  87. }
  88. $config['slug'] = $field;
  89. $config['style'] = $slug->style;
  90. $config['updatable'] = $slug->updatable;
  91. $config['unique'] = $slug->unique;
  92. $config['separator'] = $slug->separator;
  93. }
  94. }
  95. }
  96. /**
  97. * Checks if $field type is valid as Sluggable field
  98. *
  99. * @param ClassMetadataInfo $meta
  100. * @param string $field
  101. * @return boolean
  102. */
  103. protected function _isValidField(ClassMetadataInfo $meta, $field)
  104. {
  105. return in_array($meta->getTypeOfField($field), $this->_validTypes);
  106. }
  107. }