Annotation.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Gedmo\Translatable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AbstractAnnotationDriver;
  4. use Gedmo\Exception\InvalidMappingException;
  5. /**
  6. * This is an annotation mapping driver for Translatable
  7. * behavioral extension. Used for extraction of extended
  8. * metadata from Annotations specifically for Translatable
  9. * extension.
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class Annotation extends AbstractAnnotationDriver
  15. {
  16. /**
  17. * Annotation to identity translation entity to be used for translation storage
  18. */
  19. const ENTITY_CLASS = 'Gedmo\\Mapping\\Annotation\\TranslationEntity';
  20. /**
  21. * Annotation to identify field as translatable
  22. */
  23. const TRANSLATABLE = 'Gedmo\\Mapping\\Annotation\\Translatable';
  24. /**
  25. * Annotation to identify field which can store used locale or language
  26. * alias is LANGUAGE
  27. */
  28. const LOCALE = 'Gedmo\\Mapping\\Annotation\\Locale';
  29. /**
  30. * Annotation to identify field which can store used locale or language
  31. * alias is LOCALE
  32. */
  33. const LANGUAGE = 'Gedmo\\Mapping\\Annotation\\Language';
  34. /**
  35. * {@inheritDoc}
  36. */
  37. public function readExtendedMetadata($meta, array &$config)
  38. {
  39. $class = $this->getMetaReflectionClass($meta);
  40. // class annotations
  41. if ($annot = $this->reader->getClassAnnotation($class, self::ENTITY_CLASS)) {
  42. if (!$cl = $this->getRelatedClassName($meta, $annot->class)) {
  43. throw new InvalidMappingException("Translation class: {$annot->class} does not exist.");
  44. }
  45. $config['translationClass'] = $cl;
  46. }
  47. // property annotations
  48. foreach ($class->getProperties() as $property) {
  49. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  50. $meta->isInheritedField($property->name) ||
  51. isset($meta->associationMappings[$property->name]['inherited'])
  52. ) {
  53. continue;
  54. }
  55. // translatable property
  56. if ($translatable = $this->reader->getPropertyAnnotation($property, self::TRANSLATABLE)) {
  57. $field = $property->getName();
  58. if (!$meta->hasField($field)) {
  59. throw new InvalidMappingException("Unable to find translatable [{$field}] as mapped property in entity - {$meta->name}");
  60. }
  61. // fields cannot be overrided and throws mapping exception
  62. $config['fields'][] = $field;
  63. if (isset($translatable->fallback)) {
  64. $config['fallback'][$field] = $translatable->fallback;
  65. }
  66. }
  67. // locale property
  68. if ($this->reader->getPropertyAnnotation($property, self::LOCALE)) {
  69. $field = $property->getName();
  70. if ($meta->hasField($field)) {
  71. throw new InvalidMappingException("Locale field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sense");
  72. }
  73. $config['locale'] = $field;
  74. } elseif ($this->reader->getPropertyAnnotation($property, self::LANGUAGE)) {
  75. $field = $property->getName();
  76. if ($meta->hasField($field)) {
  77. throw new InvalidMappingException("Language field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sense");
  78. }
  79. $config['locale'] = $field;
  80. }
  81. }
  82. // Embedded entity
  83. if (property_exists($meta, 'embeddedClasses') && $meta->embeddedClasses) {
  84. foreach ($meta->embeddedClasses as $propertyName => $embeddedClassInfo) {
  85. $embeddedClass = new \ReflectionClass($embeddedClassInfo['class']);
  86. foreach ($embeddedClass->getProperties() as $embeddedProperty) {
  87. if ($translatable = $this->reader->getPropertyAnnotation($embeddedProperty, self::TRANSLATABLE)) {
  88. $field = $propertyName . '.' . $embeddedProperty->getName();
  89. $config['fields'][] = $field;
  90. if (isset($translatable->fallback)) {
  91. $config['fallback'][$field] = $translatable->fallback;
  92. }
  93. }
  94. }
  95. }
  96. }
  97. if (!$meta->isMappedSuperclass && $config) {
  98. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  99. throw new InvalidMappingException("Translatable does not support composite identifiers in class - {$meta->name}");
  100. }
  101. }
  102. }
  103. }