Annotation.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 ENTITY_CLASS = 'Gedmo\\Mapping\\Annotation\\TranslationEntity';
  25. /**
  26. * Annotation to identify field as translatable
  27. */
  28. const TRANSLATABLE = 'Gedmo\\Mapping\\Annotation\\Translatable';
  29. /**
  30. * Annotation to identify field which can store used locale or language
  31. * alias is LANGUAGE
  32. */
  33. const LOCALE = 'Gedmo\\Mapping\\Annotation\\Locale';
  34. /**
  35. * Annotation to identify field which can store used locale or language
  36. * alias is LOCALE
  37. */
  38. const LANGUAGE = 'Gedmo\\Mapping\\Annotation\\Language';
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function validateFullMetadata(ClassMetadata $meta, array $config)
  43. {
  44. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  45. throw new InvalidMappingException("Translatable does not support composite identifiers in class - {$meta->name}");
  46. }
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  52. $reader = new AnnotationReader();
  53. $reader->setAnnotationNamespaceAlias('Gedmo\\Mapping\\Annotation\\', 'gedmo');
  54. $reader->setAutoloadAnnotations(true);
  55. $class = $meta->getReflectionClass();
  56. // class annotations
  57. $classAnnotations = $reader->getClassAnnotations($class);
  58. if (isset($classAnnotations[self::ENTITY_CLASS])) {
  59. $annot = $classAnnotations[self::ENTITY_CLASS];
  60. if (!class_exists($annot->class)) {
  61. throw new InvalidMappingException("Translation class: {$annot->class} does not exist.");
  62. }
  63. $config['translationClass'] = $annot->class;
  64. }
  65. // property annotations
  66. foreach ($class->getProperties() as $property) {
  67. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  68. $meta->isInheritedField($property->name) ||
  69. isset($meta->associationMappings[$property->name]['inherited'])
  70. ) {
  71. continue;
  72. }
  73. // translatable property
  74. if ($translatable = $reader->getPropertyAnnotation($property, self::TRANSLATABLE)) {
  75. $field = $property->getName();
  76. if (!$meta->hasField($field)) {
  77. throw new InvalidMappingException("Unable to find translatable [{$field}] as mapped property in entity - {$meta->name}");
  78. }
  79. // fields cannot be overrided and throws mapping exception
  80. $config['fields'][] = $field;
  81. }
  82. // locale property
  83. if ($locale = $reader->getPropertyAnnotation($property, self::LOCALE)) {
  84. $field = $property->getName();
  85. if ($meta->hasField($field)) {
  86. throw new InvalidMappingException("Locale field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  87. }
  88. $config['locale'] = $field;
  89. } elseif ($language = $reader->getPropertyAnnotation($property, self::LANGUAGE)) {
  90. $field = $property->getName();
  91. if ($meta->hasField($field)) {
  92. throw new InvalidMappingException("Language field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  93. }
  94. $config['locale'] = $field;
  95. }
  96. }
  97. }
  98. }