<commit_before><?php

namespace Plugins\NestedSet\Database\ORM;

use Nova\Database\ORM\ModelNotFoundException;
use Nova\Database\ORM\Builder as BaseBuilder;
use Nova\Database\Query\Grammars\Grammar;
use Nova\Database\Query\Processors\Processor;
use Nova\Database\Query\Expression;
use Nova\Database\Query\Builder as QueryBuilder;
use Nova\Database\ConnectionInterface;

use LogicException;


class Builder extends BaseBuilder
{

    /**
     * @var Node
     */
    protected $model;


    /**
     * Get node's `lft` and `rgt` values.
     *
     * @since 2.0
     *
     * @param mixed $id
     * @param bool $required
     *
     * @return array
     */
    public function getNodeData($id, $required = false)
    {
        $this->query->where($this->model->getKeyName(), '=', $id);

        $data = $this->query->first([ $this->model->getLeftName(), $this->model->getRightName() ]);

        if ( ! $data and $required) throw new ModelNotFoundException;

        return (array)$data;
    }

    /**
     * Get plain node data.
     *
     * @since 2.0
     *
     * @param mixed $id
     * @param bool $required
     *
     * @return array
     */
    public function getPlainNodeData($id, $required = false)
    {
        return array_values($this->getNodeData($id, $required));
    }

    /**
     * Scope limits query to select just root node.
     *
     * @return $this
     */
    public function whereIsRoot()
    {
        $this->query->whereNull($this->model->getParentIdName());

        return $this;
    }

    /**
     * Limit results to ancestors of specified node.
     *
     * @since 2.0
     *
     * @param mixed $id
     *
     * @return $this
     */
    public function whereAncestorOf($id)
    {
        $table = $this->wrappedTable();
        $keyName = $this->model->getKeyName();

        list($lft, $rgt) = $this->wrappedColumns();

        $key = $this->query->getGrammar()->wrap($keyName);

        if ($id instanceof Node) {
            $dataSource = '?';

            $this->query->addBinding($id->getLeft());

            $id = $id->getKey();
        } else {
            $dataSource = "(select _.{$lft} from {$table} _ where _.{$key} = ? limit 1)";

            $this->query->addBinding($id);
        }

        $this->query->whereRaw($dataSource." between {$lft} and {$rgt}");

        // Exclude the node
        $this->where($keyName, '<>', $id);

        return $this;
    }

    /**
     * Get ancestors of specified node.
     *
     * @since 2.0
     *
     * @param mixed $id
     * @param array $columns
     *
     * @return \Kalnoy\Nestedset\Collection
     */
    public function ancestorsOf($id, array $columns = array('*'))
    {
        return $this->whereAncestorOf($id)->get($columns);
    }

    /**
     * Add node selection statement between specified range.
     *
     * @since 2.0
     *
     * @param array $values
     * @param string $boolean
     * @param bool $not
     *
     * @return $this
     */
    public function whereNodeBetween($values, $boolean = 'and', $not = false)
    {
        $this->query->whereBetween($this->model->getLeftName(), $values, $boolean, $not);

        return $this;
    }

    /**
     * Add node selection statement between specified range joined with `or` operator.
     *
     * @since 2.0
     *
     * @param array $values
     *
     * @return $this
     */
    public function orWhereNodeBetween($values)
    {
        return $this->whereNodeBetween($values, 'or');
    }

    /**
     * Add constraint statement to descendants of specified node.
     *
     * @since 2.0
     *
     * @param mixed $id
     * @param string $boolean
     * @param bool $not
     *
     * @return $this
     */
    public function whereDescendantOf($id, $boolean = 'and', $not = false)
    {
        if ($id instanceof Node) {
            $data = $id->getBounds();
        } else {
            $data = $this->model->newServiceQuery()->getPlainNodeData($id, true);
        }

        // Don't include the node
        ++$data[0];

        return $this->whereNodeBetween($data, $boolean, $not);
    }

    /**
     * @param mixed $id
     *
     * @return \Plugins\NestedSet\Database\ORM\Builder
     */
    public function whereNotDescendantOf($id)
    {
        return $this->whereDescendantOf($id, 'and', true);
    }

    /**
     * @param mixed $id
     *
     * @return \Plugins\NestedSet\Database\ORM\Builder
     */
    public function orWhereDescendantOf($id)
    {
        return $this->whereDescendantOf($id, 'or');
    }

    /**
     * @param mixed $id
     *
     * @return \Plugins\NestedSet\Database\ORM\Builder
     */
    public function orWhereNotDescendantOf($id)
    {
        return $this->whereDescendantOf($id, 'or', true);
    }

    /**
     * Get descendants of specified node.
     *
     * @since 2.0
     *
     * @param mixed $id
     * @param array $columns
     *
     * @return \Kalnoy\Nestedset\Collection
     */
    public function descendantsOf($id, array $columns = array('*'))
    {
        try {
            return $this->whereDescendantOf($id)->get($columns);
        }
        catch (ModelNotFoundException $e) {
            return $this->model->newCollection();
        }
    }

