Xml.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 validateFullMetadata($meta, array $config)
  33. {
  34. if ($config && !isset($config['position'])) {
  35. throw new InvalidMappingException("Missing property: 'position' in class - {$meta->name}");
  36. }
  37. }
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public function readExtendedMetadata($meta, array &$config)
  42. {
  43. /**
  44. * @var \SimpleXmlElement $xml
  45. */
  46. $xml = $this->_getMapping($meta->name);
  47. if (isset($xml->field)) {
  48. foreach ($xml->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->{'sortable-position'})) {
  56. if (!$this->isValidField($meta, $field)) {
  57. throw new InvalidMappingException("Sortable position field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  58. }
  59. $config['position'] = $field;
  60. } elseif (isset($mapping->{'sortable-group'})) {
  61. if (!isset($config['groups'])) {
  62. $config['groups'] = array();
  63. }
  64. $config['groups'][] = $field;
  65. }
  66. }
  67. }
  68. }
  69. /**
  70. * Checks if $field type is valid as Sluggable field
  71. *
  72. * @param ClassMetadata $meta
  73. * @param string $field
  74. * @return boolean
  75. */
  76. protected function isValidField($meta, $field)
  77. {
  78. $mapping = $meta->getFieldMapping($field);
  79. return $mapping && in_array($mapping['type'], $this->validTypes);
  80. }
  81. }