Annotation.php 4.4 KB

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