* @package Gedmo.Translatable.Mapping.Driver * @subpackage Annotation * @link http://www.gediminasm.org * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ class Annotation implements Driver { /** * Annotation to identity translation entity to be used for translation storage */ const ANNOTATION_ENTITY_CLASS = 'Gedmo\Translatable\Mapping\TranslationEntity'; /** * Annotation to identify field as translatable */ const ANNOTATION_TRANSLATABLE = 'Gedmo\Translatable\Mapping\Translatable'; /** * Annotation to identify field which can store used locale or language * alias is ANNOTATION_LANGUAGE */ const ANNOTATION_LOCALE = 'Gedmo\Translatable\Mapping\Locale'; /** * Annotation to identify field which can store used locale or language * alias is ANNOTATION_LOCALE */ const ANNOTATION_LANGUAGE = 'Gedmo\Translatable\Mapping\Language'; /** * List of types which are valid for translation, * this property is public and you can add some * other types in case it needs translation * * @var array */ public $validTypes = array( 'string', 'text' ); /** * {@inheritDoc} */ public function validateFullMetadata(ClassMetadataInfo $meta, array $config) { } /** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadataInfo $meta, array &$config) { require_once __DIR__ . '/../Annotations.php'; $reader = new AnnotationReader(); $reader->setAnnotationNamespaceAlias('Gedmo\Translatable\Mapping\\', 'gedmo'); $class = $meta->getReflectionClass(); // class annotations $classAnnotations = $reader->getClassAnnotations($class); if (isset($classAnnotations[self::ANNOTATION_ENTITY_CLASS])) { $annot = $classAnnotations[self::ANNOTATION_ENTITY_CLASS]; if (!class_exists($annot->class)) { throw new InvalidArgumentException("Translation entity class: {$annot->class} does not exist."); } $config['translationClass'] = $annot->class; } // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || $meta->isInheritedAssociation($property->name) ) { continue; } // translatable property if ($translatable = $reader->getPropertyAnnotation($property, self::ANNOTATION_TRANSLATABLE)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidArgumentException("Unable to find translatable [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->_isValidField($meta, $field)) { throw new InvalidArgumentException("Translatable field - [{$field}] type is not valid and must be 'string' or 'text' in class - {$meta->name}"); } // fields cannot be overrided and throws mapping exception $config['fields'][] = $field; } // locale property if ($locale = $reader->getPropertyAnnotation($property, self::ANNOTATION_LOCALE)) { $field = $property->getName(); if ($meta->hasField($field)) { throw new InvalidArgumentException("Locale field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence"); } $config['locale'] = $field; } elseif ($language = $reader->getPropertyAnnotation($property, self::ANNOTATION_LANGUAGE)) { $field = $property->getName(); if ($meta->hasField($field)) { throw new InvalidArgumentException("Language field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence"); } $config['locale'] = $field; } } } /** * Checks if $field type is valid as Translatable field * * @param ClassMetadataInfo $meta * @param string $field * @return boolean */ protected function _isValidField(ClassMetadataInfo $meta, $field) { return in_array($meta->getTypeOfField($field), $this->validTypes); } }