Xml.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Gedmo\Sluggable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\Xml as BaseXml,
  4. Gedmo\Exception\InvalidMappingException;
  5. /**
  6. * This is a xml mapping driver for Sluggable
  7. * behavioral extension. Used for extraction of extended
  8. * metadata from xml specificaly for Sluggable
  9. * extension.
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @author Miha Vrhovnik <miha.vrhovnik@gmail.com>
  13. * @package Gedmo.Sluggable.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 slug and sluggable fields
  22. *
  23. * @var array
  24. */
  25. private $validTypes = array(
  26. 'string',
  27. 'text',
  28. 'integer'
  29. );
  30. /**
  31. * {@inheritDoc}
  32. */
  33. public function readExtendedMetadata($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->slug)) {
  48. /**
  49. * @var \SimpleXmlElement $slug
  50. */
  51. $slug = $mapping->slug;
  52. if (!$this->isValidField($meta, $field)) {
  53. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
  54. }
  55. $fields = array_map('trim', explode(',', (string)$this->_getAttribute($slug, 'fields')));
  56. foreach ($fields as $slugField) {
  57. if (!$meta->hasField($slugField)) {
  58. throw new InvalidMappingException("Unable to find slug [{$slugField}] as mapped property in entity - {$meta->name}");
  59. }
  60. if (!$this->isValidField($meta, $slugField)) {
  61. throw new InvalidMappingException("Cannot use field - [{$slugField}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
  62. }
  63. }
  64. $handlers = array();
  65. if (isset($slug->handler)) {
  66. foreach ($slug->handler as $handler) {
  67. $class = (string)$this->_getAttribute($handler, 'class');
  68. $handlers[$class] = array();
  69. foreach ($handler->{'handler-option'} as $option) {
  70. $handlers[$class][(string)$this->_getAttribute($option, 'name')]
  71. = (string)$this->_getAttribute($option, 'value')
  72. ;
  73. }
  74. $class::validate($handlers[$class], $meta);
  75. }
  76. }
  77. // set all options
  78. $config['slugs'][$field] = array(
  79. 'fields' => $fields,
  80. 'slug' => $field,
  81. 'style' => $this->_isAttributeSet($slug, 'style') ?
  82. $this->_getAttribute($slug, 'style') : 'default',
  83. 'updatable' => $this->_isAttributeSet($slug, 'updatable') ?
  84. (bool)$this->_getAttribute($slug, 'updatable') : true,
  85. 'unique' => $this->_isAttributeSet($slug, 'unique') ?
  86. (bool)$this->_getAttribute($slug, 'unique') : true,
  87. 'separator' => $this->_isAttributeSet($slug, 'separator') ?
  88. $this->_getAttribute($slug, 'separator') : '-',
  89. 'handlers' => $handlers
  90. );
  91. if ($meta->isIdentifier($field) && !$config['slugs'][$field]['unique']) {
  92. throw new InvalidMappingException("Identifier field - [{$field}] slug must be unique in order to maintain primary key in class - {$meta->name}");
  93. }
  94. }
  95. }
  96. }
  97. }
  98. /**
  99. * Checks if $field type is valid as Sluggable field
  100. *
  101. * @param object $meta
  102. * @param string $field
  103. * @return boolean
  104. */
  105. protected function isValidField($meta, $field)
  106. {
  107. $mapping = $meta->getFieldMapping($field);
  108. return $mapping && in_array($mapping['type'], $this->validTypes);
  109. }
  110. }