Xml.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Gedmo\Sortable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\Xml as BaseXml,
  4. Gedmo\Exception\InvalidMappingException;
  5. use Doctrine\Common\Persistence\Mapping\ClassMetadata;
  6. /**
  7. * This is a xml mapping driver for Sortable
  8. * behavioral extension. Used for extraction of extended
  9. * metadata from xml specificaly for Sortable
  10. * extension.
  11. *
  12. * @author Lukas Botsch <lukas.botsch@gmail.com>
  13. * @package Gedmo.Sortable.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. * List of types which are valid for position field
  22. *
  23. * @var array
  24. */
  25. private $validTypes = array(
  26. 'integer',
  27. 'smallint',
  28. 'bigint'
  29. );
  30. /**
  31. * {@inheritDoc}
  32. */
  33. public function readExtendedMetadata(ClassMetadata $meta, array &$config)
  34. {
  35. /**
  36. * @var \SimpleXmlElement $xml
  37. */
  38. $xml = $this->_getMapping($meta->name);
  39. if (isset($xml->field)) {
  40. foreach ($xml->field as $mapping) {
  41. $mappingDoctrine = $mapping;
  42. /**
  43. * @var \SimpleXmlElement $mapping
  44. */
  45. $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
  46. $field = $this->_getAttribute($mappingDoctrine, 'name');
  47. if (isset($mapping->{'sortable-position'})) {
  48. if (!$this->isValidField($meta, $field)) {
  49. throw new InvalidMappingException("Sortable position field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  50. }
  51. $config['position'] = $field;
  52. }
  53. }
  54. $this->readSortableGroups($xml->field, $config, 'name');
  55. }
  56. // Search for sortable-groups in association mappings
  57. if (isset($xml->{'many-to-one'})) {
  58. $this->readSortableGroups($xml->{'many-to-one'}, $config);
  59. }
  60. // Search for sortable-groups in association mappings
  61. if (isset($xml->{'many-to-many'})) {
  62. $this->readSortableGroups($xml->{'many-to-many'}, $config);
  63. }
  64. if (!$meta->isMappedSuperclass && $config) {
  65. if (!isset($config['position'])) {
  66. throw new InvalidMappingException("Missing property: 'position' in class - {$meta->name}");
  67. }
  68. }
  69. }
  70. private function readSortableGroups($mapping, array &$config, $fieldAttr='field')
  71. {
  72. foreach ($mapping as $map) {
  73. $mappingDoctrine = $map;
  74. /**
  75. * @var \SimpleXmlElement $mapping
  76. */
  77. $map = $map->children(self::GEDMO_NAMESPACE_URI);
  78. $field = $this->_getAttribute($mappingDoctrine, $fieldAttr);
  79. if (isset($map->{'sortable-group'})) {
  80. if (!isset($config['groups'])) {
  81. $config['groups'] = array();
  82. }
  83. $config['groups'][] = $field;
  84. }
  85. }
  86. }
  87. /**
  88. * Checks if $field type is valid as Sortable Position field
  89. *
  90. * @param ClassMetadata $meta
  91. * @param string $field
  92. * @return boolean
  93. */
  94. protected function isValidField($meta, $field)
  95. {
  96. $mapping = $meta->getFieldMapping($field);
  97. return $mapping && in_array($mapping['type'], $this->validTypes);
  98. }
  99. }