Yaml.php 3.3 KB

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