protected function convert(array $attributes, $toLdap = true)
    {
        $direction = $toLdap ? 'toLdap' : 'fromLdap';

        foreach ($attributes as $attribute => $values) {
            // No converter, but the value should still be encoded.
            if (!$this->schema->hasConverter($attribute) && !isset($this->converted[$attribute])) {
                $attributes[$attribute] = $this->encodeValues($values);
            // Only continue if it has a converter and has not already been converted.
            } elseif ($this->schema->hasConverter($attribute) && !in_array($attribute, $this->converted)) {
                $values = $this->getConvertedValues($values, $attribute, $direction);
                if (in_array($attribute, $this->aggregated)) {
                    $attribute = $this->schema->getAttributeToLdap($attribute);
                }
                $attributes[$attribute] = (count($values) == 1) ? reset($values) : $values;
            }
        }

        return $this->removeValuesFromEntry($attributes, array_merge($this->aggregated, $this->remove));
    }