<commit_before><?php
/**
 * This file is part of the LdapTools package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace LdapTools\Resolver;

use LdapTools\AttributeConverter\AttributeConverterInterface;
use LdapTools\Connection\LdapConnectionInterface;
use LdapTools\Factory\AttributeConverterFactory;
use LdapTools\Schema\LdapObjectSchema;

/**
 * The base value resolver for sending data back to LDAP.
 *
 * @author <NAME> <<EMAIL>>
 */
abstract class BaseValueResolver
{
    /**
     * Any converter options defined by the schema object.
     *
     * @var array
     */
    protected $options = [];

    /**
     * @var LdapConnectionInterface
     */
    protected $connection;

    /**
     * @var LdapObjectSchema
     */
    protected $schema;

    /**
     * @var string|null The full DN of the LDAP entry for this context.
     */
    protected $dn;

    /**
     * @var int The LDAP operation type that the converter is working against.
     */
    protected $type;

    /**
     * @var array Attribute names that were merged into a single attribute.
     */
    protected $aggregated = [];

    /**
     * Iterate through the aggregates for a specific LDAP value to build up the value it should actually be.
     *
     * @param array $toAggregate
     * @param mixed $values
     * @param AttributeConverterInterface $converter
     * @return array|string The final value after all possible values have been iterated through.
     */
    abstract protected function iterateAggregates(array $toAggregate, $values, AttributeConverterInterface $converter);

    /**
     * @param LdapObjectSchema $schema
     * @param int $type
     */
    public function __construct(LdapObjectSchema $schema, $type)
    {
        $this->type = $type;
        $this->schema = $schema;
        $this->options = $schema->getConverterOptions();
    }

    /**
     * Set the LDAP connection for the current context.
     *
     * @param LdapConnectionInterface $connection
     */
    public function setLdapConnection(LdapConnectionInterface $connection)
    {
        $this->connection = $connection;
    }

    /**
     * Set the DN for the entry whose values are being converted.
     *
     * @param string $dn
     */
    public function setDn($dn)
    {
        $this->dn = $dn;
    }

    /**
     * Get the values for an attribute after applying any converters.
     *
     * @param mixed $values
     * @param string $attribute
     * @param string $direction
     * @param AttributeConverterInterface|null $aggregate
     * @return array
     */
    protected function getConvertedValues($values, $attribute, $direction, $aggregate = null)
    {
        $values = is_array($values) ? $values : [$values];
        $converter = is_null($aggregate) ? $this->getConverterWithOptions($this->schema->getConverter($attribute)) : $aggregate;

        if (is_null($aggregate) && $converter->getShouldAggregateValues() && $direction == 'toLdap') {
            return $this->convertAggregateValues($attribute, $values, $converter);
        }

        foreach ($values as $index => $value) {
            $converter = is_null($aggregate) ? $this->getConverterWithOptions($this->schema->getConverter($attribute)) : $aggregate;
            $converter->setAttribute($attribute);
            $values[$index] = $converter->$direction($value);
        }

        return $values;
    }

    /**
     * Loops through all the attributes that are to be aggregated into a single attribute for a specific converter.
     *
     * @param string $attribute
     * @param array $values
     * @param AttributeConverterInterface $converter
     * @return array
     */
    protected function convertAggregateValues($attribute, array $values, AttributeConverterInterface $converter)
    {
        $this->aggregated[] = $attribute;
        $converterName = $this->schema->getConverter($attribute);
        $toAggregate = array_keys($this->schema->getConverterMap(), $converterName);
        $values = $this->iterateAggregates($toAggregate, $values, $converter);

        return is_array($values) ? $values : [$values];
    }

    /**
     * Get an instance of a converter with its options set.
     *
     * @param string $converterName The name of the converter from the schema.
     * @return AttributeConverterInterface
     */
    protected function getConverterWithOptions($converterName)
    {
        $converter = AttributeConverterFactory::get($converterName);

        if (isset($this->options[$converterName])) {
            $converter->setOptions($this->options[$converterName]);
        }
        if ($this->connection) {
            $converter->setLdapConnection($this->connection);
        }
        if ($this->dn !== null) {
            $converter->setDn($this->dn);
        }
        $converter->setOperationType($this->type);

        return $converter;
    }
}<commit_msg>Refactor getConvertedValues.<commit_after><?php
/**
 * This file is part of the LdapTools package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace LdapTools\Resolver;

use LdapTools\AttributeConverter\AttributeConverterInterface;
use LdapTools\Connection\LdapConnectionInterface;
use LdapTools\Factory\AttributeConverterFactory;
use LdapTools\Schema\LdapObjectSchema;

/**
 * The base value resolver for sending data back to LDAP.
 *
 * @author <NAME> <<EMAIL>>
 */
