Annotation.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 readExtendedMetadata(ClassMetadata $meta, array &$config) {
  59. $class = $meta->getReflectionClass();
  60. // class annotations
  61. if ($annot = $this->reader->getClassAnnotation($class, self::ENTITY_CLASS)) {
  62. if (!class_exists($annot->class)) {
  63. throw new InvalidMappingException("Translation class: {$annot->class} does not exist.");
  64. }
  65. $config['translationClass'] = $annot->class;
  66. }
  67. // property annotations
  68. foreach ($class->getProperties() as $property) {
  69. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  70. $meta->isInheritedField($property->name) ||
  71. isset($meta->associationMappings[$property->name]['inherited'])
  72. ) {
  73. continue;
  74. }
  75. // translatable property
  76. if ($translatable = $this->reader->getPropertyAnnotation($property, self::TRANSLATABLE)) {
  77. $field = $property->getName();
  78. if (!$meta->hasField($field)) {
  79. throw new InvalidMappingException("Unable to find translatable [{$field}] as mapped property in entity - {$meta->name}");
  80. }
  81. // fields cannot be overrided and throws mapping exception
  82. $config['fields'][] = $field;
  83. }
  84. // locale property
  85. if ($locale = $this->reader->getPropertyAnnotation($property, self::LOCALE)) {
  86. $field = $property->getName();
  87. if ($meta->hasField($field)) {
  88. throw new InvalidMappingException("Locale field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  89. }
  90. $config['locale'] = $field;
  91. } elseif ($language = $this->reader->getPropertyAnnotation($property, self::LANGUAGE)) {
  92. $field = $property->getName();
  93. if ($meta->hasField($field)) {
  94. throw new InvalidMappingException("Language field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  95. }
  96. $config['locale'] = $field;
  97. }
  98. }
  99. if (!$meta->isMappedSuperclass && $config) {
  100. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  101. throw new InvalidMappingException("Translatable does not support composite identifiers in class - {$meta->name}");
  102. }
  103. }
  104. }
  105. /**
  106. * Passes in the mapping read by original driver
  107. *
  108. * @param $driver
  109. * @return void
  110. */
  111. public function setOriginalDriver($driver)
  112. {
  113. $this->_originalDriver = $driver;
  114. }
  115. }