Annotation.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Gedmo\Translatable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AbstractAnnotationDriver,
  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 extends AbstractAnnotationDriver
  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. * {@inheritDoc}
  39. */
  40. public function readExtendedMetadata($meta, array &$config)
  41. {
  42. $class = $this->getMetaReflectionClass($meta);
  43. // class annotations
  44. if ($annot = $this->reader->getClassAnnotation($class, self::ENTITY_CLASS)) {
  45. if (!class_exists($annot->class)) {
  46. throw new InvalidMappingException("Translation class: {$annot->class} does not exist.");
  47. }
  48. $config['translationClass'] = $annot->class;
  49. }
  50. // property annotations
  51. foreach ($class->getProperties() as $property) {
  52. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  53. $meta->isInheritedField($property->name) ||
  54. isset($meta->associationMappings[$property->name]['inherited'])
  55. ) {
  56. continue;
  57. }
  58. // translatable property
  59. if ($translatable = $this->reader->getPropertyAnnotation($property, self::TRANSLATABLE)) {
  60. $field = $property->getName();
  61. if (!$meta->hasField($field)) {
  62. throw new InvalidMappingException("Unable to find translatable [{$field}] as mapped property in entity - {$meta->name}");
  63. }
  64. // fields cannot be overrided and throws mapping exception
  65. $config['fields'][] = $field;
  66. if (isset($translatable->fallback)) {
  67. $config['fallback'][$field] = $translatable->fallback;
  68. }
  69. }
  70. // locale property
  71. if ($locale = $this->reader->getPropertyAnnotation($property, self::LOCALE)) {
  72. $field = $property->getName();
  73. if ($meta->hasField($field)) {
  74. throw new InvalidMappingException("Locale field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  75. }
  76. $config['locale'] = $field;
  77. } elseif ($language = $this->reader->getPropertyAnnotation($property, self::LANGUAGE)) {
  78. $field = $property->getName();
  79. if ($meta->hasField($field)) {
  80. throw new InvalidMappingException("Language field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  81. }
  82. $config['locale'] = $field;
  83. }
  84. }
  85. if (!$meta->isMappedSuperclass && $config) {
  86. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  87. throw new InvalidMappingException("Translatable does not support composite identifiers in class - {$meta->name}");
  88. }
  89. }
  90. }
  91. }