Annotation.php 4.5 KB

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