Skip to content

Commit 7966f89

Browse files
authored
Merge pull request #1770 from Pitoune/v2.4.x
Recognize "sluggable" in "attribute-overrides"
2 parents fe152e1 + 20af1b0 commit 7966f89

File tree

1 file changed

+78
-67
lines changed
  • lib/Gedmo/Sluggable/Mapping/Driver

1 file changed

+78
-67
lines changed

lib/Gedmo/Sluggable/Mapping/Driver/Xml.php

Lines changed: 78 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -43,79 +43,90 @@ public function readExtendedMetadata($meta, array &$config)
4343

4444
if (isset($xml->field)) {
4545
foreach ($xml->field as $mapping) {
46-
$mappingDoctrine = $mapping;
47-
/**
48-
* @var \SimpleXmlElement $mapping
49-
*/
50-
$mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
46+
$field = $this->_getAttribute($mapping, 'name');
47+
$this->buildFieldConfiguration($meta, $field, $mapping, $config);
48+
}
49+
}
5150

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-
}
51+
if (isset($xml->{'attribute-overrides'})) {
52+
foreach ($xml->{'attribute-overrides'}->{'attribute-override'} as $mapping) {
53+
$field = $this->_getAttribute($mapping, 'name');
54+
$this->buildFieldConfiguration($meta, $field, $mapping->field, $config);
55+
}
56+
}
57+
}
7058

