Annotation.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. * {@inheritDoc}
  46. */
  47. public function setAnnotationReader($reader)
  48. {
  49. $this->reader = $reader;
  50. }
  51. /**
  52. * {@inheritDoc}
  53. */
  54. public function validateFullMetadata(ClassMetadata $meta, array $config)
  55. {
  56. if ($config && is_array($meta->identifier) && count($meta->identifier) > 1) {
  57. throw new InvalidMappingException("Translatable does not support composite identifiers in class - {$meta->name}");
  58. }
  59. }
  60. /**
  61. * {@inheritDoc}
  62. */
  63. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  64. $class = $meta->getReflectionClass();
  65. // class annotations
  66. if ($annot = $this->reader->getClassAnnotation($class, self::ENTITY_CLASS)) {
  67. if (!class_exists($annot->class)) {
  68. throw new InvalidMappingException("Translation class: {$annot->class} does not exist.");
  69. }
  70. $config['translationClass'] = $annot->class;
  71. }
  72. // property annotations
  73. foreach ($class->getProperties() as $property) {
  74. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  75. $meta->isInheritedField($property->name) ||
  76. isset($meta->associationMappings[$property->name]['inherited'])
  77. ) {
  78. continue;
  79. }
  80. // translatable property
  81. if ($translatable = $this->reader->getPropertyAnnotation($property, self::TRANSLATABLE)) {
  82. $field = $property->getName();
  83. if (!$meta->hasField($field)) {
  84. throw new InvalidMappingException("Unable to find translatable [{$field}] as mapped property in entity - {$meta->name}");
  85. }
  86. // fields cannot be overrided and throws mapping exception
  87. $config['fields'][] = $field;
  88. }
  89. // locale property
  90. if ($locale = $this->reader->getPropertyAnnotation($property, self::LOCALE)) {
  91. $field = $property->getName();
  92. if ($meta->hasField($field)) {
  93. throw new InvalidMappingException("Locale field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  94. }
  95. $config['locale'] = $field;
  96. } elseif ($language = $this->reader->getPropertyAnnotation($property, self::LANGUAGE)) {
  97. $field = $property->getName();
  98. if ($meta->hasField($field)) {
  99. throw new InvalidMappingException("Language field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  100. }
  101. $config['locale'] = $field;
  102. }
  103. }
  104. }
  105. }