Annotation.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Gedmo\Translatable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver,
  4. Doctrine\Common\Persistence\Mapping\ClassMetadata,
  5. Doctrine\Common\Annotations\AnnotationReader,
  6. Gedmo\Exception\InvalidMappingException;
  7. /**
  8. * This is an annotation mapping driver for Translatable
  9. * behavioral extension. Used for extraction of extended
  10. * metadata from Annotations specificaly for Translatable
  11. * extension.
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @package Gedmo.Translatable.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 identity translation entity to be used for translation storage
  23. */
  24. const ANNOTATION_ENTITY_CLASS = 'Gedmo\Translatable\Mapping\TranslationEntity';
  25. /**
  26. * Annotation to identify field as translatable
  27. */
  28. const ANNOTATION_TRANSLATABLE = 'Gedmo\Translatable\Mapping\Translatable';
  29. /**
  30. * Annotation to identify field which can store used locale or language
  31. * alias is ANNOTATION_LANGUAGE
  32. */
  33. const ANNOTATION_LOCALE = 'Gedmo\Translatable\Mapping\Locale';
  34. /**
  35. * Annotation to identify field which can store used locale or language
  36. * alias is ANNOTATION_LOCALE
  37. */
  38. const ANNOTATION_LANGUAGE = 'Gedmo\Translatable\Mapping\Language';
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function validateFullMetadata(ClassMetadata $meta, array $config) {}
  43. /**
  44. * {@inheritDoc}
  45. */
  46. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  47. require_once __DIR__ . '/../Annotations.php';
  48. $reader = new AnnotationReader();
  49. $reader->setAnnotationNamespaceAlias('Gedmo\Translatable\Mapping\\', 'gedmo');
  50. $class = $meta->getReflectionClass();
  51. // class annotations
  52. $classAnnotations = $reader->getClassAnnotations($class);
  53. if (isset($classAnnotations[self::ANNOTATION_ENTITY_CLASS])) {
  54. $annot = $classAnnotations[self::ANNOTATION_ENTITY_CLASS];
  55. if (!class_exists($annot->class)) {
  56. throw new InvalidMappingException("Translation class: {$annot->class} does not exist.");
  57. }
  58. $config['translationClass'] = $annot->class;
  59. }
  60. // property annotations
  61. foreach ($class->getProperties() as $property) {
  62. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  63. $meta->isInheritedField($property->name) ||
  64. isset($meta->associationMappings[$property->name]['inherited'])
  65. ) {
  66. continue;
  67. }
  68. // translatable property
  69. if ($translatable = $reader->getPropertyAnnotation($property, self::ANNOTATION_TRANSLATABLE)) {
  70. $field = $property->getName();
  71. if (!$meta->hasField($field)) {
  72. throw new InvalidMappingException("Unable to find translatable [{$field}] as mapped property in entity - {$meta->name}");
  73. }
  74. // fields cannot be overrided and throws mapping exception
  75. $config['fields'][] = $field;
  76. }
  77. // locale property
  78. if ($locale = $reader->getPropertyAnnotation($property, self::ANNOTATION_LOCALE)) {
  79. $field = $property->getName();
  80. if ($meta->hasField($field)) {
  81. throw new InvalidMappingException("Locale field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  82. }
  83. $config['locale'] = $field;
  84. } elseif ($language = $reader->getPropertyAnnotation($property, self::ANNOTATION_LANGUAGE)) {
  85. $field = $property->getName();
  86. if ($meta->hasField($field)) {
  87. throw new InvalidMappingException("Language field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  88. }
  89. $config['locale'] = $field;
  90. }
  91. }
  92. }
  93. }