<commit_before><?php

namespace BeBat\PolyTree\Relations;

use BeBat\PolyTree\Contracts\Node;
use BeBat\PolyTree\Exceptions\LockedRelationship;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
 * Indirect Relations
 *
 * Shared functionality for ancestor and descendant relations.
 *
 * @package BeBat\PolyTree
 * @subpackage Relations
 * @author <NAME> <<EMAIL>>
 * @copyright 2016 <NAME>
 * @license https://github.com/bbatsche/PolyTree/blob/master/LICENSE.md MIT License
 */
abstract class Indirect extends BelongsToMany
{
    /** @var bool By default, do not allow this relationship to be modified in any way. */
    protected $locked = true;

    /**
     * Create a new indirect relationship instance.
     *
     * @param \BeBat\PolyTree\Contracts\Node $node
     * @param string $foreignKey Column name that points back to this node.
     * @param string $otherKey   Column name that points to the other nodes in this relationship.
     */
    public function __construct(Node $node, $foreignKey, $otherKey)
    {
        $table = $node->getAncestryTable();
        $query = $node->newQuery();

        parent::__construct($query, $node, $table, $foreignKey, $otherKey);
    }

    /**
     * Unlock this relationship and allow it to be modified.
     *
     * @return self
     */
    public function unlock()
    {
        $this->locked = false;

        return $this;
    }

    /**
     * Lock this relationship and prevent any modifications.
     *
     * @return self
     */
    public function lock()
    {
        $this->locked = true;

        return $this;
    }

    /**
     * Is the relationship currently locked?
     *
     * @return bool
     */
    public function isLocked()
    {
        return $this->locked;
    }

    /**
     * Add a node to the indirect ancestry
     *
     * @throws \BeBat\PolyTree\Exceptions\LockedRelationship if this relationship has not been unlocked first.
     *
     * @param \BeBat\PolyTree\Contracts\Node $node
     * @param array $attributes
     * @param bool  $touch
     *
     * @return void
     */
    public function attach($node, array $attributes = [], $touch = true)
    {
        if ($this->isLocked()) {
            throw new LockedRelationship();
        }

        return parent::attach($node);
    }

    /**
     * Remove a node from the indirect ancestry.
     *
     * @throws \BeBat\PolyTree\Exceptions\LockedRelationship if this relationship has not been unlocked first.
     *
     * @param int|array $ids
     * @param bool      $touch
     *
     * @return int Number of nodes detached
     */
    public function detach($ids = [], $touch = true)
    {
        if ($this->isLocked()) {
            throw new LockedRelationship();
        }

        return parent::detach($id, $touch);
    }

    /**
     * Generate a query builder object that joins the ancestor ids of $parent with the descendant ids of $child
     *
     * @param \BeBat\PolyTree\Contracts\Node $parent
     * @param \BeBat\PolyTree\Contracts\Node $child
     *
     * @return \Illuminate\Database\Query\Builder
     */
    public function getQueryForJoinedNodes(Node $parent, Node $child)
    {
        $query = $this->newPivotStatement()
            ->addSelect($this->getTable().'.'.$parent->getAncestorKeyName())
            ->addSelect('joined.'.$child->getDescendantKeyName());

        $query->join($this->getTable().' AS joined', function($join) use ($parent, $child)
        {<commit_msg>Typo in indirect detach<commit_after><?php

namespace BeBat\PolyTree\Relations;

use BeBat\PolyTree\Contracts\Node;
use BeBat\PolyTree\Exceptions\LockedRelationship;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
 * Indirect Relations
 *
 * Shared functionality for ancestor and descendant relations.
 *
 * @package BeBat\PolyTree
 * @subpackage Relations
 * @author <NAME> <<EMAIL>>
 * @copyright 2016 <NAME>
 * @license https://github.com/bbatsche/PolyTree/blob/master/LICENSE.md MIT License
 */
abstract class Indirect extends BelongsToMany
{
    /** @var bool By default, do not allow this relationship to be modified in any way. */
    protected $locked = true;

    /**
     * Create a new indirect relationship instance.
     *
     * @param \BeBat\PolyTree\Contracts\Node $node
     * @param string $foreignKey Column name that points back to this node.
     * @param string $otherKey   Column name that points to the other nodes in this relationship.
     */
    public function __construct(Node $node, $foreignKey, $otherKey)
    {
        $table = $node->getAncestryTable();
        $query = $node->newQuery();

        parent::__construct($query, $node, $table, $foreignKey, $otherKey);
    }

    /**
     * Unlock this relationship and allow it to be modified.
     *
     * @return self
     */
    public function unlock()
    {
        $this->locked = false;

        return $this;
    }

    /**
     * Lock this relationship and prevent any modifications.
     *
     * @return self
     */
    public function lock()
    {
        $this->locked = true;

        return $this;
    }

    /**
     * Is the relationship currently locked?
     *
     * @return bool
     */
    public function isLocked()
    {
        return $this->locked;
    }

    /**
     * Add a node to the indirect ancestry
     *
     * @throws \BeBat\PolyTree\Exceptions\LockedRelationship if this relationship has not been unlocked first.
     *
     * @param \BeBat\PolyTree\Contracts\Node $node
     * @param array $attributes
     * @param bool  $touch
     *
     * @return void
     */
    public function attach($node, array $attributes = [], $touch = true)
    {
        if ($this->isLocked()) {
            throw new LockedRelationship();
        }

        return parent::attach($node);
    }

    /**
     * Remove a node from the indirect ancestry.
     *
     * @throws \BeBat\PolyTree\Exceptions\LockedRelationship if this relationship has not been unlocked first.
     *
     * @param int|array $ids
     * @param bool      $touch
     *
     * @return int Number of nodes detached
     */
    public function detach($ids = [], $touch = true)
    {
        if ($this->isLocked()) {
            throw new LockedRelationship();
        }

        return parent::detach($ids, $touch);
    }

    /**
     * Generate a query builder object that joins the ancestor ids of $parent with the descendant ids of $child
     *
     * @param \BeBat\PolyTree\Contracts\Node $parent
     * @param \BeBat\PolyTree\Contracts\Node $child
     *
     * @return \Illuminate\Database\Query\Builder
     */
    public function getQueryForJoinedNodes(Node $parent, Node $child)
    {
        $query = $this->newPivotStatement()
            ->addSelect($this->getTable().'.'.$parent->getAncestorKeyName())
            ->addSelect('joined.'.$child->getDescendantKeyName());

        $query->join($this->getTable().' AS joined', function($join) use ($parent, $child)
        {
{'fraction_non_alphanumeric': 0.11842673966287279, 'fraction_numerical': 0.0011525716755510734, 'mean_word_length': 2.3830409356725144, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 41, 'https://': 2, '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': '6481389', 'n_tokens_mistral': 2046, 'n_tokens_neox': 1976, 'n_words': 557}