Xml.php 4.7 KB

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