<commit_before><?php
namespace ManaPHP\Query;

use ManaPHP\Component;
use ManaPHP\Exception\InvalidValueException;
use ManaPHP\Exception\MisuseException;
use ManaPHP\Exception\NotSupportedException;
use ManaPHP\Model;
use ManaPHP\Query;

/**
 * Class Merger
 * @package ManaPHP\Query
 * @property-read \ManaPHP\Http\RequestInterface $request
 * @property-read \ManaPHP\Paginator             $paginator
 */
class Merger extends Component implements \ManaPHP\QueryInterface, \IteratorAggregate
{
    /**
     * @var \ManaPHP\QueryInterface[]
     */
    protected $queries;

    /**
     * @var int
     */
    protected $_limit;

    /**
     * @var int
     */
    protected $_offset;

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

    /**
     * Merger constructor.
     *
     * @param array        $queries
     * @param string|array $fields
     */
    public function __construct($queries, $fields = null)
    {
        $this->setQueries($queries);

        if ($fields) {
            $this->select($fields);
        }
    }

    public function __clone()
    {
        foreach ($this->queries as $k => $v) {
            $this->queries[$k] = clone $v;
        }
    }

    public function getIterator()
    {
        return new \ArrayIterator($this->fetch(true));
    }

    public function jsonSerialize()
    {
        return $this->fetch(true);
    }

    public function setDb($db)
    {
        throw new MisuseException(__METHOD__);
    }

    public function from($table, $alias = null)
    {
        throw new MisuseException(__METHOD__);
    }

    public function execute()
    {
        throw new MisuseException(__METHOD__);
    }

    /**
     * @param string[]|\ManaPHP\ModelInterface[]|\ManaPHP\QueryInterface[] $queries
     *
     * @return static
     */
    public function setQueries($queries)
    {
        if (is_string($queries[0])) {
            foreach ($queries as $k => $query) {
                $queries[$k] = new $query();
            }
        }

        foreach ($queries as $query) {
            if ($query instanceof Query) {
                $this->queries[] = $query;
            } elseif ($query instanceof Model) {
                $this->queries[] = $query::query(null, $query);
            }
        }

        return $this;
    }

    /**
     * @return \ManaPHP\QueryInterface[]
     */
    public function getQueries()
    {
        return $this->queries;
    }

    /**
     * @return \ManaPHP\Model
     */
    public function getModel()
    {
        return $this->queries[0]->getModel();
    }

    public function setModel($model)
    {
        return $this;
    }

    /**
     * @param string|array $fields
     *
     * @return static
     */
    public function select($fields)
    {
        foreach ($this->queries as $query) {
            $query->select($fields);
        }

        return $this;
    }

    /**
     * Sets SELECT DISTINCT / SELECT ALL flag
     *
     * @param bool $distinct
     *
     * @return static
     */
    public function distinct($distinct = true)
    {
        foreach ($this->queries as $query) {
            $query->distinct($distinct);
        }

        return $this;
    }

    /**
     * @param array $expr
     */
    public function aggregate($expr)
    {
        throw new NotSupportedException(__METHOD__);
    }

    /**
     * @param string|array           $filter
     * @param int|float|string|array $value
     *
     * @return static
     */
    public function where($filter, $value = null)
    {
        foreach ($this->queries as $query) {
            $query->where($filter, $value);
        }

        return $this;
    }

    /**
     * @param string $field
     * @param mixed  $value
     *
     * @return static
     */
    public function whereEq($field, $value)
    {
        foreach ($this->queries as $query) {
            $query->where($field, $value);
        }

        return $this;
    }

    /**
     * @param array $filters
     *
     * @return static
     */
    public function whereSearch($filters)
    {
        foreach ($this->queries as $query) {
            $query->whereSearch($filters);
        }

        return $this;
    }

