Yaml.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace Gedmo\Translatable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\File,
  4. Gedmo\Mapping\Driver,
  5. 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 specificaly 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 (!class_exists($translationEntity)) {
  33. throw new InvalidMappingException("Translation entity class: {$translationEntity} does not exist.");
  34. }
  35. $config['translationClass'] = $translationEntity;
  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. if (isset($fieldMapping['gedmo'])) {
  46. if (in_array('translatable', $fieldMapping['gedmo']) || isset($fieldMapping['gedmo']['translatable'])) {
  47. // fields cannot be overrided and throws mapping exception
  48. $config['fields'][] = $field;
  49. if (isset($fieldMapping['gedmo']['translatable']['fallback'])) {
  50. $config['fallback'][$field] = $fieldMapping['gedmo']['translatable']['fallback'];
  51. }
  52. }
  53. }
  54. }
  55. }
  56. if (!$meta->isMappedSuperclass && $config) {
  57. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  58. throw new InvalidMappingException("Translatable does not support composite identifiers in class - {$meta->name}");
  59. }
  60. }
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. protected function _loadMappingFile($file)
  66. {
  67. return \Symfony\Component\Yaml\Yaml::parse($file);
  68. }
  69. }