Annotation.php 5.0 KB

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