Xml.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Gedmo\Sluggable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\Xml as BaseXml;
  4. use 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 specifically 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. 'datetime',
  28. 'citext',
  29. );
  30. /**
  31. * {@inheritDoc}
  32. */
  33. public function readExtendedMetadata($meta, array &$config)
  34. {
  35. /**
  36. * @var \SimpleXmlElement $xml
  37. */
  38. $xml = $this->_getMapping($meta->name);
  39. if (isset($xml->field)) {
  40. foreach ($xml->field as $mapping) {
  41. $field = $this->_getAttribute($mapping, 'name');
  42. $this->buildFieldConfiguration($meta, $field, $mapping, $config);
  43. }
  44. }
  45. if (isset($xml->{'attribute-overrides'})) {
  46. foreach ($xml->{'attribute-overrides'}->{'attribute-override'} as $mapping) {
  47. $field = $this->_getAttribute($mapping, 'name');
  48. $this->buildFieldConfiguration($meta, $field, $mapping->field, $config);
  49. }
  50. }
  51. }
  52. private function buildFieldConfiguration($meta, $field, \SimpleXMLElement $mapping, array &$config)
  53. {
  54. /**
  55. * @var \SimpleXmlElement $mapping
  56. */
  57. $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
  58. if (isset($mapping->slug)) {
  59. /**
  60. * @var \SimpleXmlElement $slug
  61. */
  62. $slug = $mapping->slug;
  63. if (!$this->isValidField($meta, $field)) {
  64. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
  65. }
  66. $fields = array_map('trim', explode(',', (string) $this->_getAttribute($slug, 'fields')));
  67. foreach ($fields as $slugField) {
  68. if (!$meta->hasField($slugField)) {
  69. throw new InvalidMappingException("Unable to find slug [{$slugField}] as mapped property in entity - {$meta->name}");
  70. }
  71. if (!$this->isValidField($meta, $slugField)) {
  72. throw new InvalidMappingException("Cannot use field - [{$slugField}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
  73. }
  74. }
  75. $handlers = array();
  76. if (isset($slug->handler)) {
  77. foreach ($slug->handler as $handler) {
  78. $class = (string) $this->_getAttribute($handler, 'class');
  79. $handlers[$class] = array();
  80. foreach ($handler->{'handler-option'} as $option) {
  81. $handlers[$class][(string) $this->_getAttribute($option, 'name')]
  82. = (string) $this->_getAttribute($option, 'value')
  83. ;
  84. }
  85. $class::validate($handlers[$class], $meta);
  86. }
  87. }
  88. // set all options
  89. $config['slugs'][$field] = array(
  90. 'fields' => $fields,
  91. 'slug' => $field,
  92. 'style' => $this->_isAttributeSet($slug, 'style') ?
  93. $this->_getAttribute($slug, 'style') : 'default',
  94. 'updatable' => $this->_isAttributeSet($slug, 'updatable') ?
  95. $this->_getBooleanAttribute($slug, 'updatable') : true,
  96. 'dateFormat' => $this->_isAttributeSet($slug, 'dateFormat') ?
  97. $this->_getAttribute($slug, 'dateFormat') : 'Y-m-d-H:i',
  98. 'unique' => $this->_isAttributeSet($slug, 'unique') ?
  99. $this->_getBooleanAttribute($slug, 'unique') : true,
  100. 'unique_base' => $this->_isAttributeSet($slug, 'unique-base') ?
  101. $this->_getAttribute($slug, 'unique-base') : null,
  102. 'separator' => $this->_isAttributeSet($slug, 'separator') ?
  103. $this->_getAttribute($slug, 'separator') : '-',
  104. 'prefix' => $this->_isAttributeSet($slug, 'prefix') ?
  105. $this->_getAttribute($slug, 'prefix') : '',
  106. 'suffix' => $this->_isAttributeSet($slug, 'suffix') ?
  107. $this->_getAttribute($slug, 'suffix') : '',
  108. 'handlers' => $handlers,
  109. );
  110. if (!$meta->isMappedSuperclass && $meta->isIdentifier($field) && !$config['slugs'][$field]['unique']) {
  111. throw new InvalidMappingException("Identifier field - [{$field}] slug must be unique in order to maintain primary key in class - {$meta->name}");
  112. }
  113. $ubase = $config['slugs'][$field]['unique_base'];
  114. if ($config['slugs'][$field]['unique'] === false && $ubase) {
  115. throw new InvalidMappingException("Slug annotation [unique_base] can not be set if unique is unset or 'false'");
  116. }
  117. if ($ubase && !$meta->hasField($ubase) && !$meta->hasAssociation($ubase)) {
  118. throw new InvalidMappingException("Unable to find [{$ubase}] as mapped property in entity - {$meta->name}");
  119. }
  120. }
  121. }
  122. /**
  123. * Checks if $field type is valid as Sluggable field
  124. *
  125. * @param object $meta
  126. * @param string $field
  127. *
  128. * @return boolean
  129. */
  130. protected function isValidField($meta, $field)
  131. {
  132. $mapping = $meta->getFieldMapping($field);
  133. return $mapping && in_array($mapping['type'], $this->validTypes);
  134. }
  135. }