Yaml.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Gedmo\Translatable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\File,
  4. Gedmo\Mapping\Driver,
  5. Doctrine\ORM\Mapping\ClassMetadataInfo,
  6. Gedmo\Translatable\Mapping\MappingException;
  7. /**
  8. * This is a yaml mapping driver for Translatable
  9. * behavioral extension. Used for extraction of extended
  10. * metadata from yaml specificaly for Translatable
  11. * extension.
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @package Gedmo.Translatable.Mapping.Driver
  15. * @subpackage Yaml
  16. * @link http://www.gediminasm.org
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. class Yaml extends File implements Driver
  20. {
  21. /**
  22. * File extension
  23. * @var string
  24. */
  25. protected $_extension = '.dcm.yml';
  26. /**
  27. * List of types which are valid for translation,
  28. * this property is public and you can add some
  29. * other types in case it needs translation
  30. *
  31. * @var array
  32. */
  33. public $validTypes = array(
  34. 'string',
  35. 'text'
  36. );
  37. /**
  38. * (non-PHPdoc)
  39. * @see Gedmo\Mapping.Driver::validateFullMetadata()
  40. */
  41. public function validateFullMetadata(ClassMetadataInfo $meta, array $config)
  42. {
  43. }
  44. /**
  45. * (non-PHPdoc)
  46. * @see Gedmo\Mapping.Driver::readExtendedMetadata()
  47. */
  48. public function readExtendedMetadata(ClassMetadataInfo $meta, array &$config) {
  49. $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
  50. $mapping = $yaml[$meta->name];
  51. if (isset($mapping['gedmo'])) {
  52. $classMapping = $mapping['gedmo'];
  53. if (isset($classMapping['translatable']['entity'])) {
  54. $translationEntity = $classMapping['translatable']['entity'];
  55. if (!class_exists($translationEntity)) {
  56. throw MappingException::translationClassNotFound($translationEntity);
  57. }
  58. $config['translationClass'] = $translationEntity;
  59. }
  60. if (isset($classMapping['translatable']['locale'])) {
  61. $config['locale'] = $classMapping['translatable']['locale'];
  62. } elseif (isset($classMapping['translatable']['language'])) {
  63. $config['locale'] = $classMapping['translatable']['language'];
  64. }
  65. }
  66. if (isset($mapping['fields'])) {
  67. foreach ($mapping['fields'] as $field => $fieldMapping) {
  68. if (isset($fieldMapping['gedmo'])) {
  69. if (in_array('translatable', $fieldMapping['gedmo'])) {
  70. if (!$this->_isValidField($meta, $field)) {
  71. throw MappingException::notValidFieldType($field, $meta->name);
  72. }
  73. // fields cannot be overrided and throws mapping exception
  74. $config['fields'][] = $field;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * (non-PHPdoc)
  82. * @see Gedmo\Mapping\Driver.File::_loadMappingFile()
  83. */
  84. protected function _loadMappingFile($file)
  85. {
  86. return \Symfony\Component\Yaml\Yaml::load($file);
  87. }
  88. /**
  89. * Checks if $field type is valid as Translatable field
  90. *
  91. * @param ClassMetadataInfo $meta
  92. * @param string $field
  93. * @return boolean
  94. */
  95. protected function _isValidField(ClassMetadataInfo $meta, $field)
  96. {
  97. return in_array($meta->getTypeOfField($field), $this->validTypes);
  98. }
  99. }