Skip to content

Commit

Permalink
Merge pull request #1633 from krafas/sluggable-attribute-override
Browse files Browse the repository at this point in the history
Allow gedmo:slug to be overrided by attributeOverride
  • Loading branch information
l3pp4rd authored Jul 15, 2016
2 parents dc1c7a9 + 0102b1d commit 1a08240
Showing 1 changed file with 79 additions and 68 deletions.
147 changes: 79 additions & 68 deletions lib/Gedmo/Sluggable/Mapping/Driver/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,75 +46,13 @@ public function readExtendedMetadata($meta, array &$config)

if (isset($mapping['fields'])) {
foreach ($mapping['fields'] as $field => $fieldMapping) {
if (isset($fieldMapping['gedmo'])) {
if (isset($fieldMapping['gedmo']['slug'])) {
$slug = $fieldMapping['gedmo']['slug'];
if (!$this->isValidField($meta, $field)) {
throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
}
// process slug handlers
$handlers = array();
if (isset($slug['handlers'])) {
foreach ($slug['handlers'] as $handlerClass => $options) {
if (!strlen($handlerClass)) {
throw new InvalidMappingException("SlugHandler class: {$handlerClass} should be a valid class name in entity - {$meta->name}");
}
$handlers[$handlerClass] = $options;
$handlerClass::validate($handlers[$handlerClass], $meta);
}
}
// process slug fields
if (empty($slug['fields']) || !is_array($slug['fields'])) {
throw new InvalidMappingException("Slug must contain at least one field for slug generation in class - {$meta->name}");
}
foreach ($slug['fields'] as $slugField) {
if (!$meta->hasField($slugField)) {
throw new InvalidMappingException("Unable to find slug [{$slugField}] as mapped property in entity - {$meta->name}");
}
if (!$this->isValidField($meta, $slugField)) {
throw new InvalidMappingException("Cannot use field - [{$slugField}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
}
}

$config['slugs'][$field]['fields'] = $slug['fields'];
$config['slugs'][$field]['handlers'] = $handlers;
$config['slugs'][$field]['slug'] = $field;
$config['slugs'][$field]['style'] = isset($slug['style']) ?
(string) $slug['style'] : 'default';

$config['slugs'][$field]['dateFormat'] = isset($slug['dateFormat']) ?
(string) $slug['dateFormat'] : 'Y-m-d-H:i';

$config['slugs'][$field]['updatable'] = isset($slug['updatable']) ?
(bool) $slug['updatable'] : true;

$config['slugs'][$field]['unique'] = isset($slug['unique']) ?
(bool) $slug['unique'] : true;

$config['slugs'][$field]['unique_base'] = isset($slug['unique_base']) ?
$slug['unique_base'] : null;

$config['slugs'][$field]['separator'] = isset($slug['separator']) ?
(string) $slug['separator'] : '-';

$config['slugs'][$field]['prefix'] = isset($slug['prefix']) ?
(string) $slug['prefix'] : '';

$config['slugs'][$field]['suffix'] = isset($slug['suffix']) ?
(string) $slug['suffix'] : '';
$this->buildFieldConfiguration($field, $fieldMapping, $meta, $config);
}
}

if (!$meta->isMappedSuperclass && $meta->isIdentifier($field) && !$config['slugs'][$field]['unique']) {
throw new InvalidMappingException("Identifier field - [{$field}] slug must be unique in order to maintain primary key in class - {$meta->name}");
}
$ubase = $config['slugs'][$field]['unique_base'];
if ($config['slugs'][$field]['unique'] === false && $ubase) {
throw new InvalidMappingException("Slug annotation [unique_base] can not be set if unique is unset or 'false'");
}
if ($ubase && !$meta->hasField($ubase) && !$meta->hasAssociation($ubase)) {
throw new InvalidMappingException("Unable to find [{$ubase}] as mapped property in entity - {$meta->name}");
}
}
}
if (isset($mapping['attributeOverride'])) {
foreach ($mapping['attributeOverride'] as $field => $overrideMapping) {
$this->buildFieldConfiguration($field, $overrideMapping, $meta, $config);
}
}
}
Expand All @@ -141,4 +79,77 @@ protected function isValidField($meta, $field)

return $mapping && in_array($mapping['type'], $this->validTypes);
}

private function buildFieldConfiguration($field, array $fieldMapping, $meta, array &$config)
{
if (isset($fieldMapping['gedmo'])) {
if (isset($fieldMapping['gedmo']['slug'])) {
$slug = $fieldMapping['gedmo']['slug'];
if (!$this->isValidField($meta, $field)) {
throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
}
// process slug handlers
$handlers = array();
if (isset($slug['handlers'])) {
foreach ($slug['handlers'] as $handlerClass => $options) {
if (!strlen($handlerClass)) {
throw new InvalidMappingException("SlugHandler class: {$handlerClass} should be a valid class name in entity - {$meta->name}");
}
$handlers[$handlerClass] = $options;
$handlerClass::validate($handlers[$handlerClass], $meta);
}
}
// process slug fields
if (empty($slug['fields']) || !is_array($slug['fields'])) {
throw new InvalidMappingException("Slug must contain at least one field for slug generation in class - {$meta->name}");
}
foreach ($slug['fields'] as $slugField) {
if (!$meta->hasField($slugField)) {
throw new InvalidMappingException("Unable to find slug [{$slugField}] as mapped property in entity - {$meta->name}");
}
if (!$this->isValidField($meta, $slugField)) {
throw new InvalidMappingException("Cannot use field - [{$slugField}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
}
}

$config['slugs'][$field]['fields'] = $slug['fields'];
$config['slugs'][$field]['handlers'] = $handlers;
$config['slugs'][$field]['slug'] = $field;
$config['slugs'][$field]['style'] = isset($slug['style']) ?
(string) $slug['style'] : 'default';

$config['slugs'][$field]['dateFormat'] = isset($slug['dateFormat']) ?
(string) $slug['dateFormat'] : 'Y-m-d-H:i';

$config['slugs'][$field]['updatable'] = isset($slug['updatable']) ?
(bool) $slug['updatable'] : true;

$config['slugs'][$field]['unique'] = isset($slug['unique']) ?
(bool) $slug['unique'] : true;

$config['slugs'][$field]['unique_base'] = isset($slug['unique_base']) ?
$slug['unique_base'] : null;

$config['slugs'][$field]['separator'] = isset($slug['separator']) ?
(string) $slug['separator'] : '-';

$config['slugs'][$field]['prefix'] = isset($slug['prefix']) ?
(string) $slug['prefix'] : '';

$config['slugs'][$field]['suffix'] = isset($slug['suffix']) ?
(string) $slug['suffix'] : '';

if (!$meta->isMappedSuperclass && $meta->isIdentifier($field) && !$config['slugs'][$field]['unique']) {
throw new InvalidMappingException("Identifier field - [{$field}] slug must be unique in order to maintain primary key in class - {$meta->name}");
}
$ubase = $config['slugs'][$field]['unique_base'];
if ($config['slugs'][$field]['unique'] === false && $ubase) {
throw new InvalidMappingException("Slug annotation [unique_base] can not be set if unique is unset or 'false'");
}
if ($ubase && !$meta->hasField($ubase) && !$meta->hasAssociation($ubase)) {
throw new InvalidMappingException("Unable to find [{$ubase}] as mapped property in entity - {$meta->name}");
}
}
}
}
}

0 comments on commit 1a08240

Please sign in to comment.