Xml.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Gedmo\Translatable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\Xml as BaseXml,
  4. 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 specificaly 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. * @var \SimpleXmlElement $xml
  23. */
  24. $xml = $this->_getMapping($meta->name);
  25. $xmlDoctrine = $xml;
  26. $xml = $xml->children(self::GEDMO_NAMESPACE_URI);
  27. if (($xmlDoctrine->getName() == 'entity' || $xmlDoctrine->getName() == 'mapped-superclass')) {
  28. if (isset($xml->translation)) {
  29. /**
  30. * @var \SimpleXmlElement $data
  31. */
  32. $data = $xml->translation;
  33. if ($this->_isAttributeSet($data, 'locale')) {
  34. $config['locale'] = $this->_getAttribute($data, 'locale');
  35. } elseif ($this->_isAttributeSet($data, 'language')) {
  36. $config['locale'] = $this->_getAttribute($data, 'language');
  37. }
  38. if ($this->_isAttributeSet($data, 'entity')) {
  39. $entity = $this->_getAttribute($data, 'entity');
  40. if (!class_exists($entity)) {
  41. throw new InvalidMappingException("Translation entity class: {$entity} does not exist.");
  42. }
  43. $config['translationClass'] = $entity;
  44. }
  45. }
  46. }
  47. if (isset($xmlDoctrine->field)) {
  48. foreach ($xmlDoctrine->field as $mapping) {
  49. $mappingDoctrine = $mapping;
  50. /**
  51. * @var \SimpleXmlElement $mapping
  52. */
  53. $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
  54. $field = $this->_getAttribute($mappingDoctrine, 'name');
  55. if (isset($mapping->translatable)) {
  56. $config['fields'][] = $field;
  57. /** @var \SimpleXmlElement $data */
  58. $data = $mapping->translatable;
  59. if ($this->_isAttributeSet($data, 'fallback')) {
  60. $config['fallback'][$field] = 'true' == $this->_getAttribute($data, 'fallback') ? true : false;
  61. }
  62. }
  63. }
  64. }
  65. if (!$meta->isMappedSuperclass && $config) {
  66. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  67. throw new InvalidMappingException("Translatable does not support composite identifiers in class - {$meta->name}");
  68. }
  69. }
  70. }
  71. }