Annotation.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Gedmo\Translatable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AnnotationDriverInterface,
  4. Gedmo\Exception\InvalidMappingException;
  5. /**
  6. * This is an annotation mapping driver for Translatable
  7. * behavioral extension. Used for extraction of extended
  8. * metadata from Annotations specificaly for Translatable
  9. * extension.
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @package Gedmo.Translatable.Mapping.Driver
  13. * @subpackage Annotation
  14. * @link http://www.gediminasm.org
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class Annotation implements AnnotationDriverInterface
  18. {
  19. /**
  20. * Annotation to identity translation entity to be used for translation storage
  21. */
  22. const ENTITY_CLASS = 'Gedmo\\Mapping\\Annotation\\TranslationEntity';
  23. /**
  24. * Annotation to identify field as translatable
  25. */
  26. const TRANSLATABLE = 'Gedmo\\Mapping\\Annotation\\Translatable';
  27. /**
  28. * Annotation to identify field which can store used locale or language
  29. * alias is LANGUAGE
  30. */
  31. const LOCALE = 'Gedmo\\Mapping\\Annotation\\Locale';
  32. /**
  33. * Annotation to identify field which can store used locale or language
  34. * alias is LOCALE
  35. */
  36. const LANGUAGE = 'Gedmo\\Mapping\\Annotation\\Language';
  37. /**
  38. * Annotation reader instance
  39. *
  40. * @var object
  41. */
  42. private $reader;
  43. /**
  44. * original driver if it is available
  45. */
  46. protected $_originalDriver = null;
  47. /**
  48. * {@inheritDoc}
  49. */
  50. public function setAnnotationReader($reader)
  51. {
  52. $this->reader = $reader;
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function readExtendedMetadata($meta, array &$config)
  58. {
  59. $class = $meta->getReflectionClass();
  60. if (!$class) {
  61. // based on recent doctrine 2.3.0-DEV maybe will be fixed in some way
  62. // this happens when running annotation driver in combination with
  63. // static reflection services. This is not the nicest fix
  64. $class = new \ReflectionClass($meta->name);
  65. }
  66. // class annotations
  67. if ($annot = $this->reader->getClassAnnotation($class, self::ENTITY_CLASS)) {
  68. if (!class_exists($annot->class)) {
  69. throw new InvalidMappingException("Translation class: {$annot->class} does not exist.");
  70. }
  71. $config['translationClass'] = $annot->class;
  72. }
  73. // property annotations
  74. foreach ($class->getProperties() as $property) {
  75. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  76. $meta->isInheritedField($property->name) ||
  77. isset($meta->associationMappings[$property->name]['inherited'])
  78. ) {
  79. continue;
  80. }
  81. // translatable property
  82. if ($translatable = $this->reader->getPropertyAnnotation($property, self::TRANSLATABLE)) {
  83. $field = $property->getName();
  84. if (!$meta->hasField($field)) {
  85. throw new InvalidMappingException("Unable to find translatable [{$field}] as mapped property in entity - {$meta->name}");
  86. }
  87. // fields cannot be overrided and throws mapping exception
  88. $config['fields'][] = $field;
  89. if (false === $translatable->fallback) {
  90. $config['nofallback'][] = $field;
  91. }
  92. }
  93. // locale property
  94. if ($locale = $this->reader->getPropertyAnnotation($property, self::LOCALE)) {
  95. $field = $property->getName();
  96. if ($meta->hasField($field)) {
  97. throw new InvalidMappingException("Locale field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  98. }
  99. $config['locale'] = $field;
  100. } elseif ($language = $this->reader->getPropertyAnnotation($property, self::LANGUAGE)) {
  101. $field = $property->getName();
  102. if ($meta->hasField($field)) {
  103. throw new InvalidMappingException("Language field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  104. }
  105. $config['locale'] = $field;
  106. }
  107. }
  108. if (!$meta->isMappedSuperclass && $config) {
  109. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  110. throw new InvalidMappingException("Translatable does not support composite identifiers in class - {$meta->name}");
  111. }
  112. }
  113. }
  114. /**
  115. * Passes in the mapping read by original driver
  116. *
  117. * @param $driver
  118. * @return void
  119. */
  120. public function setOriginalDriver($driver)
  121. {
  122. $this->_originalDriver = $driver;
  123. }
  124. }