Xml.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 validateFullMetadata(ClassMetadata $meta, array $config)
  34. {
  35. if ($config && !isset($config['position'])) {
  36. throw new InvalidMappingException("Missing property: 'position' in class - {$meta->name}");
  37. }
  38. }
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function readExtendedMetadata(ClassMetadata $meta, array &$config)
  43. {
  44. /**
  45. * @var \SimpleXmlElement $xml
  46. */
  47. $xml = $this->_getMapping($meta->name);
  48. if (isset($xml->field)) {
  49. foreach ($xml->field as $mapping) {
  50. $mappingDoctrine = $mapping;
  51. /**
  52. * @var \SimpleXmlElement $mapping
  53. */
  54. $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
  55. $field = $this->_getAttribute($mappingDoctrine, 'name');
  56. if (isset($mapping->{'sortable-position'})) {
  57. if (!$this->isValidField($meta, $field)) {
  58. throw new InvalidMappingException("Sortable position field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  59. }
  60. $config['position'] = $field;
  61. }
  62. }
  63. $this->readSortableGroups($xml->field, $config, 'name');
  64. }
  65. // Search for sortable-groups in association mappings
  66. if (isset($xml->{'many-to-one'})) {
  67. $this->readSortableGroups($xml->{'many-to-one'}, $config);
  68. }
  69. // Search for sortable-groups in association mappings
  70. if (isset($xml->{'many-to-many'})) {
  71. $this->readSortableGroups($xml->{'many-to-many'}, $config);
  72. }
  73. }
  74. private function readSortableGroups($mapping, array &$config, $fieldAttr='field')
  75. {
  76. foreach ($mapping as $map) {
  77. $mappingDoctrine = $map;
  78. /**
  79. * @var \SimpleXmlElement $mapping
  80. */
  81. $map = $map->children(self::GEDMO_NAMESPACE_URI);
  82. $field = $this->_getAttribute($mappingDoctrine, $fieldAttr);
  83. if (isset($map->{'sortable-group'})) {
  84. if (!isset($config['groups'])) {
  85. $config['groups'] = array();
  86. }
  87. $config['groups'][] = $field;
  88. }
  89. }
  90. }
  91. /**
  92. * Checks if $field type is valid as Sortable Position field
  93. *
  94. * @param ClassMetadata $meta
  95. * @param string $field
  96. * @return boolean
  97. */
  98. protected function isValidField($meta, $field)
  99. {
  100. $mapping = $meta->getFieldMapping($field);
  101. return $mapping && in_array($mapping['type'], $this->validTypes);
  102. }
  103. }