abstract class BaseValueResolver
{
    /**
     * Any converter options defined by the schema object.
     *
     * @var array
     */
    protected $options = [];

    /**
     * @var LdapConnectionInterface
     */
    protected $connection;

    /**
     * @var LdapObjectSchema
     */
    protected $schema;

    /**
     * @var string|null The full DN of the LDAP entry for this context.
     */
    protected $dn;

    /**
     * @var int The LDAP operation type that the converter is working against.
     */
    protected $type;

    /**
     * @var array Attribute names that were merged into a single attribute.
     */
    protected $aggregated = [];

    /**
     * Iterate through the aggregates for a specific LDAP value to build up the value it should actually be.
     *
     * @param array $toAggregate
     * @param mixed $values
     * @param AttributeConverterInterface $converter
     * @return array|string The final value after all possible values have been iterated through.
     */
    abstract protected function iterateAggregates(array $toAggregate, $values, AttributeConverterInterface $converter);

    /**
     * @param LdapObjectSchema $schema
     * @param int $type
     */
    public function __construct(LdapObjectSchema $schema, $type)
    {
        $this->type = $type;
        $this->schema = $schema;
        $this->options = $schema->getConverterOptions();
    }

    /**
     * Set the LDAP connection for the current context.
     *
     * @param LdapConnectionInterface $connection
     */
    public function setLdapConnection(LdapConnectionInterface $connection)
    {
        $this->connection = $connection;
    }

    /**
     * Set the DN for the entry whose values are being converted.
     *
     * @param string $dn
     */
    public function setDn($dn)
    {
        $this->dn = $dn;
    }

    /**
     * Get the values for an attribute after applying any converters.
     *
     * @param mixed $values
     * @param string $attribute
     * @param string $direction
     * @param AttributeConverterInterface|null $aggregate
     * @return array
     */
    protected function getConvertedValues($values, $attribute, $direction, $aggregate = null)
    {
        $values = is_array($values) ? $values : [$values];
        $converter = is_null($aggregate) ? $this->getConverterWithOptions($this->schema->getConverter($attribute)) : $aggregate;

        if (is_null($aggregate) && $converter->getShouldAggregateValues() && $direction == 'toLdap') {
            $values = $this->convertAggregateValues($attribute, $values, $converter);
        } else {
            $values = $this->doConvertValues($attribute, $values, $direction, $aggregate);
        }

        return $values;
    }

    /**
     * Loops through all the attributes that are to be aggregated into a single attribute for a specific converter.
     *
     * @param string $attribute
     * @param array $values
     * @param AttributeConverterInterface $converter
     * @return array
     */
    protected function convertAggregateValues($attribute, array $values, AttributeConverterInterface $converter)
    {
        $this->aggregated[] = $attribute;
        $converterName = $this->schema->getConverter($attribute);
        $toAggregate = array_keys($this->schema->getConverterMap(), $converterName);
        $values = $this->iterateAggregates($toAggregate, $values, $converter);

        return is_array($values) ? $values : [$values];
    }

    /**
     * Get an instance of a converter with its options set.
     *
     * @param string $converterName The name of the converter from the schema.
     * @return AttributeConverterInterface
     */
    protected function getConverterWithOptions($converterName)
    {
        $converter = AttributeConverterFactory::get($converterName);

        if (isset($this->options[$converterName])) {
            $converter->setOptions($this->options[$converterName]);
        }
        if ($this->connection) {
            $converter->setLdapConnection($this->connection);
        }
        if ($this->dn !== null) {
            $converter->setDn($this->dn);
        }
        $converter->setOperationType($this->type);

        return $converter;
    }

    /**
     * Convert a set of values for an attribute.
     *
     * @param string $attribute
     * @param array $values
     * @param string $direction
     * @param AttributeConverterInterface|null $converter
     * @return mixed
     */
    protected function doConvertValues($attribute, array $values, $direction, AttributeConverterInterface $converter = null)
    {
        $converter = is_null($converter) ? $this->getConverterWithOptions($this->schema->getConverter($attribute)) : $converter;

        foreach ($values as $index => $value) {
            $converter->setAttribute($attribute);
            $values[$index] = $converter->$direction($value);
        }

        return $values;
    }
}
{'fraction_non_alphanumeric': 0.11476190476190476, 'fraction_numerical': 0.0, 'mean_word_length': 2.7476802284082797, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 84, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7239700', 'n_tokens_mistral': 2866, 'n_tokens_neox': 2839, 'n_words': 786}