Annotation.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Gedmo\Translatable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver,
  4. Doctrine\Common\Persistence\Mapping\ClassMetadata,
  5. Doctrine\Common\Annotations\AnnotationReader,
  6. Gedmo\Exception\InvalidMappingException;
  7. /**
  8. * This is an annotation mapping driver for Translatable
  9. * behavioral extension. Used for extraction of extended
  10. * metadata from Annotations specificaly for Translatable
  11. * extension.
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @package Gedmo.Translatable.Mapping.Driver
  15. * @subpackage Annotation
  16. * @link http://www.gediminasm.org
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. class Annotation implements Driver
  20. {
  21. /**
  22. * Annotation to identity translation entity to be used for translation storage
  23. */
  24. const ANNOTATION_ENTITY_CLASS = 'Gedmo\Translatable\Mapping\TranslationEntity';
  25. /**
  26. * Annotation to identify field as translatable
  27. */
  28. const ANNOTATION_TRANSLATABLE = 'Gedmo\Translatable\Mapping\Translatable';
  29. /**
  30. * Annotation to identify field which can store used locale or language
  31. * alias is ANNOTATION_LANGUAGE
  32. */
  33. const ANNOTATION_LOCALE = 'Gedmo\Translatable\Mapping\Locale';
  34. /**
  35. * Annotation to identify field which can store used locale or language
  36. * alias is ANNOTATION_LOCALE
  37. */
  38. const ANNOTATION_LANGUAGE = 'Gedmo\Translatable\Mapping\Language';
  39. /**
  40. * List of types which are valid for translation,
  41. * this property is public and you can add some
  42. * other types in case it needs translation
  43. *
  44. * @var array
  45. */
  46. protected $validTypes = array(
  47. 'string',
  48. 'text'
  49. );
  50. /**
  51. * {@inheritDoc}
  52. */
  53. public function validateFullMetadata(ClassMetadata $meta, array $config) {}
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  58. require_once __DIR__ . '/../Annotations.php';
  59. $reader = new AnnotationReader();
  60. $reader->setAnnotationNamespaceAlias('Gedmo\Translatable\Mapping\\', 'gedmo');
  61. $class = $meta->getReflectionClass();
  62. // class annotations
  63. $classAnnotations = $reader->getClassAnnotations($class);
  64. if (isset($classAnnotations[self::ANNOTATION_ENTITY_CLASS])) {
  65. $annot = $classAnnotations[self::ANNOTATION_ENTITY_CLASS];
  66. if (!class_exists($annot->class)) {
  67. throw new InvalidMappingException("Translation class: {$annot->class} does not exist.");
  68. }
  69. $config['translationClass'] = $annot->class;
  70. }
  71. // property annotations
  72. foreach ($class->getProperties() as $property) {
  73. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  74. $meta->isInheritedField($property->name) ||
  75. isset($meta->associationMappings[$property->name]['inherited'])
  76. ) {
  77. continue;
  78. }
  79. // translatable property
  80. if ($translatable = $reader->getPropertyAnnotation($property, self::ANNOTATION_TRANSLATABLE)) {
  81. $field = $property->getName();
  82. if (!$meta->hasField($field)) {
  83. throw new InvalidMappingException("Unable to find translatable [{$field}] as mapped property in entity - {$meta->name}");
  84. }
  85. if (!$this->isValidField($meta, $field)) {
  86. throw new InvalidMappingException("Translatable field - [{$field}] type is not valid and must be 'string' or 'text' in class - {$meta->name}");
  87. }
  88. // fields cannot be overrided and throws mapping exception
  89. $config['fields'][] = $field;
  90. }
  91. // locale property
  92. if ($locale = $reader->getPropertyAnnotation($property, self::ANNOTATION_LOCALE)) {
  93. $field = $property->getName();
  94. if ($meta->hasField($field)) {
  95. throw new InvalidMappingException("Locale field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  96. }
  97. $config['locale'] = $field;
  98. } elseif ($language = $reader->getPropertyAnnotation($property, self::ANNOTATION_LANGUAGE)) {
  99. $field = $property->getName();
  100. if ($meta->hasField($field)) {
  101. throw new InvalidMappingException("Language field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  102. }
  103. $config['locale'] = $field;
  104. }
  105. }
  106. }
  107. /**
  108. * Checks if $field type is valid as Translatable field
  109. *
  110. * @param ClassMetadata $meta
  111. * @param string $field
  112. * @return boolean
  113. */
  114. protected function isValidField(ClassMetadata $meta, $field)
  115. {
  116. $mapping = $meta->getFieldMapping($field);
  117. return $mapping && in_array($mapping['type'], $this->validTypes);
  118. }
  119. }