Xml.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Gedmo\Sluggable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\Xml as BaseXml,
  4. Doctrine\Common\Persistence\Mapping\ClassMetadata,
  5. Gedmo\Exception\InvalidMappingException;
  6. /**
  7. * This is a xml mapping driver for Sluggable
  8. * behavioral extension. Used for extraction of extended
  9. * metadata from xml specificaly for Sluggable
  10. * extension.
  11. *
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @author Miha Vrhovnik <miha.vrhovnik@gmail.com>
  14. * @package Gedmo.Sluggable.Mapping.Driver
  15. * @subpackage Xml
  16. * @link http://www.gediminasm.org
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. class Xml extends BaseXml
  20. {
  21. /**
  22. * List of types which are valid for slug and sluggable fields
  23. *
  24. * @var array
  25. */
  26. private $validTypes = array(
  27. 'string',
  28. 'text'
  29. );
  30. /**
  31. * {@inheritDoc}
  32. */
  33. public function validateFullMetadata(ClassMetadata $meta, array $config)
  34. {
  35. if ($config && !isset($config['fields'])) {
  36. throw new InvalidMappingException("Unable to find any sluggable fields specified for Sluggable entity - {$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->sluggable)) {
  57. if (!$this->isValidField($meta, $field)) {
  58. throw new InvalidMappingException("Cannot slug field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}");
  59. }
  60. <<<<<<< HEAD
  61. $options = array('position'=>false, 'field'=>$field, 'slugField'=>'slug');
  62. if ($this->_isAttributeSet($mapping->sluggable, 'position')) {
  63. $options['position'] = (int)$this->_getAttribute($mapping->sluggable, 'position');
  64. }
  65. if ($this->_isAttributeSet($mapping->sluggable, 'slugField')) {
  66. $options['slugField'] = $this->_getAttribute($mapping->sluggable, 'slugField');
  67. }
  68. =======
  69. $options = array('position' => false, 'field' => $field, 'slugField' => 'slug');
  70. if ($this->_isAttributeSet($mapping->sluggable, 'position')) {
  71. $options['position'] = (int)$this->_getAttribute($mapping->sluggable, 'position');
  72. }
  73. if ($this->_isAttributeSet($mapping->sluggable, 'slugField')) {
  74. $options['slugField'] = $this->_getAttribute($mapping->sluggable, 'slugField');
  75. }
  76. >>>>>>> multiple_slugs_1
  77. $config['fields'][$options['slugField']][] = $options;
  78. } elseif (isset($mapping->slug)) {
  79. /**
  80. * @var \SimpleXmlElement $slug
  81. */
  82. $slug = $mapping->slug;
  83. if (!$this->isValidField($meta, $field)) {
  84. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
  85. }
  86. if (isset($config['slug'])) {
  87. throw new InvalidMappingException("There cannot be two slug fields: [{$slug}] and [{$config['slug']}], in class - {$meta->name}.");
  88. }
  89. $config['slugFields'][$field]['slug'] = $field;
  90. $config['slugFields'][$field]['style'] = $this->_isAttributeSet($slug, 'style') ?
  91. $this->_getAttribute($slug, 'style') : 'default';
  92. $config['slugFields'][$field]['updatable'] = $this->_isAttributeSet($slug, 'updatable') ?
  93. (bool)$this->_getAttribute($slug, 'updatable') : true;
  94. $config['slugFields'][$field]['unique'] = $this->_isAttributeSet($slug, 'unique') ?
  95. (bool)$this->_getAttribute($slug, 'unique') : true;
  96. $config['slugFields'][$field]['separator'] = $this->_isAttributeSet($slug, 'separator') ?
  97. $this->_getAttribute($slug, 'separator') : '-';
  98. }
  99. }
  100. }
  101. }
  102. /**
  103. * Checks if $field type is valid as Sluggable field
  104. *
  105. * @param ClassMetadata $meta
  106. * @param string $field
  107. * @return boolean
  108. */
  109. protected function isValidField(ClassMetadata $meta, $field)
  110. {
  111. $mapping = $meta->getFieldMapping($field);
  112. return $mapping && in_array($mapping['type'], $this->validTypes);
  113. }
  114. }