    /**
     * Constraint nodes to those that are after specified node.
     *<commit_msg>Correction of typos<commit_after><?php

namespace Plugins\NestedSet\Database\ORM;

use Nova\Database\ORM\ModelNotFoundException;
use Nova\Database\ORM\Builder as BaseBuilder;
use Nova\Database\Query\Grammars\Grammar;
use Nova\Database\Query\Processors\Processor;
use Nova\Database\Query\Expression;
use Nova\Database\Query\Builder as QueryBuilder;
use Nova\Database\ConnectionInterface;

use LogicException;


class Builder extends BaseBuilder
{

    /**
     * @var Node
     */
    protected $model;


    /**
     * Get node's `lft` and `rgt` values.
     *
     * @since 2.0
     *
     * @param mixed $id
     * @param bool $required
     *
     * @return array
     */
    public function getNodeData($id, $required = false)
    {
        $this->query->where($this->model->getKeyName(), '=', $id);

        $data = $this->query->first([ $this->model->getLeftName(), $this->model->getRightName() ]);

        if ( ! $data and $required) throw new ModelNotFoundException;

        return (array)$data;
    }

    /**
     * Get plain node data.
     *
     * @since 2.0
     *
     * @param mixed $id
     * @param bool $required
     *
     * @return array
     */
    public function getPlainNodeData($id, $required = false)
    {
        return array_values($this->getNodeData($id, $required));
    }

    /**
     * Scope limits query to select just root node.
     *
     * @return $this
     */
    public function whereIsRoot()
    {
        $this->query->whereNull($this->model->getParentIdName());

        return $this;
    }

    /**
     * Limit results to ancestors of specified node.
     *
     * @since 2.0
     *
     * @param mixed $id
     *
     * @return $this
     */
    public function whereAncestorOf($id)
    {
        $table = $this->wrappedTable();
        $keyName = $this->model->getKeyName();

        list($lft, $rgt) = $this->wrappedColumns();

        $key = $this->query->getGrammar()->wrap($keyName);

        if ($id instanceof Node) {
            $dataSource = '?';

            $this->query->addBinding($id->getLeft());

            $id = $id->getKey();
        } else {
            $dataSource = "(select _.{$lft} from {$table} _ where _.{$key} = ? limit 1)";

            $this->query->addBinding($id);
        }

        $this->query->whereRaw($dataSource." between {$lft} and {$rgt}");

        // Exclude the node
        $this->where($keyName, '<>', $id);

        return $this;
    }

    /**
     * Get ancestors of specified node.
     *
     * @param $id
     * @param array $columns
     * @return \Nova\Database\ORM\Collection|static[]
     */
    public function ancestorsOf($id, array $columns = array('*'))
    {
        return $this->whereAncestorOf($id)->get($columns);
    }

    /**
     * Add node selection statement between specified range.
     *
     * @since 2.0
     *
     * @param array $values
     * @param string $boolean
     * @param bool $not
     *
     * @return $this
     */
    public function whereNodeBetween($values, $boolean = 'and', $not = false)
    {
        $this->query->whereBetween($this->model->getLeftName(), $values, $boolean, $not);

        return $this;
    }

    /**
     * Add node selection statement between specified range joined with `or` operator.
     *
     * @since 2.0
     *
     * @param array $values
     *
     * @return $this
     */
    public function orWhereNodeBetween($values)
    {
        return $this->whereNodeBetween($values, 'or');
    }

    /**
     * Add constraint statement to descendants of specified node.
     *
     * @since 2.0
     *
     * @param mixed $id
     * @param string $boolean
     * @param bool $not
     *
     * @return $this
     */
    public function whereDescendantOf($id, $boolean = 'and', $not = false)
    {
        if ($id instanceof Node) {
            $data = $id->getBounds();
        } else {
            $data = $this->model->newServiceQuery()->getPlainNodeData($id, true);
        }

        // Don't include the node
        ++$data[0];

        return $this->whereNodeBetween($data, $boolean, $not);
    }

    /**
     * @param mixed $id
     *
     * @return \Plugins\NestedSet\Database\ORM\Builder
     */
    public function whereNotDescendantOf($id)
    {
        return $this->whereDescendantOf($id, 'and', true);
    }

    /**
     * @param mixed $id
     *
     * @return \Plugins\NestedSet\Database\ORM\Builder
     */
    public function orWhereDescendantOf($id)
    {
        return $this->whereDescendantOf($id, 'or');
    }

    /**
     * @param mixed $id
     *
     * @return \Plugins\NestedSet\Database\ORM\Builder
     */
    public function orWhereNotDescendantOf($id)
    {
        return $this->whereDescendantOf($id, 'or', true);
    }

    /**
     * Get descendants of specified node.
     *
     * @param $id
     * @param array $columns
     * @return \Nova\Database\ORM\Collection|Collection|static[]
     */
    public function descendantsOf($id, array $columns = array('*'))
    {
        try {
            return $this->whereDescendantOf($id)->get($columns);
        }
        catch (ModelNotFoundException $e) {
            return $this->model->newCollection();
        }
    }

    /**
     * Constraint nodes to those that are after specified node.
     *
{'fraction_non_alphanumeric': 0.1478919943154903, 'fraction_numerical': 0.0030317385125532923, 'mean_word_length': 2.01686196056016, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 105, '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': '2128463', 'n_tokens_mistral': 3303, 'n_tokens_neox': 3208, 'n_words': 720}