Xml.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. 'int',
  30. );
  31. /**
  32. * {@inheritDoc}
  33. */
  34. public function readExtendedMetadata($meta, array &$config)
  35. {
  36. /**
  37. * @var \SimpleXmlElement $xml
  38. */
  39. $xml = $this->_getMapping($meta->name);
  40. if (isset($xml->field)) {
  41. foreach ($xml->field as $mapping) {
  42. $mappingDoctrine = $mapping;
  43. /**
  44. * @var \SimpleXmlElement $mapping
  45. */
  46. $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
  47. $field = $this->_getAttribute($mappingDoctrine, 'name');
  48. if (isset($mapping->slug)) {
  49. /**
  50. * @var \SimpleXmlElement $slug
  51. */
  52. $slug = $mapping->slug;
  53. if (!$this->isValidField($meta, $field)) {
  54. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
  55. }
  56. $fields = array_map('trim', explode(',', (string)$this->_getAttribute($slug, 'fields')));
  57. foreach ($fields as $slugField) {
  58. if (!$meta->hasField($slugField)) {
  59. throw new InvalidMappingException("Unable to find slug [{$slugField}] as mapped property in entity - {$meta->name}");
  60. }
  61. if (!$this->isValidField($meta, $slugField)) {
  62. throw new InvalidMappingException("Cannot use field - [{$slugField}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
  63. }
  64. }
  65. // set all options
  66. $config['slugs'][$field] = array(
  67. 'fields' => $fields,
  68. 'slug' => $field,
  69. 'style' => $this->_isAttributeSet($slug, 'style') ?
  70. $this->_getAttribute($slug, 'style') : 'default',
  71. 'updatable' => $this->_isAttributeSet($slug, 'updatable') ?
  72. $this->_getBooleanAttribute($slug, 'updatable') : true,
  73. 'unique' => $this->_isAttributeSet($slug, 'unique') ?
  74. $this->_getBooleanAttribute($slug, 'unique') : true,
  75. 'separator' => $this->_isAttributeSet($slug, 'separator') ?
  76. $this->_getAttribute($slug, 'separator') : '-',
  77. );
  78. if (!$meta->isMappedSuperclass && $meta->isIdentifier($field) && !$config['slugs'][$field]['unique']) {
  79. throw new InvalidMappingException("Identifier field - [{$field}] slug must be unique in order to maintain primary key in class - {$meta->name}");
  80. }
  81. }
  82. }
  83. }
  84. }
  85. /**
  86. * Checks if $field type is valid as Sluggable field
  87. *
  88. * @param object $meta
  89. * @param string $field
  90. * @return boolean
  91. */
  92. protected function isValidField($meta, $field)
  93. {
  94. $mapping = $meta->getFieldMapping($field);
  95. return $mapping && in_array($mapping['type'], $this->validTypes);
  96. }
  97. }