Xml.php 3.3 KB

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