Xml.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. * @package Gedmo.Translatable.Mapping.Driver
  14. * @subpackage Xml
  15. * @link http://www.gediminasm.org
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. class Xml extends BaseXml
  19. {
  20. /**
  21. * {@inheritDoc}
  22. */
  23. public function readExtendedMetadata($meta, array &$config) {
  24. /**
  25. * @var \SimpleXmlElement $xml
  26. */
  27. $xml = $this->_getMapping($meta->name);
  28. $xmlDoctrine = $xml;
  29. $xml = $xml->children(self::GEDMO_NAMESPACE_URI);
  30. if (($xmlDoctrine->getName() == 'entity' || $xmlDoctrine->getName() == 'mapped-superclass')) {
  31. if (isset($xml->translation)) {
  32. /**
  33. * @var \SimpleXmlElement $data
  34. */
  35. $data = $xml->translation;
  36. if ($this->_isAttributeSet($data, 'locale')) {
  37. $config['locale'] = $this->_getAttribute($data, 'locale');
  38. } elseif ($this->_isAttributeSet($data, 'language')) {
  39. $config['locale'] = $this->_getAttribute($data, 'language');
  40. }
  41. if ($this->_isAttributeSet($data, 'entity')) {
  42. $entity = $this->_getAttribute($data, 'entity');
  43. if (!class_exists($entity)) {
  44. throw new InvalidMappingException("Translation entity class: {$entity} does not exist.");
  45. }
  46. $config['translationClass'] = $entity;
  47. }
  48. }
  49. }
  50. if (isset($xmlDoctrine->field)) {
  51. foreach ($xmlDoctrine->field as $mapping) {
  52. $mappingDoctrine = $mapping;
  53. /**
  54. * @var \SimpleXmlElement $mapping
  55. */
  56. $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
  57. $field = $this->_getAttribute($mappingDoctrine, 'name');
  58. if (isset($mapping->translatable)) {
  59. $config['fields'][] = $field;
  60. if (isset($mapping->nofallback)) {
  61. $config['nofallback'][] = $field;
  62. }
  63. }
  64. }
  65. }
  66. if (!$meta->isMappedSuperclass && $config) {
  67. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  68. throw new InvalidMappingException("Translatable does not support composite identifiers in class - {$meta->name}");
  69. }
  70. }
  71. }
  72. }