<commit_before><?php

namespace Gedmo\Sluggable\Mapping\Driver;

use Gedmo\Mapping\Driver\File,
    Gedmo\Mapping\Driver,
    Gedmo\Exception\InvalidMappingException;

/**
 * This is a yaml mapping driver for Sluggable
 * behavioral extension. Used for extraction of extended
 * metadata from yaml specificaly for Sluggable
 * extension.
 *
 * @author <NAME> <<EMAIL>>
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
class Yaml extends File implements Driver
{
    /**
     * File extension
     * @var string
     */
    protected $_extension = '.dcm.yml';

    /**
     * List of types which are valid for slug and sluggable fields
     *
     * @var array
     */
    private $validTypes = array(
        'string',
        'text',
        'integer',
        'int',
    );

    /**
     * {@inheritDoc}
     */
    public function readExtendedMetadata($meta, array &$config)
    {
        $mapping = $this->_getMapping($meta->name);

        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) || $meta->isInheritedField($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]['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'] : '-';

                        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 && !$this->isValidField($meta, $ubase) && !$meta->hasAssociation($ubase)) {
                            throw new InvalidMappingException("Unable to find [{$ubase}] as mapped property in entity - {$meta->name}");
                        }
                    }
                }
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    protected function _loadMappingFile($file)
    {
        return \Symfony\Component\Yaml\Yaml::parse($file);
    }

    /**
     * Checks if $field type is valid as Sluggable field
     *
     * @param object $meta
     * @param string $field
     * @return boolean
     */
    protected function isValidField($meta, $field)
    {
        $mapping = $meta->getFieldMapping($field);
        return $mapping && in_array($mapping['type'], $this->validTypes);
    }
}
<commit_msg>[sluggable] yaml mapping should support inheritance, closes #794<commit_after><?php

namespace Gedmo\Sluggable\Mapping\Driver;

use Gedmo\Mapping\Driver\File,
    Gedmo\Mapping\Driver,
    Gedmo\Exception\InvalidMappingException;

/**
 * This is a yaml mapping driver for Sluggable
 * behavioral extension. Used for extraction of extended
 * metadata from yaml specificaly for Sluggable
 * extension.
 *
 * @author <NAME> <<EMAIL>>
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
class Yaml extends File implements Driver
{
    /**
     * File extension
     * @var string
     */
    protected $_extension = '.dcm.yml';

    /**
     * List of types which are valid for slug and sluggable fields
     *
     * @var array
     */
    private $validTypes = array(
        'string',
        'text',
        'integer',
        'int',
    );

    /**
     * {@inheritDoc}
     */
    public function readExtendedMetadata($meta, array &$config)
    {
        $mapping = $this->_getMapping($meta->name);

        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]['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'] : '-';

                        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 && !$this->isValidField($meta, $ubase) && !$meta->hasAssociation($ubase)) {
                            throw new InvalidMappingException("Unable to find [{$ubase}] as mapped property in entity - {$meta->name}");
                        }
                    }
                }
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    protected function _loadMappingFile($file)
    {
        return \Symfony\Component\Yaml\Yaml::parse($file);
    }

    /**
     * Checks if $field type is valid as Sluggable field
     *
     * @param object $meta
     * @param string $field
     * @return boolean
     */
    protected function isValidField($meta, $field)
    {
        $mapping = $meta->getFieldMapping($field);
        return $mapping && in_array($mapping['type'], $this->validTypes);
    }
}

{'fraction_non_alphanumeric': 0.14176111595466434, 'fraction_numerical': 0.00026155187445510024, 'mean_word_length': 1.5970115462983925, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2001110', 'n_tokens_mistral': 2880, 'n_tokens_neox': 2816, 'n_words': 668}