<commit_before><?php

namespace Oro\Bundle\SearchBundle\Query;

use Doctrine\Common\Persistence\ObjectManager;

use Oro\Bundle\SearchBundle\Query\Criteria\Criteria;
use Oro\Bundle\SearchBundle\Exception\ExpressionSyntaxError;

/**
 * @TODO: In platform 2.0 this class should be extended from the Doctrine\Common\Collections\Criteria.
 *        We should refactor this class only from platform v2.0 because it will break backward compatibility.
 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
 */
class Query
{
    const ORDER_ASC  = 'asc';
    const ORDER_DESC = 'desc';

    const KEYWORD_SELECT      = 'select';
    const KEYWORD_FROM        = 'from';
    const KEYWORD_WHERE       = 'where';
    const KEYWORD_AND         = 'and';
    const KEYWORD_OR          = 'or';
    const KEYWORD_OFFSET      = 'offset';
    const KEYWORD_MAX_RESULTS = 'max_results';
    const KEYWORD_ORDER_BY    = 'order_by';
    const KEYWORD_AS          = 'as';

    const OPERATOR_EQUALS              = '=';
    const OPERATOR_NOT_EQUALS          = '!=';
    const OPERATOR_GREATER_THAN        = '>';
    const OPERATOR_GREATER_THAN_EQUALS = '>=';
    const OPERATOR_LESS_THAN           = '<';
    const OPERATOR_LESS_THAN_EQUALS    = '<=';
    const OPERATOR_CONTAINS            = '~';
    const OPERATOR_NOT_CONTAINS        = '!~';
    const OPERATOR_IN                  = 'in';
    const OPERATOR_NOT_IN              = '!in';
    const OPERATOR_EXISTS              = 'exists';
    const OPERATOR_NOT_EXISTS          = 'notexists';

    const TYPE_TEXT     = 'text';
    const TYPE_INTEGER  = 'integer';
    const TYPE_DATETIME = 'datetime';
    const TYPE_DECIMAL  = 'decimal';

    const INFINITY = 10000000;
    const FINITY   = 0.000001;

    const DELIMITER = ' ';

    /** @var array */
    protected $select = [];

    /** @var array */
    protected $from;

    /** @var array */
    protected $mappingConfig;

    /** @var array */
    protected $fields;

    /** @var array */
    protected $selectAliases = [];

    /** @var Criteria */
    protected $criteria;

    /**
     */
    public function __construct()
    {
        $this->maxResults = 0;
        $this->from       = false;

        $this->criteria = Criteria::create();

        $this->criteria->setMaxResults(0);
    }

    /**
     * @return Criteria
     */
    public function getCriteria()
    {
        return $this->criteria;
    }

    /**
     * @param Criteria $criteria
     */
    public function setCriteria(Criteria $criteria)
    {
        $this->criteria = $criteria;
    }

    /**
     * Get entity class name from alias
     *
     * @param $aliasName
     *
     * @return bool|string
     */
    public function getEntityByAlias($aliasName)
    {
        foreach ($this->mappingConfig as $entity => $config) {
            if (isset($config['alias']) && $config['alias'] == $aliasName) {
                return $entity;
            }
        }

        return false;
    }

    /**
     * Set mapping config parameters
     *
     * @param array $mappingConfig
     */
    public function setMappingConfig($mappingConfig)
    {
        $fields = [];

        foreach ($mappingConfig as $entity => $config) {
            foreach ($config['fields'] as $field) {
                if (isset($field['relation_fields'])) {
                    $fields = $this->mapRelationFields($fields, $field, $entity);
                } elseif (isset($field['target_fields']) && count($field['target_fields']) > 0) {
                    $fields = $this->mapTargetFields($fields, $field, $entity);
                }
            }
        }

        $this->fields        = $fields;
        $this->mappingConfig = $mappingConfig;
    }

    /**
     * @param ObjectManager $em
     */
    public function setEntityManager(ObjectManager $em)
    {
        $this->em = $em;
    }

    /**
     * Init query
     *
     * @param string $query
     *
     * @return Query
     */
    public function createQuery($query)
    {
        $this->query = $query;

        return $this;
    }

