Xml.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class Xml extends BaseXml
  16. {
  17. /**
  18. * List of types which are valid for slug and sluggable fields
  19. *
  20. * @var array
  21. */
  22. private $validTypes = array(
  23. 'string',
  24. 'text',
  25. 'integer',
  26. 'int',
  27. );
  28. /**
  29. * {@inheritDoc}
  30. */
  31. public function readExtendedMetadata($meta, array &$config)
  32. {
  33. /**
  34. * @var \SimpleXmlElement $xml
  35. */
  36. $xml = $this->_getMapping($meta->name);
  37. if (isset($xml->field)) {
  38. foreach ($xml->field as $mapping) {
  39. $mappingDoctrine = $mapping;
  40. /**
  41. * @var \SimpleXmlElement $mapping
  42. */
  43. $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
  44. $field = $this->_getAttribute($mappingDoctrine, 'name');
  45. if (isset($mapping->slug)) {
  46. /**
  47. * @var \SimpleXmlElement $slug
  48. */
  49. $slug = $mapping->slug;
  50. if (!$this->isValidField($meta, $field)) {
  51. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
  52. }
  53. $fields = array_map('trim', explode(',', (string)$this->_getAttribute($slug, 'fields')));
  54. foreach ($fields as $slugField) {
  55. if (!$meta->hasField($slugField)) {
  56. throw new InvalidMappingException("Unable to find slug [{$slugField}] as mapped property in entity - {$meta->name}");
  57. }
  58. if (!$this->isValidField($meta, $slugField)) {
  59. throw new InvalidMappingException("Cannot use field - [{$slugField}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
  60. }
  61. }
  62. $handlers = array();
  63. if (isset($slug->handler)) {
  64. foreach ($slug->handler as $handler) {
  65. $class = (string)$this->_getAttribute($handler, 'class');
  66. $handlers[$class] = array();
  67. foreach ($handler->{'handler-option'} as $option) {
  68. $handlers[$class][(string)$this->_getAttribute($option, 'name')]
  69. = (string)$this->_getAttribute($option, 'value')
  70. ;
  71. }
  72. $class::validate($handlers[$class], $meta);
  73. }
  74. }
  75. // set all options
  76. $config['slugs'][$field] = array(
  77. 'fields' => $fields,
  78. 'slug' => $field,
  79. 'style' => $this->_isAttributeSet($slug, 'style') ?
  80. $this->_getAttribute($slug, 'style') : 'default',
  81. 'updatable' => $this->_isAttributeSet($slug, 'updatable') ?
  82. $this->_getBooleanAttribute($slug, 'updatable') : true,
  83. 'unique' => $this->_isAttributeSet($slug, 'unique') ?
  84. $this->_getBooleanAttribute($slug, 'unique') : true,
  85. 'unique_base' => $this->_isAttributeSet($slug, 'unique_base') ?
  86. $this->_getAttribute($slug, 'unique_base') : null,
  87. 'separator' => $this->_isAttributeSet($slug, 'separator') ?
  88. $this->_getAttribute($slug, 'separator') : '-',
  89. 'prefix' => $this->_isAttributeSet($slug, 'prefix') ?
  90. $this->_getAttribute($slug, 'prefix') : '',
  91. 'suffix' => $this->_isAttributeSet($slug, 'suffix') ?
  92. $this->_getAttribute($slug, 'suffix') : '',
  93. 'handlers' => $handlers,
  94. );
  95. if (!$meta->isMappedSuperclass && $meta->isIdentifier($field) && !$config['slugs'][$field]['unique']) {
  96. throw new InvalidMappingException("Identifier field - [{$field}] slug must be unique in order to maintain primary key in class - {$meta->name}");
  97. }
  98. $ubase = $config['slugs'][$field]['unique_base'];
  99. if ($config['slugs'][$field]['unique'] === false && $ubase) {
  100. throw new InvalidMappingException("Slug annotation [unique_base] can not be set if unique is unset or 'false'");
  101. }
  102. if ($ubase && !$this->isValidField($meta, $ubase) && !$meta->hasAssociation($ubase)) {
  103. throw new InvalidMappingException("Unable to find [{$ubase}] as mapped property in entity - {$meta->name}");
  104. }
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * Checks if $field type is valid as Sluggable field
  111. *
  112. * @param object $meta
  113. * @param string $field
  114. * @return boolean
  115. */
  116. protected function isValidField($meta, $field)
  117. {
  118. $mapping = $meta->getFieldMapping($field);
  119. return $mapping && in_array($mapping['type'], $this->validTypes);
  120. }
  121. }