<commit_before><?php

namespace OpenOrchestra\Transformer;

use Symfony\Component\Form\DataTransformerInterface;

/**
 * Class SqlToBddTransformer
 */
class SqlToBddTransformer implements DataTransformerInterface
{
    protected $field = null;

    /**"
     * @param string $field
     */
    public function __construct($field)
    {
        $this->field = $field;
    }

    /**
     * @param array $value
     *
     * @return array
     */
    public function transform($value)
    {
        if (is_array($value) && array_key_exists($this->field, $value)) {
            $value[$this->field] = $this->transformField(json_decode($value[$this->field], true));
        }

        return $value;
    }

    /**
     * @param array $value
     *
     * @return array
     */
    public function reverseTransform($value)
    {
        if (is_array($value) && array_key_exists($this->field, $value)) {
            $value[$this->field] = $this->reverseTransformField($value[$this->field]);
        }

        return $value;
    }

    /**
     * @param string $conditions
     *
     * @return array|string
     */
    protected function transformField($conditions)
    {
        if (!is_null($conditions)) {
            foreach($conditions as $key => $condition) {
                if(is_array($condition)){
                    $conditions[$key] = $this->transformField($condition);
                }
                if ($key === '$eq' || $key === $this->field) {
                    $conditions = $conditions[$key];
                    break;
                } elseif ($key === '$ne') {
                    $conditions = 'NOT '.$conditions[$key];
                    break;
                } elseif ($key === '$and') {
                    $conditions = '('.implode(' AND ', $conditions[$key]).')';
                    break;
                } elseif ($key === '$or') {
                    $conditions = '('.implode(' OR ', $conditions[$key]).')';
                    break;
                }
            }
        }

        return $conditions;
    }

    /**
     * @param string $condition
     * @param int    $count
     * @param array  $aliases
     * @param string $delimiter
     *
     * @return string|null
     */
    protected function reverseTransformField($condition, $count = 0, $aliases = array(), $delimiter = '##')
    {
        if (!is_null($condition)) {
            $result = array();
            $reserved = preg_quote(')(');
            $findEncapsuledCondition = '/\(([^'.$reserved.']+)\)/';
            $encapsuledElements = array();
            preg_match_all($findEncapsuledCondition, $condition, $encapsuledElements);
            foreach ($encapsuledElements[0] as $key => $encapsuledElement) {
                $alias = $delimiter.$count.$delimiter;
                $condition = preg_replace('/'.preg_quote($encapsuledElement).'/', $alias, $condition, 1);
                $aliases[$alias] = $this->transformConditionArrayToMongoCondition($encapsuledElements[1][$key], $aliases);
                $count++;
            }

            if (count($encapsuledElements[0]) > 0) {
                $result = $this->reverseTransformField($condition, $count, $aliases, $delimiter);
            } else {
                $result = json_encode($this->transformConditionArrayToMongoCondition($condition, $aliases));
            }
        } else {
            $result = null;
        }

        return $result;
    }

    /**
     * @param string $condition
     * @param array  $aliases
     *
     * @return array
     */
    protected function transformConditionArrayToMongoCondition($condition, array &$aliases)
    {
        $operator = '$or';
        $conditionElements = explode(' OR ', $condition);

        if (count($conditionElements) == 1) {
            $operator = '$and';
            $conditionElements = explode(' AND ', $condition);
        }

        foreach ($conditionElements as $key => $element) {<commit_msg>renamme method<commit_after><?php

namespace OpenOrchestra\Transformer;

use Symfony\Component\Form\DataTransformerInterface;

/**
 * Class SqlToBddTransformer
 */
class SqlToBddTransformer implements DataTransformerInterface
{
    protected $field = null;

    /**"
     * @param string $field
     */
    public function __construct($field)
    {
        $this->field = $field;
    }

    /**
     * @param array $value
     *
     * @return array
     */
    public function transform($value)
    {
        if (is_array($value) && array_key_exists($this->field, $value)) {
            $value[$this->field] = $this->transformField(json_decode($value[$this->field], true));
        }

        return $value;
    }

    /**
     * @param array $value
     *
     * @return array
     */
    public function reverseTransform($value)
    {
        if (is_array($value) && array_key_exists($this->field, $value)) {
            $value[$this->field] = $this->reverseTransformField($value[$this->field]);
        }

        return $value;
    }

    /**
     * @param string $conditions
     *
     * @return array|string
     */
    protected function transformField($conditions)
    {
        if (!is_null($conditions)) {
            foreach($conditions as $key => $condition) {
                if(is_array($condition)){
                    $conditions[$key] = $this->transformField($condition);
                }
                if ($key === '$eq' || $key === $this->field) {
                    $conditions = $conditions[$key];
                    break;
                } elseif ($key === '$ne') {
                    $conditions = 'NOT '.$conditions[$key];
                    break;
                } elseif ($key === '$and') {
                    $conditions = '('.implode(' AND ', $conditions[$key]).')';
                    break;
                } elseif ($key === '$or') {
                    $conditions = '('.implode(' OR ', $conditions[$key]).')';
                    break;
                }
            }
        }

        return $conditions;
    }

    /**
     * @param string $condition
     * @param int    $count
     * @param array  $aliases
     * @param string $delimiter
     *
     * @return string|null
     */
    protected function reverseTransformField($condition, $count = 0, $aliases = array(), $delimiter = '##')
    {
        if (!is_null($condition)) {
            $result = array();
            $reserved = preg_quote(')(');
            $findEncapsuledCondition = '/\(([^'.$reserved.']+)\)/';
            $encapsuledElements = array();
            preg_match_all($findEncapsuledCondition, $condition, $encapsuledElements);
            foreach ($encapsuledElements[0] as $key => $encapsuledElement) {
                $alias = $delimiter.$count.$delimiter;
                $condition = preg_replace('/'.preg_quote($encapsuledElement).'/', $alias, $condition, 1);
                $aliases[$alias] = $this->transformConditionToMongoCondition($encapsuledElements[1][$key], $aliases);
                $count++;
            }

            if (count($encapsuledElements[0]) > 0) {
                $result = $this->reverseTransformField($condition, $count, $aliases, $delimiter);
            } else {
                $result = json_encode($this->transformConditionToMongoCondition($condition, $aliases));
            }
        } else {
            $result = null;
        }

        return $result;
    }

    /**
     * @param string $condition
     * @param array  $aliases
     *
     * @return array
     */
    protected function transformConditionToMongoCondition($condition, array &$aliases)
    {
        $operator = '$or';
        $conditionElements = explode(' OR ', $condition);

        if (count($conditionElements) == 1) {
            $operator = '$and';
            $conditionElements = explode(' AND ', $condition);
        }

        foreach ($conditionElements as $key => $element) {
{'fraction_non_alphanumeric': 0.15136097685067412, 'fraction_numerical': 0.0017807173747138134, 'mean_word_length': 1.8718042366691015, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, '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': '6182746', 'n_tokens_mistral': 2195, 'n_tokens_neox': 2100, 'n_words': 413}