71-
$handlers = array();
72-
if (isset($slug->handler)) {
73-
foreach ($slug->handler as $handler) {
74-
$class = (string) $this->_getAttribute($handler, 'class');
75-
$handlers[$class] = array();
76-
foreach ($handler->{'handler-option'} as $option) {
77-
$handlers[$class][(string) $this->_getAttribute($option, 'name')]
78-
= (string) $this->_getAttribute($option, 'value')
79-
;
80-
}
81-
$class::validate($handlers[$class], $meta);
82-
}
83-
}
59+
private function buildFieldConfiguration($meta, $field, \SimpleXMLElement $mapping, array &$config)
60+
{
61+
/**
62+
* @var \SimpleXmlElement $mapping
63+
*/
64+
$mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
8465

85-
// set all options
86-
$config['slugs'][$field] = array(
87-
'fields' => $fields,
88-
'slug' => $field,
89-
'style' => $this->_isAttributeSet($slug, 'style') ?
90-
$this->_getAttribute($slug, 'style') : 'default',
91-
'updatable' => $this->_isAttributeSet($slug, 'updatable') ?
92-
$this->_getBooleanAttribute($slug, 'updatable') : true,
93-
'dateFormat' => $this->_isAttributeSet($slug, 'dateFormat') ?
94-
$this->_getAttribute($slug, 'dateFormat') : 'Y-m-d-H:i',
95-
'unique' => $this->_isAttributeSet($slug, 'unique') ?
96-
$this->_getBooleanAttribute($slug, 'unique') : true,
97-
'unique_base' => $this->_isAttributeSet($slug, 'unique-base') ?
98-
$this->_getAttribute($slug, 'unique-base') : null,
99-
'separator' => $this->_isAttributeSet($slug, 'separator') ?
100-
$this->_getAttribute($slug, 'separator') : '-',
101-
'prefix' => $this->_isAttributeSet($slug, 'prefix') ?
102-
$this->_getAttribute($slug, 'prefix') : '',
103-
'suffix' => $this->_isAttributeSet($slug, 'suffix') ?
104-
$this->_getAttribute($slug, 'suffix') : '',
105-
'handlers' => $handlers,
106-
);
107-
if (!$meta->isMappedSuperclass && $meta->isIdentifier($field) && !$config['slugs'][$field]['unique']) {
108-
throw new InvalidMappingException("Identifier field - [{$field}] slug must be unique in order to maintain primary key in class - {$meta->name}");
109-
}
110-
$ubase = $config['slugs'][$field]['unique_base'];
111-
if ($config['slugs'][$field]['unique'] === false && $ubase) {
112-
throw new InvalidMappingException("Slug annotation [unique_base] can not be set if unique is unset or 'false'");
113-
}
114-
if ($ubase && !$meta->hasField($ubase) && !$meta->hasAssociation($ubase)) {
115-
throw new InvalidMappingException("Unable to find [{$ubase}] as mapped property in entity - {$meta->name}");
66+
if (isset($mapping->slug)) {
67+
/**
68+
* @var \SimpleXmlElement $slug
69+
*/
70+
$slug = $mapping->slug;
71+
if (!$this->isValidField($meta, $field)) {
72+
throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
73+
}
74+
$fields = array_map('trim', explode(',', (string) $this->_getAttribute($slug, 'fields')));
75+
foreach ($fields as $slugField) {
76+
if (!$meta->hasField($slugField)) {
77+
throw new InvalidMappingException("Unable to find slug [{$slugField}] as mapped property in entity - {$meta->name}");
78+
}
79+
if (!$this->isValidField($meta, $slugField)) {
80+
throw new InvalidMappingException("Cannot use field - [{$slugField}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
81+
}
82+
}
83+
84+
$handlers = array();
85+
if (isset($slug->handler)) {
86+
foreach ($slug->handler as $handler) {
87+
$class = (string) $this->_getAttribute($handler, 'class');
88+
$handlers[$class] = array();
89+
foreach ($handler->{'handler-option'} as $option) {
90+
$handlers[$class][(string) $this->_getAttribute($option, 'name')]
91+
= (string) $this->_getAttribute($option, 'value')
92+
;
11693
}
94+
$class::validate($handlers[$class], $meta);
11795
}
11896
}
97+
98+
// set all options
99+
$config['slugs'][$field] = array(
100+
'fields' => $fields,
101+
'slug' => $field,
102+
'style' => $this->_isAttributeSet($slug, 'style') ?
103+
$this->_getAttribute($slug, 'style') : 'default',
104+
'updatable' => $this->_isAttributeSet($slug, 'updatable') ?
105+
$this->_getBooleanAttribute($slug, 'updatable') : true,
106+
'dateFormat' => $this->_isAttributeSet($slug, 'dateFormat') ?
107+
$this->_getAttribute($slug, 'dateFormat') : 'Y-m-d-H:i',
108+
'unique' => $this->_isAttributeSet($slug, 'unique') ?
109+
$this->_getBooleanAttribute($slug, 'unique') : true,
110+
'unique_base' => $this->_isAttributeSet($slug, 'unique-base') ?
111+
$this->_getAttribute($slug, 'unique-base') : null,
112+
'separator' => $this->_isAttributeSet($slug, 'separator') ?
113+
$this->_getAttribute($slug, 'separator') : '-',
114+
'prefix' => $this->_isAttributeSet($slug, 'prefix') ?
115+
$this->_getAttribute($slug, 'prefix') : '',
116+
'suffix' => $this->_isAttributeSet($slug, 'suffix') ?
117+
$this->_getAttribute($slug, 'suffix') : '',
118+
'handlers' => $handlers,
119+
);
120+
if (!$meta->isMappedSuperclass && $meta->isIdentifier($field) && !$config['slugs'][$field]['unique']) {
121+
throw new InvalidMappingException("Identifier field - [{$field}] slug must be unique in order to maintain primary key in class - {$meta->name}");
122+
}
123+
$ubase = $config['slugs'][$field]['unique_base'];
124+
if ($config['slugs'][$field]['unique'] === false && $ubase) {
125+
throw new InvalidMappingException("Slug annotation [unique_base] can not be set if unique is unset or 'false'");
126+
}
127+
if ($ubase && !$meta->hasField($ubase) && !$meta->hasAssociation($ubase)) {
128+
throw new InvalidMappingException("Unable to find [{$ubase}] as mapped property in entity - {$meta->name}");
129+
}
119130
}
120131
}
121132

0 commit comments

Comments
 (0)