/**
  * Load metadata for a translatable class
  *
  * @param string $className
  * @param mixed $config
  * @return TranslatableMetadata|null
  */
 protected function loadTranslatableMetadata($className, $config)
 {
     if (!isset($config[$className]) || !isset($config[$className]['prezent']) || !array_key_exists('translatable', $config[$className]['prezent'])) {
         return;
     }
     $classMetadata = new TranslatableMetadata($className);
     $translatable = $config[$className]['prezent']['translatable'] ?: array();
     $propertyMetadata = new PropertyMetadata($className, isset($translatable['field']) ? $translatable['field'] : 'translations');
     // default targetEntity
     $targetEntity = $className . 'Translation';
     $classMetadata->targetEntity = isset($translatable['targetEntity']) ? $translatable['targetEntity'] : $targetEntity;
     $classMetadata->translations = $propertyMetadata;
     $classMetadata->addPropertyMetadata($propertyMetadata);
     if (isset($translatable['currentLocale'])) {
         $propertyMetadata = new PropertyMetadata($className, $translatable['currentLocale']);
         $classMetadata->currentLocale = $propertyMetadata;
         $classMetadata->addPropertyMetadata($propertyMetadata);
     }
     if (isset($translatable['fallbackLocale'])) {
         $propertyMetadata = new PropertyMetadata($className, $translatable['fallbackLocale']);
         $classMetadata->fallbackLocale = $propertyMetadata;
         $classMetadata->addPropertyMetadata($propertyMetadata);
     }
     return $classMetadata;
 }
 public function testTranslatableSerialization()
 {
     $currentProp = new PropertyMetadata('Prezent\\Tests\\Fixture\\Basic', 'currentLocale');
     $fallbackProp = new PropertyMetadata('Prezent\\Tests\\Fixture\\Basic', 'fallbackLocale');
     $transProp = new PropertyMetadata('Prezent\\Tests\\Fixture\\Basic', 'translations');
     $meta = new TranslatableMetadata('Prezent\\Tests\\Fixture\\Basic');
     $meta->targetEntity = 'Prezent\\Tests\\Fixture\\BasicTranslation';
     $meta->currentLocale = $currentProp;
     $meta->fallbackLocale = $fallbackProp;
     $meta->translations = $transProp;
     $meta->addPropertyMetadata($currentProp);
     $meta->addPropertyMetadata($fallbackProp);
     $meta->addPropertyMetadata($transProp);
     $string = serialize($meta);
     $copy = unserialize($string);
     $this->assertEquals($meta, $copy);
     $this->assertSame($meta->currentLocale, $meta->propertyMetadata['currentLocale']);
 }
 /**
  * Load metadata for a translatable class
  *
  * @param string $className
  * @param mixed  $config
  *
  * @throws \Exception
  * @return TranslatableMetadata|null
  */
 protected function loadTranslatableMetadata($className, $config)
 {
     if (!$config) {
         return;
     }
     $xml = new SimpleXMLElement($config);
     $xml->registerXPathNamespace('prezent', 'prezent');
     $nodeList = $xml->xpath('//prezent:translatable');
     if (0 == count($nodeList)) {
         return;
     }
     if (1 < count($nodeList)) {
         throw new \Exception("Configuration defined twice");
     }
     $node = $nodeList[0];
     $classMetadata = new TranslatableMetadata($className);
     $translatableField = (string) $node['field'];
     $propertyMetadata = new PropertyMetadata($className, !empty($translatableField) ? $translatableField : 'translations');
     // default targetEntity
     $targetEntity = $className . 'Translation';
     $translatableTargetEntity = (string) $node['target-entity'];
     $classMetadata->targetEntity = !empty($translatableTargetEntity) ? $translatableTargetEntity : $targetEntity;
     $classMetadata->translations = $propertyMetadata;
     $classMetadata->addPropertyMetadata($propertyMetadata);
     $currentLocale = (string) $node['current-locale'];
     if (!empty($currentLocale)) {
         $propertyMetadata = new PropertyMetadata($className, $currentLocale);
         $classMetadata->currentLocale = $propertyMetadata;
         $classMetadata->addPropertyMetadata($propertyMetadata);
     }
     $fallbackLocale = (string) $node['fallback-locale'];
     if (!empty($fallbackLocale)) {
         $propertyMetadata = new PropertyMetadata($className, $fallbackLocale);
         $classMetadata->fallbackLocale = $propertyMetadata;
         $classMetadata->addPropertyMetadata($propertyMetadata);
     }
     return $classMetadata;
 }
 /**
  * Load metadata for a translatable class
  *
  * @param \ReflectionClass $class
  * @return TranslatableMetadata
  */
 private function loadTranslatableMetadata(\ReflectionClass $class)
 {
     $classMetadata = new TranslatableMetadata($class->name);
     foreach ($class->getProperties() as $property) {
         if ($property->class !== $class->name) {
             continue;
         }
         $propertyMetadata = new PropertyMetadata($class->name, $property->getName());
         if ($this->reader->getPropertyAnnotation($property, 'Prezent\\Doctrine\\Translatable\\Annotation\\CurrentLocale')) {
             $classMetadata->currentLocale = $propertyMetadata;
             $classMetadata->addPropertyMetadata($propertyMetadata);
         }
         if ($this->reader->getPropertyAnnotation($property, 'Prezent\\Doctrine\\Translatable\\Annotation\\FallbackLocale')) {
             $classMetadata->fallbackLocale = $propertyMetadata;
             $classMetadata->addPropertyMetadata($propertyMetadata);
         }
         if ($annot = $this->reader->getPropertyAnnotation($property, 'Prezent\\Doctrine\\Translatable\\Annotation\\Translations')) {
             $classMetadata->targetEntity = $annot->targetEntity;
             $classMetadata->translations = $propertyMetadata;
             $classMetadata->addPropertyMetadata($propertyMetadata);
         }
     }
     return $classMetadata;
 }