vendor/gedmo/doctrine-extensions/src/Translatable/Mapping/Driver/Annotation.php line 53

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