Annotation.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 that does not require fallback
  29. */
  30. const NOFALLBACK = 'Gedmo\\Mapping\\Annotation\\NoFallback';
  31. /**
  32. * Annotation to identify field which can store used locale or language
  33. * alias is LANGUAGE
  34. */
  35. const LOCALE = 'Gedmo\\Mapping\\Annotation\\Locale';
  36. /**
  37. * Annotation to identify field which can store used locale or language
  38. * alias is LOCALE
  39. */
  40. const LANGUAGE = 'Gedmo\\Mapping\\Annotation\\Language';
  41. /**
  42. * Annotation reader instance
  43. *
  44. * @var object
  45. */
  46. private $reader;
  47. /**
  48. * original driver if it is available
  49. */
  50. protected $_originalDriver = null;
  51. /**
  52. * {@inheritDoc}
  53. */
  54. public function setAnnotationReader($reader)
  55. {
  56. $this->reader = $reader;
  57. }
  58. /**
  59. * {@inheritDoc}
  60. */
  61. public function readExtendedMetadata($meta, array &$config)
  62. {
  63. $class = $meta->getReflectionClass();
  64. if (!$class) {
  65. // based on recent doctrine 2.3.0-DEV maybe will be fixed in some way
  66. // this happens when running annotation driver in combination with
  67. // static reflection services. This is not the nicest fix
  68. $class = new \ReflectionClass($meta->name);
  69. }
  70. // class annotations
  71. if ($annot = $this->reader->getClassAnnotation($class, self::ENTITY_CLASS)) {
  72. if (!class_exists($annot->class)) {
  73. throw new InvalidMappingException("Translation class: {$annot->class} does not exist.");
  74. }
  75. $config['translationClass'] = $annot->class;
  76. }
  77. // property annotations
  78. foreach ($class->getProperties() as $property) {
  79. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  80. $meta->isInheritedField($property->name) ||
  81. isset($meta->associationMappings[$property->name]['inherited'])
  82. ) {
  83. continue;
  84. }
  85. // translatable property
  86. if ($translatable = $this->reader->getPropertyAnnotation($property, self::TRANSLATABLE)) {
  87. $field = $property->getName();
  88. if (!$meta->hasField($field)) {
  89. throw new InvalidMappingException("Unable to find translatable [{$field}] as mapped property in entity - {$meta->name}");
  90. }
  91. // fields cannot be overrided and throws mapping exception
  92. $config['fields'][] = $field;
  93. if ($this->reader->getPropertyAnnotation($property, self::NOFALLBACK)) {
  94. $config['nofallback'][] = $field;
  95. }
  96. }
  97. // locale property
  98. if ($locale = $this->reader->getPropertyAnnotation($property, self::LOCALE)) {
  99. $field = $property->getName();
  100. if ($meta->hasField($field)) {
  101. throw new InvalidMappingException("Locale field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  102. }
  103. $config['locale'] = $field;
  104. } elseif ($language = $this->reader->getPropertyAnnotation($property, self::LANGUAGE)) {
  105. $field = $property->getName();
  106. if ($meta->hasField($field)) {
  107. throw new InvalidMappingException("Language field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  108. }
  109. $config['locale'] = $field;
  110. }
  111. }
  112. if (!$meta->isMappedSuperclass && $config) {
  113. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  114. throw new InvalidMappingException("Translatable does not support composite identifiers in class - {$meta->name}");
  115. }
  116. }
  117. }
  118. /**
  119. * Passes in the mapping read by original driver
  120. *
  121. * @param $driver
  122. * @return void
  123. */
  124. public function setOriginalDriver($driver)
  125. {
  126. $this->_originalDriver = $driver;
  127. }
  128. }