    /**
     * Insert list of required fields to query select
     *
     * @param mixed  $field
     *
     * @param string $enforcedFieldType
     *
     * @return Query
     */
    public function select($field, $enforcedFieldType = null)
    {
        $this->select = $this->selectAliases = [];

        if (is_array($field)) {
            foreach ($field as $_field) {
                $this->addSelect($_field, $enforcedFieldType);
            }

            return $this;
        }

        $this->addSelect($field, $enforcedFieldType);

        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function addSelect($fieldNames, $enforcedFieldType = null)
    {
        foreach ((array)$fieldNames as $fieldName) {
            $fieldName = $this->parseFieldAliasing($fieldName);

            $this->addToSelect($fieldName, $enforcedFieldType);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function from($entities)
    {
        if (!is_array($entities)) {
            $entities = [$entities];
        }

        $this->from = $entities;

        return $this;
    }

    /**
     * Add "AND WHERE" parameter
     *
     * @deprecated Since 1.8 use criteria to add conditions
     *
     * @param string $fieldName<commit_msg>BB-4196: Refactor ProductBundle\ComponentProcessor\ComponentProcessorFilter - refactored current solutions in accordance with 4032 - joined ProductRepository solutions into one - fixed small bug in the platform's Query<commit_after><?php

namespace Oro\Bundle\SearchBundle\Query;

use Doctrine\Common\Persistence\ObjectManager;

use Oro\Bundle\SearchBundle\Query\Criteria\Criteria;
use Oro\Bundle\SearchBundle\Exception\ExpressionSyntaxError;

/**
 * @TODO: In platform 2.0 this class should be extended from the Doctrine\Common\Collections\Criteria.
 *        We should refactor this class only from platform v2.0 because it will break backward compatibility.
 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
 */
class Query
{
    const ORDER_ASC  = 'asc';
    const ORDER_DESC = 'desc';

    const KEYWORD_SELECT      = 'select';
    const KEYWORD_FROM        = 'from';
    const KEYWORD_WHERE       = 'where';
    const KEYWORD_AND         = 'and';
    const KEYWORD_OR          = 'or';
    const KEYWORD_OFFSET      = 'offset';
    const KEYWORD_MAX_RESULTS = 'max_results';
    const KEYWORD_ORDER_BY    = 'order_by';
    const KEYWORD_AS          = 'as';

    const OPERATOR_EQUALS              = '=';
    const OPERATOR_NOT_EQUALS          = '!=';
    const OPERATOR_GREATER_THAN        = '>';
    const OPERATOR_GREATER_THAN_EQUALS = '>=';
    const OPERATOR_LESS_THAN           = '<';
    const OPERATOR_LESS_THAN_EQUALS    = '<=';
    const OPERATOR_CONTAINS            = '~';
    const OPERATOR_NOT_CONTAINS        = '!~';
    const OPERATOR_IN                  = 'in';
    const OPERATOR_NOT_IN              = '!in';
    const OPERATOR_EXISTS              = 'exists';
    const OPERATOR_NOT_EXISTS          = 'notexists';

    const TYPE_TEXT     = 'text';
    const TYPE_INTEGER  = 'integer';
    const TYPE_DATETIME = 'datetime';
    const TYPE_DECIMAL  = 'decimal';

    const INFINITY = 10000000;
    const FINITY   = 0.000001;

    const DELIMITER = ' ';

    /** @var array */
    protected $select = [];

    /** @var array */
    protected $from;

    /** @var array */
    protected $mappingConfig;

    /** @var array */
    protected $fields;

    /** @var array */
    protected $selectAliases = [];

    /** @var Criteria */
    protected $criteria;

    /**
     */
    public function __construct()
    {
        $this->maxResults = 0;
        $this->from       = false;

        $this->criteria = Criteria::create();

        $this->criteria->setMaxResults(0);
    }

    /**
     * @return Criteria
     */
    public function getCriteria()
    {
        return $this->criteria;
    }

    /**
     * @param Criteria $criteria
     */
    public function setCriteria(Criteria $criteria)
    {
        $this->criteria = $criteria;
    }

    /**
     * Get entity class name from alias
     *
     * @param $aliasName
     *
     * @return bool|string
     */
    public function getEntityByAlias($aliasName)
    {
        foreach ($this->mappingConfig as $entity => $config) {
            if (isset($config['alias']) && $config['alias'] == $aliasName) {
                return $entity;
            }
        }

        return false;
    }

    /**
     * Set mapping config parameters
     *
     * @param array $mappingConfig
     */
    public function setMappingConfig($mappingConfig)
    {
        $fields = [];

        foreach ($mappingConfig as $entity => $config) {
            foreach ($config['fields'] as $field) {
                if (isset($field['relation_fields'])) {
                    $fields = $this->mapRelationFields($fields, $field, $entity);
                } elseif (isset($field['target_fields']) && count($field['target_fields']) > 0) {
                    $fields = $this->mapTargetFields($fields, $field, $entity);
                }
            }
        }

        $this->fields        = $fields;
        $this->mappingConfig = $mappingConfig;
    }

    /**
     * @param ObjectManager $em
     */
    public function setEntityManager(ObjectManager $em)
    {
        $this->em = $em;
    }

    /**
     * Init query
     *
     * @param string $query
     *
     * @return Query
     */
    public function createQuery($query)
    {
        $this->query = $query;

        return $this;
    }

    /**
     * Insert list of required fields to query select
     *
     * @param mixed  $field
     *
     * @param string $enforcedFieldType
     *
     * @return Query
     */
    public function select($field, $enforcedFieldType = null)
    {
        $this->select = $this->selectAliases = [];

        if (is_array($field)) {
            foreach ($field as $_field) {
                $this->addSelect($_field, $enforcedFieldType);
            }

            return $this;
        }

        $this->addSelect($field, $enforcedFieldType);

        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function addSelect($fieldNames, $enforcedFieldType = null)
    {
        foreach ((array)$fieldNames as $fieldName) {
            $fieldName = $this->parseFieldAliasing($fieldName);

            $this->addToSelect($fieldName, $enforcedFieldType);
        }

        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function from($entities)
    {
        if (!is_array($entities)) {
            $entities = [$entities];
        }

        $this->from = $entities;

        return $this;
    }

    /**
     * Add "AND WHERE" parameter
     *
     * @deprecated Since 1.8 use criteria to add conditions
     *
     * @param string $fieldName
{'fraction_non_alphanumeric': 0.12481563421828909, 'fraction_numerical': 0.005162241887905605, 'mean_word_length': 1.9353354978354977, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '4322232', 'n_tokens_mistral': 3284, 'n_tokens_neox': 3160, 'n_words': 692}