<commit_before><?php

namespace Oxygen\Data\Behaviour;

use Doctrine\Common\Collections\Collection;

trait Versions {

    /**
     * Returns the versions of the entity.
     *
     * @return Collection
     */

    public function getVersions() {
        if($this->isHead()) {
            return $this->versions;
        } else {
            return $this->headVersion->getVersions();
        }
    }

    /**
     * Returns true if the entity has any versions.
     *
     * @return boolean
     */

    public function hasVersions() {
        return !$this->getVersions()->isEmpty();
    }

    /**
     * Whether the current version is the head version.
     *
     * @return boolean
     */

    public function isHead() {
        return $this->headVersion === null;
    }

    /**
     * Returns the primary key of the head.
     *
     * @return integer
     */

    public function getHeadId() {
        return $this->isHead() ? $this->getId() : $this->headVersion->getId();
    }

    /**
     * Returns the head version.
     *
     * @return object
     */

    public function getHead() {
        return ($this->isHead()) ? $this : $this->headVersion;
    }

    /**
     * Sets the head version.
     *
     * @param object $head
     * @return $this
     */

    public function setHead($head) {
        $this->headVersion = $head;
        return $this;
    }

}
<commit_msg>Added `getUniqueValidationRule` helper<commit_after><?php

namespace Oxygen\Data\Behaviour;

use Doctrine\Common\Collections\Collection;

trait Versions {

    /**
     * Returns the versions of the entity.
     *
     * @return Collection
     */

    public function getVersions() {
        if($this->isHead()) {
            return $this->versions;
        } else {
            return $this->headVersion->getVersions();
        }
    }

    /**
     * Returns true if the entity has any versions.
     *
     * @return boolean
     */

    public function hasVersions() {
        return !$this->getVersions()->isEmpty();
    }

    /**
     * Whether the current version is the head version.
     *
     * @return boolean
     */

    public function isHead() {
        return $this->headVersion === null;
    }

    /**
     * Returns the primary key of the head.
     *
     * @return integer
     */

    public function getHeadId() {
        return $this->isHead() ? $this->getId() : $this->headVersion->getId();
    }

    /**
     * Returns the head version.
     *
     * @return object
     */

    public function getHead() {
        return ($this->isHead()) ? $this : $this->headVersion;
    }

    /**
     * Sets the head version.
     *
     * @param object $head
     * @return $this
     */

    public function setHead($head) {
        $this->headVersion = $head;
        return $this;
    }

    /**
     * Returns a validation rule that validates this entity for uniqueness, ignoring other versions.
     *
     * @param $field
     * @return string
     */
    protected function getUniqueValidationRule($field) {
        $rule = 'unique:' . get_class($this) . ',' . $field . ',' . $this->getHeadId() . ',id';

        // ignore other versions of this entity
        if($this->getHeadId()) {
            $rule .= ',headVersion,!=,' . $this->getHeadId();
        }

        return $rule;
    }

}

{'fraction_non_alphanumeric': 0.13706505295007565, 'fraction_numerical': 0.0, 'mean_word_length': 1.8184143222506395, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 34, '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': '487343', 'n_tokens_mistral': 970, 'n_tokens_neox': 947, 'n_words': 240}