Yaml.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Gedmo\Translatable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\File;
  4. use Gedmo\Mapping\Driver;
  5. use Gedmo\Exception\InvalidMappingException;
  6. /**
  7. * This is a yaml mapping driver for Translatable
  8. * behavioral extension. Used for extraction of extended
  9. * metadata from yaml specifically for Translatable
  10. * extension.
  11. *
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class Yaml extends File implements Driver
  16. {
  17. /**
  18. * File extension
  19. * @var string
  20. */
  21. protected $_extension = '.dcm.yml';
  22. /**
  23. * {@inheritDoc}
  24. */
  25. public function readExtendedMetadata($meta, array &$config)
  26. {
  27. $mapping = $this->_getMapping($meta->name);
  28. if (isset($mapping['gedmo'])) {
  29. $classMapping = $mapping['gedmo'];
  30. if (isset($classMapping['translation']['entity'])) {
  31. $translationEntity = $classMapping['translation']['entity'];
  32. if (!$cl = $this->getRelatedClassName($meta, $translationEntity)) {
  33. throw new InvalidMappingException("Translation entity class: {$translationEntity} does not exist.");
  34. }
  35. $config['translationClass'] = $cl;
  36. }
  37. if (isset($classMapping['translation']['locale'])) {
  38. $config['locale'] = $classMapping['translation']['locale'];
  39. } elseif (isset($classMapping['translation']['language'])) {
  40. $config['locale'] = $classMapping['translation']['language'];
  41. }
  42. }
  43. if (isset($mapping['fields'])) {
  44. foreach ($mapping['fields'] as $field => $fieldMapping) {
  45. $this->buildFieldConfiguration($field, $fieldMapping, $config);
  46. }
  47. }
  48. if (isset($mapping['attributeOverride'])) {
  49. foreach ($mapping['attributeOverride'] as $field => $overrideMapping) {
  50. $this->buildFieldConfiguration($field, $overrideMapping, $config);
  51. }
  52. }
  53. if (!$meta->isMappedSuperclass && $config) {
  54. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  55. throw new InvalidMappingException("Translatable does not support composite identifiers in class - {$meta->name}");
  56. }
  57. }
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. protected function _loadMappingFile($file)
  63. {
  64. return \Symfony\Component\Yaml\Yaml::parse(file_get_contents($file));
  65. }
  66. private function buildFieldConfiguration($field, $fieldMapping, array &$config)
  67. {
  68. if (is_array($fieldMapping) && isset($fieldMapping['gedmo'])) {
  69. if (in_array('translatable', $fieldMapping['gedmo']) || isset($fieldMapping['gedmo']['translatable'])) {
  70. // fields cannot be overrided and throws mapping exception
  71. $config['fields'][] = $field;
  72. if (isset($fieldMapping['gedmo']['translatable']['fallback'])) {
  73. $config['fallback'][$field] = $fieldMapping['gedmo']['translatable']['fallback'];
  74. }
  75. }
  76. }
  77. }
  78. }