Annotation.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Gedmo\Translatable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver,
  4. Doctrine\Common\Annotations\AnnotationReader,
  5. Gedmo\Exception\InvalidArgumentException;
  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 Driver
  19. {
  20. /**
  21. * Annotation to identity translation entity to be used for translation storage
  22. */
  23. const ANNOTATION_ENTITY_CLASS = 'Gedmo\Translatable\Mapping\TranslationEntity';
  24. /**
  25. * Annotation to identify field as translatable
  26. */
  27. const ANNOTATION_TRANSLATABLE = 'Gedmo\Translatable\Mapping\Translatable';
  28. /**
  29. * Annotation to identify field which can store used locale or language
  30. * alias is ANNOTATION_LANGUAGE
  31. */
  32. const ANNOTATION_LOCALE = 'Gedmo\Translatable\Mapping\Locale';
  33. /**
  34. * Annotation to identify field which can store used locale or language
  35. * alias is ANNOTATION_LOCALE
  36. */
  37. const ANNOTATION_LANGUAGE = 'Gedmo\Translatable\Mapping\Language';
  38. /**
  39. * List of types which are valid for translation,
  40. * this property is public and you can add some
  41. * other types in case it needs translation
  42. *
  43. * @var array
  44. */
  45. protected $_validTypes = array(
  46. 'string',
  47. 'text'
  48. );
  49. /**
  50. * {@inheritDoc}
  51. */
  52. public function validateFullMetadata($meta, array $config) {}
  53. /**
  54. * {@inheritDoc}
  55. */
  56. public function readExtendedMetadata($meta, array &$config) {
  57. require_once __DIR__ . '/../Annotations.php';
  58. $reader = new AnnotationReader();
  59. $reader->setAnnotationNamespaceAlias('Gedmo\Translatable\Mapping\\', 'gedmo');
  60. $class = $meta->getReflectionClass();
  61. // class annotations
  62. $classAnnotations = $reader->getClassAnnotations($class);
  63. if (isset($classAnnotations[self::ANNOTATION_ENTITY_CLASS])) {
  64. $annot = $classAnnotations[self::ANNOTATION_ENTITY_CLASS];
  65. if (!class_exists($annot->class)) {
  66. throw new InvalidArgumentException("Translation entity class: {$annot->class} does not exist.");
  67. }
  68. $config['translationClass'] = $annot->class;
  69. }
  70. // property annotations
  71. foreach ($class->getProperties() as $property) {
  72. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  73. $meta->isInheritedField($property->name) ||
  74. isset($meta->associationMappings[$property->name]['inherited'])
  75. ) {
  76. continue;
  77. }
  78. // translatable property
  79. if ($translatable = $reader->getPropertyAnnotation($property, self::ANNOTATION_TRANSLATABLE)) {
  80. $field = $property->getName();
  81. if (!$meta->hasField($field)) {
  82. throw new InvalidArgumentException("Unable to find translatable [{$field}] as mapped property in entity - {$meta->name}");
  83. }
  84. if (!$this->_isValidField($meta, $field)) {
  85. throw new InvalidArgumentException("Translatable field - [{$field}] type is not valid and must be 'string' or 'text' in class - {$meta->name}");
  86. }
  87. // fields cannot be overrided and throws mapping exception
  88. $config['fields'][] = $field;
  89. }
  90. // locale property
  91. if ($locale = $reader->getPropertyAnnotation($property, self::ANNOTATION_LOCALE)) {
  92. $field = $property->getName();
  93. if ($meta->hasField($field)) {
  94. throw new InvalidArgumentException("Locale field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  95. }
  96. $config['locale'] = $field;
  97. } elseif ($language = $reader->getPropertyAnnotation($property, self::ANNOTATION_LANGUAGE)) {
  98. $field = $property->getName();
  99. if ($meta->hasField($field)) {
  100. throw new InvalidArgumentException("Language field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence");
  101. }
  102. $config['locale'] = $field;
  103. }
  104. }
  105. }
  106. /**
  107. * Checks if $field type is valid as Translatable field
  108. *
  109. * @param ClassMetadata $meta
  110. * @param string $field
  111. * @return boolean
  112. */
  113. protected function _isValidField($meta, $field)
  114. {
  115. $mapping = $meta->getFieldMapping($field);
  116. return $mapping && in_array($mapping['type'], $this->_validTypes);
  117. }
  118. }