vendor/gedmo/doctrine-extensions/src/Translatable/Mapping/Driver/Attribute.php line 58

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Doctrine Behavioral Extensions package.
  4. * (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace Gedmo\Translatable\Mapping\Driver;
  9. use Doctrine\ORM\Mapping\EmbeddedClassMapping;
  10. use Gedmo\Exception\InvalidMappingException;
  11. use Gedmo\Mapping\Annotation\Language;
  12. use Gedmo\Mapping\Annotation\Locale;
  13. use Gedmo\Mapping\Annotation\Translatable;
  14. use Gedmo\Mapping\Annotation\TranslationEntity;
  15. use Gedmo\Mapping\Driver\AbstractAnnotationDriver;
  16. /**
  17. * Mapping driver for the translatable extension which reads extended metadata from annotations on a translatable class.
  18. *
  19. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  20. *
  21. * @internal
  22. */
  23. class Attribute extends AbstractAnnotationDriver
  24. {
  25. /**
  26. * Mapping object to configure the translation model for a translatable class.
  27. */
  28. public const ENTITY_CLASS = TranslationEntity::class;
  29. /**
  30. * Mapping object to identify a field as translatable in a translatable class.
  31. */
  32. public const TRANSLATABLE = Translatable::class;
  33. /**
  34. * Mapping object to identify the field which stores the locale or language for the translation.
  35. *
  36. * This object is an alias of {@see self::LANGUAGE}
  37. */
  38. public const LOCALE = Locale::class;
  39. /**
  40. * Mapping object to identify the field which stores the locale or language for the translation.
  41. *
  42. * This object is an alias of {@see self::LOCALE}
  43. */
  44. public const LANGUAGE = Language::class;
  45. public function readExtendedMetadata($meta, array &$config)
  46. {
  47. $class = $this->getMetaReflectionClass($meta);
  48. // class annotations
  49. if ($annot = $this->reader->getClassAnnotation($class, self::ENTITY_CLASS)) {
  50. \assert($annot instanceof TranslationEntity);
  51. if (!$cl = $this->getRelatedClassName($meta, $annot->class)) {
  52. throw new InvalidMappingException("Translation class: {$annot->class} does not exist.");
  53. }
  54. $config['translationClass'] = $cl;
  55. }
  56. // property annotations
  57. foreach ($class->getProperties() as $property) {
  58. if ($meta->isMappedSuperclass && !$property->isPrivate()
  59. || $meta->isInheritedField($property->name)
  60. || isset($meta->associationMappings[$property->name]['inherited'])
  61. ) {
  62. continue;
  63. }
  64. // translatable property
  65. if ($translatable = $this->reader->getPropertyAnnotation($property, self::TRANSLATABLE)) {
  66. \assert($translatable instanceof Translatable);
  67. $field = $property->getName();
  68. if (!$meta->hasField($field)) {
  69. throw new InvalidMappingException("Unable to find translatable [{$field}] as mapped property in entity - {$meta->getName()}");
  70. }
  71. // fields cannot be overrided and throws mapping exception
  72. $config['fields'][] = $field;
  73. if (isset($translatable->fallback)) {
  74. $config['fallback'][$field] = $translatable->fallback;
  75. }
  76. }
  77. // locale property
  78. if ($this->reader->getPropertyAnnotation($property, self::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->getName()}, since it makes no sense");
  82. }
  83. $config['locale'] = $field;
  84. } elseif ($this->reader->getPropertyAnnotation($property, self::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->getName()}, since it makes no sense");
  88. }
  89. $config['locale'] = $field;
  90. }
  91. }
  92. // Embedded entity
  93. if (property_exists($meta, 'embeddedClasses') && $meta->embeddedClasses) {
  94. foreach ($meta->embeddedClasses as $propertyName => $embeddedClassInfo) {
  95. if ($meta->isInheritedEmbeddedClass($propertyName)) {
  96. continue;
  97. }
  98. /** Remove conditional when ORM 2.x is no longer supported. */
  99. $className = ($embeddedClassInfo instanceof EmbeddedClassMapping) ? $embeddedClassInfo->class : $embeddedClassInfo['class'];
  100. $embeddedClass = new \ReflectionClass($className);
  101. foreach ($embeddedClass->getProperties() as $embeddedProperty) {
  102. if ($translatable = $this->reader->getPropertyAnnotation($embeddedProperty, self::TRANSLATABLE)) {
  103. \assert($translatable instanceof Translatable);
  104. $field = $propertyName.'.'.$embeddedProperty->getName();
  105. $config['fields'][] = $field;
  106. if (isset($translatable->fallback)) {
  107. $config['fallback'][$field] = $translatable->fallback;
  108. }
  109. }
  110. }
  111. }
  112. }
  113. if (!$meta->isMappedSuperclass && $config) {
  114. if (is_array($meta->getIdentifier()) && count($meta->getIdentifier()) > 1) {
  115. throw new InvalidMappingException("Translatable does not support composite identifiers in class - {$meta->getName()}");
  116. }
  117. }
  118. return $config;
  119. }
  120. }