Annotation.php 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class Annotation extends AbstractAnnotationDriver
  15. {
  16. /**
  17. * Annotation to identity translation entity to be used for translation storage
  18. */
  19. const ENTITY_CLASS = 'Gedmo\\Mapping\\Annotation\\TranslationEntity';
  20. /**
  21. * Annotation to identify field as translatable
  22. */
  23. const TRANSLATABLE = 'Gedmo\\Mapping\\Annotation\\Translatable';
  24. /**
  25. * Annotation to identify field which can store used locale or language
  26. * alias is LANGUAGE
  27. */
  28. const LOCALE = 'Gedmo\\Mapping\\Annotation\\Locale';
  29. /**
  30. * Annotation to identify field which can store used locale or language
  31. * alias is LOCALE
  32. */
  33. const LANGUAGE = 'Gedmo\\Mapping\\Annotation\\Language';
  34. /**
  35. * {@inheritDoc}
  36. */
  37. public function readExtendedMetadata($meta, array &$config)
  38. {
  39. $class = $this->getMetaReflectionClass($meta);
  40. // class annotations
  41. if ($annot = $this->reader->getClassAnnotation($class, self::ENTITY_CLASS)) {
  42. if (!class_exists($annot->class)) {
  43. throw new InvalidMappingException("Translation class: {$annot->class} does not exist.");
  44. }
  45. $config['translationClass'] = $annot->class;
  46. }
  47. // property annotations
  48. foreach ($class->getProperties() as $property) {
  49. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  50. $meta->isInheritedField($property->name) ||
  51. isset($meta->associationMappings[$property->name]['inherited'])
  52. ) {
  53. continue;
  54. }
  55. // translatable property
  56. if ($translatable = $this->reader->getPropertyAnnotation($property, self::TRANSLATABLE)) {
  57. $field = $property->getName();
  58. if (!$meta->hasField($field)) {
  59. throw new InvalidMappingException("Unable to find translatable [{$field}] as mapped property in entity - {$meta->name}");
  60. }
  61. // fields cannot be overrided and throws mapping exception
  62. $config['fields'][] = $field;
  63. if (isset($translatable->fallback)) {
  64. $config['fallback'][$field] = $translatable->fallback;
  65. }
  66. }
  67. // locale property
  68. if ($locale = $this->reader->getPropertyAnnotation($property, self::LOCALE)) {
  69. $field = $property->getName();
  70. if ($meta->hasField($field)) {
  71. throw new InvalidMappingException("Locale field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  72. }
  73. $config['locale'] = $field;
  74. } elseif ($language = $this->reader->getPropertyAnnotation($property, self::LANGUAGE)) {
  75. $field = $property->getName();
  76. if ($meta->hasField($field)) {
  77. throw new InvalidMappingException("Language field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  78. }
  79. $config['locale'] = $field;
  80. }
  81. }
  82. if (!$meta->isMappedSuperclass && $config) {
  83. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  84. throw new InvalidMappingException("Translatable does not support composite identifiers in class - {$meta->name}");
  85. }
  86. }
  87. }
  88. }