Xml.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. 'integer'
  30. );
  31. /**
  32. * {@inheritDoc}
  33. */
  34. public function validateFullMetadata(ClassMetadata $meta, array $config)
  35. {
  36. if ($config) {
  37. if (!isset($config['fields'])) {
  38. throw new InvalidMappingException("Unable to find any sluggable fields specified for Sluggable entity - {$meta->name}");
  39. }
  40. foreach ($config['fields'] as $slugField => $fields) {
  41. if (!isset($config['slugFields'][$slugField])) {
  42. throw new InvalidMappingException("Unable to find {$slugField} slugField specified for Sluggable entity - {$meta->name}, you should specify slugField annotation property");
  43. }
  44. }
  45. }
  46. }
  47. /**
  48. * {@inheritDoc}
  49. */
  50. public function readExtendedMetadata(ClassMetadata $meta, array &$config)
  51. {
  52. /**
  53. * @var \SimpleXmlElement $xml
  54. */
  55. $xml = $this->_getMapping($meta->name);
  56. if (isset($xml->field)) {
  57. foreach ($xml->field as $mapping) {
  58. $mappingDoctrine = $mapping;
  59. /**
  60. * @var \SimpleXmlElement $mapping
  61. */
  62. $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
  63. $field = $this->_getAttribute($mappingDoctrine, 'name');
  64. if (isset($mapping->sluggable)) {
  65. if (!$this->isValidField($meta, $field)) {
  66. throw new InvalidMappingException("Cannot slug field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}");
  67. }
  68. $options = array('position' => false, 'field' => $field, 'slugField' => 'slug');
  69. if ($this->_isAttributeSet($mapping->sluggable, 'position')) {
  70. $options['position'] = (int)$this->_getAttribute($mapping->sluggable, 'position');
  71. }
  72. if ($this->_isAttributeSet($mapping->sluggable, 'slugField')) {
  73. $options['slugField'] = $this->_getAttribute($mapping->sluggable, 'slugField');
  74. }
  75. $config['fields'][$options['slugField']][] = $options;
  76. } elseif (isset($mapping->slug)) {
  77. /**
  78. * @var \SimpleXmlElement $slug
  79. */
  80. $slug = $mapping->slug;
  81. if (!$this->isValidField($meta, $field)) {
  82. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
  83. }
  84. $config['slugFields'][$field]['slug'] = $field;
  85. $config['slugFields'][$field]['style'] = $this->_isAttributeSet($slug, 'style') ?
  86. $this->_getAttribute($slug, 'style') : 'default';
  87. $config['slugFields'][$field]['updatable'] = $this->_isAttributeSet($slug, 'updatable') ?
  88. (bool)$this->_getAttribute($slug, 'updatable') : true;
  89. $config['slugFields'][$field]['unique'] = $this->_isAttributeSet($slug, 'unique') ?
  90. (bool)$this->_getAttribute($slug, 'unique') : true;
  91. $config['slugFields'][$field]['separator'] = $this->_isAttributeSet($slug, 'separator') ?
  92. $this->_getAttribute($slug, 'separator') : '-';
  93. }
  94. }
  95. }
  96. }
  97. /**
  98. * Checks if $field type is valid as Sluggable field
  99. *
  100. * @param ClassMetadata $meta
  101. * @param string $field
  102. * @return boolean
  103. */
  104. protected function isValidField(ClassMetadata $meta, $field)
  105. {
  106. $mapping = $meta->getFieldMapping($field);
  107. return $mapping && in_array($mapping['type'], $this->validTypes);
  108. }
  109. }