    /**
     * @param string           $field
     * @param int|float|string $min
     * @param int|float|string $max
     *<commit_msg>Modified   ManaPHP/Query/Merger.php<commit_after><?php
namespace ManaPHP\Query;

use ManaPHP\Component;
use ManaPHP\Exception\InvalidValueException;
use ManaPHP\Exception\MisuseException;
use ManaPHP\Exception\NotSupportedException;
use ManaPHP\Model;
use ManaPHP\Query;

/**
 * Class Merger
 * @package ManaPHP\Query
 * @property-read \ManaPHP\Http\RequestInterface $request
 * @property-read \ManaPHP\Paginator             $paginator
 */
class Merger extends Component implements \ManaPHP\QueryInterface, \IteratorAggregate
{
    /**
     * @var \ManaPHP\QueryInterface[]
     */
    protected $queries;

    /**
     * @var int
     */
    protected $_limit;

    /**
     * @var int
     */
    protected $_offset;

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

    /**
     * Merger constructor.
     *
     * @param array        $queries
     * @param string|array $fields
     */
    public function __construct($queries, $fields = null)
    {
        $this->setQueries($queries);

        if ($fields) {
            $this->select($fields);
        }
    }

    public function __clone()
    {
        foreach ($this->queries as $k => $v) {
            $this->queries[$k] = clone $v;
        }
    }

    public function getIterator()
    {
        return new \ArrayIterator($this->fetch(true));
    }

    public function jsonSerialize()
    {
        return $this->fetch(true);
    }

    public function setDb($db)
    {
        throw new MisuseException(__METHOD__);
    }

    public function from($table, $alias = null)
    {
        throw new MisuseException(__METHOD__);
    }

    public function execute()
    {
        throw new MisuseException(__METHOD__);
    }

    /**
     * @param string[]|\ManaPHP\ModelInterface[]|\ManaPHP\QueryInterface[] $queries
     *
     * @return static
     */
    public function setQueries($queries)
    {
        if (is_string($queries[0])) {
            foreach ($queries as $k => $query) {
                $queries[$k] = new $query();
            }
        }

        foreach ($queries as $query) {
            if ($query instanceof Query) {
                $this->queries[] = $query;
            } elseif ($query instanceof Model) {
                $this->queries[] = $query::query(null, $query);
            }
        }

        return $this;
    }

    /**
     * @return \ManaPHP\QueryInterface[]
     */
    public function getQueries()
    {
        return $this->queries;
    }

    /**
     * @return \ManaPHP\Model
     */
    public function getModel()
    {
        return $this->queries[0]->getModel();
    }

    public function setModel($model)
    {
        return $this;
    }

    /**
     * @param string|array $fields
     *
     * @return static
     */
    public function select($fields)
    {
        foreach ($this->queries as $query) {
            $query->select($fields);
        }

        return $this;
    }

    /**
     * Sets SELECT DISTINCT / SELECT ALL flag
     *
     * @param bool $distinct
     *
     * @return static
     */
    public function distinct($distinct = true)
    {
        foreach ($this->queries as $query) {
            $query->distinct($distinct);
        }

        return $this;
    }

    /**
     * @param array $expr
     */
    public function aggregate($expr)
    {
        throw new NotSupportedException(__METHOD__);
    }

    /**
     * @param string|array           $filter
     * @param int|float|string|array $value
     *
     * @return static
     */
    public function where($filter, $value = null)
    {
        foreach ($this->queries as $query) {
            $query->where($filter, $value);
        }

        return $this;
    }

    /**
     * @param string $field
     * @param mixed  $value
     *
     * @return static
     */
    public function whereEq($field, $value)
    {
        foreach ($this->queries as $query) {
            $query->whereEq($field, $value);
        }

        return $this;
    }

    /**
     * @param array $filters
     *
     * @return static
     */
    public function whereSearch($filters)
    {
        foreach ($this->queries as $query) {
            $query->whereSearch($filters);
        }

        return $this;
    }

    /**
     * @param string           $field
     * @param int|float|string $min
     * @param int|float|string $max
     *
{'fraction_non_alphanumeric': 0.13232229682796945, 'fraction_numerical': 0.00046307015512850195, 'mean_word_length': 1.7347261791706237, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, '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': '4788885', 'n_tokens_mistral': 2607, 'n_tokens_neox': 2554, 'n_words': 549}