123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace Gedmo\Translatable\Mapping\Driver;
- use Gedmo\Mapping\Driver,
- Doctrine\Common\Annotations\AnnotationReader,
- Gedmo\Exception\InvalidArgumentException;
- /**
- * This is an annotation mapping driver for Translatable
- * behavioral extension. Used for extraction of extended
- * metadata from Annotations specificaly for Translatable
- * extension.
- *
- * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
- * @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
- */
- protected $_validTypes = array(
- 'string',
- 'text'
- );
-
- /**
- * {@inheritDoc}
- */
- public function validateFullMetadata($meta, array $config) {}
-
- /**
- * {@inheritDoc}
- */
- public function readExtendedMetadata($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) ||
- isset($meta->associationMappings[$property->name]['inherited'])
- ) {
- 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 ClassMetadata $meta
- * @param string $field
- * @return boolean
- */
- protected function _isValidField($meta, $field)
- {
- $mapping = $meta->getFieldMapping($field);
- return $mapping && in_array($mapping['type'], $this->_validTypes);
- }
- }
|