Xml.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Gedmo\Translatable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\Xml as BaseXml;
  4. use Gedmo\Exception\InvalidMappingException;
  5. /**
  6. * This is a xml mapping driver for Translatable
  7. * behavioral extension. Used for extraction of extended
  8. * metadata from xml specifically for Translatable
  9. * extension.
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @author Miha Vrhovnik <miha.vrhovnik@gmail.com>
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class Xml extends BaseXml
  16. {
  17. /**
  18. * {@inheritDoc}
  19. */
  20. public function readExtendedMetadata($meta, array &$config)
  21. {
  22. /**
  23. * @var \SimpleXmlElement $xml
  24. */
  25. $xml = $this->_getMapping($meta->name);
  26. $xmlDoctrine = $xml;
  27. $xml = $xml->children(self::GEDMO_NAMESPACE_URI);
  28. if (($xmlDoctrine->getName() == 'entity' || $xmlDoctrine->getName() == 'mapped-superclass')) {
  29. if ($xml->count() && isset($xml->translation)) {
  30. /**
  31. * @var \SimpleXmlElement $data
  32. */
  33. $data = $xml->translation;
  34. if ($this->_isAttributeSet($data, 'locale')) {
  35. $config['locale'] = $this->_getAttribute($data, 'locale');
  36. } elseif ($this->_isAttributeSet($data, 'language')) {
  37. $config['locale'] = $this->_getAttribute($data, 'language');
  38. }
  39. if ($this->_isAttributeSet($data, 'entity')) {
  40. $entity = $this->_getAttribute($data, 'entity');
  41. if (!$cl = $this->getRelatedClassName($meta, $entity)) {
  42. throw new InvalidMappingException("Translation entity class: {$entity} does not exist.");
  43. }
  44. $config['translationClass'] = $cl;
  45. }
  46. }
  47. }
  48. if (property_exists($meta, 'embeddedClasses') && $meta->embeddedClasses) {
  49. foreach ($meta->embeddedClasses as $propertyName => $embeddedClassInfo) {
  50. $xmlEmbeddedClass = $this->_getMapping($embeddedClassInfo['class']);
  51. $this->inspectElementsForTranslatableFields($xmlEmbeddedClass, $config, $propertyName);
  52. }
  53. }
  54. if ($xmlDoctrine->{'attribute-overrides'}->count() > 0) {
  55. foreach ($xmlDoctrine->{'attribute-overrides'}->{'attribute-override'} as $overrideMapping) {
  56. $this->buildFieldConfiguration($this->_getAttribute($overrideMapping, 'name'), $overrideMapping->field, $config);
  57. }
  58. }
  59. $this->inspectElementsForTranslatableFields($xmlDoctrine, $config);
  60. if (!$meta->isMappedSuperclass && $config) {
  61. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  62. throw new InvalidMappingException("Translatable does not support composite identifiers in class - {$meta->name}");
  63. }
  64. }
  65. }
  66. private function inspectElementsForTranslatableFields(\SimpleXMLElement $xml, array &$config, $prefix = null)
  67. {
  68. if (!isset($xml->field)) {
  69. return;
  70. }
  71. foreach ($xml->field as $mapping) {
  72. $mappingDoctrine = $mapping;
  73. $fieldName = $this->_getAttribute($mappingDoctrine, 'name');
  74. if ($prefix !== null) {
  75. $fieldName = $prefix . '.' . $fieldName;
  76. }
  77. $this->buildFieldConfiguration($fieldName, $mapping, $config);
  78. }
  79. }
  80. private function buildFieldConfiguration($fieldName, \SimpleXMLElement $mapping, array &$config)
  81. {
  82. $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
  83. if ($mapping->count() > 0 && isset($mapping->translatable)) {
  84. $config['fields'][] = $fieldName;
  85. /** @var \SimpleXmlElement $data */
  86. $data = $mapping->translatable;
  87. if ($this->_isAttributeSet($data, 'fallback')) {
  88. $config['fallback'][$fieldName] = 'true' == $this->_getAttribute($data, 'fallback') ? true : false;
  89. }
  90. }
  91. }
  92. }