<commit_before><?php

namespace Railken\Lem\Attributes;

use Illuminate\Support\Collection;
use Railken\Bag;
use Railken\Lem\Contracts\BelongsToAttributeContract;
use Railken\Lem\Contracts\EntityContract;
use Railken\Lem\Tokens;

class BelongsToAttribute extends BaseAttribute implements BelongsToAttributeContract
{
    /**
     * Is the attribute fillable.
     *
     * @var bool
     */
    protected $fillable = true;

    /**
     * @var string
     */
    protected $relationName;

    /**
     * @var string
     */
    protected $relationManager;

    /**
     * Schema of the attribute
     *
     * @var string
     */
    protected $schema = 'integer';

    /**
     * Set the name of the relation.
     *
     * @param string $relationName
     *
     * @return $this
     */
    public function setRelationName(string $relationName): self
    {
        $this->relationName = $relationName;

        return $this;
    }

    /**
     * Retrieve the name of the relation.
     *
     * @return string
     */
    public function getRelationName(): string
    {
        return $this->relationName;
    }

    /**
     * Retrieve eloquent relation.
     *
     * @param EntityContract $entity
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function getRelationBuilder(EntityContract $entity)
    {
        return $entity->{$this->relationName}();
    }

    /**
     * Set relation manager.
     *
     * @param string $relationManager
     */
    public function setRelationManager(string $relationManager): self
    {
        $this->relationManager = $relationManager;

        return $this;
    }

    /**
     * Retrieve relation manager.
     *
     * @param ?EntityContract $entity
     *
     * @return \Railken\Lem\Contracts\ManagerContract
     */
    public function getRelationManager(EntityContract $entity = null)
    {
        $class = $this->relationManager;

        return new $class($this->getManager()->getAgent());
    }

    /**
     * Validate.
     *
     * @param \Railken\Lem\Contracts\EntityContract $entity
     * @param \Railken\Bag                          $parameters
     *
     * @return Collection
     */
    public function validate(EntityContract $entity, Bag $parameters)
    {
        $errors = new Collection();

        $value = $parameters->get($this->getRelationName());

        if ($this->required && !$entity->exists && !$parameters->exists($this->getRelationName())) {
            $errors->push($this->newException(Tokens::NOT_DEFINED, $parameters->get($this->getName())));
        }

        if ($this->unique && $parameters->exists($this->getRelationName()) && $this->isUnique($entity, $value)) {
            $errors->push($this->newException(Tokens::NOT_UNIQUE, $parameters->get($this->getName())));
        }

        if ($parameters->exists($this->getRelationName()) && ($value !== null || $this->required) && !$this->valid($entity, $value)) {
            $errors->push($this->newException(Tokens::NOT_VALID, $parameters->get($this->getName())));
        }

        return $errors;
    }

    /**
     * Is a value valid ?
     *
     * @param \Railken\Lem\Contracts\EntityContract $entity
     * @param mixed                                 $value
     *
     * @return bool
     */
    public function valid(EntityContract $entity, $value)
    {
        $class = $this->getRelationManager($entity)->getRepository()->getEntity();

        return $value instanceof $class;
    }

    /**
     * Update entity value.
     *
     * @param \Railken\Lem\Contracts\EntityContract $entity
     * @param \Railken\Bag                          $parameters
     *
     * @return Collection
     */
    public function update(EntityContract $entity, Bag $parameters)
    {
        $errors = new Collection();

        if (!$parameters->has($this->getRelationName())) {
            if ($parameters->get($this->getName())) {
                $m = $this->getRelationManager($entity);

                if (!$m) {
                    $errors->push($this->newException(Tokens::NOT_VALID, $parameters->get($this->getName())));
                }

                if ($errors->count() === 0) {
                    $value = $m->getRepository()->findOneById($parameters->get($this->getName()));
                    $parameters->set($this->getRelationName(), $value);
                }
            } elseif ($parameters->exists($this->getName())) {
                $parameters->set($this->getRelationName(), $parameters->get($this->getName()));
            }
        }

        if ($parameters->has($this->getRelationName())) {
            $val = $parameters->get($this->getRelationName());

            if (is_array($val) || ($val instanceof \stdClass)) {
                $m = $this->getRelationManager($entity);<commit_msg>BelongsToAttribute add table name on findOneBy<commit_after><?php

namespace Railken\Lem\Attributes;

use Illuminate\Support\Collection;
use Railken\Bag;
use Railken\Lem\Contracts\BelongsToAttributeContract;
use Railken\Lem\Contracts\EntityContract;
use Railken\Lem\Tokens;

class BelongsToAttribute extends BaseAttribute implements BelongsToAttributeContract
{
    /**
     * Is the attribute fillable.
     *
     * @var bool
     */
    protected $fillable = true;

    /**
     * @var string
     */
    protected $relationName;

    /**
     * @var string
     */
    protected $relationManager;

    /**
     * Schema of the attribute
     *
     * @var string
     */
    protected $schema = 'integer';

    /**
     * Set the name of the relation.
     *
     * @param string $relationName
     *
     * @return $this
     */
    public function setRelationName(string $relationName): self
    {
        $this->relationName = $relationName;

        return $this;
    }

    /**
     * Retrieve the name of the relation.
     *
     * @return string
     */
    public function getRelationName(): string
    {
        return $this->relationName;
    }

    /**
     * Retrieve eloquent relation.
     *
     * @param EntityContract $entity
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function getRelationBuilder(EntityContract $entity)
    {
        return $entity->{$this->relationName}();
    }

    /**
     * Set relation manager.
     *
     * @param string $relationManager
     */
    public function setRelationManager(string $relationManager): self
    {
        $this->relationManager = $relationManager;

        return $this;
    }

    /**
     * Retrieve relation manager.
     *
     * @param ?EntityContract $entity
     *
     * @return \Railken\Lem\Contracts\ManagerContract
     */
    public function getRelationManager(EntityContract $entity = null)
    {
        $class = $this->relationManager;

        return new $class($this->getManager()->getAgent());
    }

    /**
     * Validate.
     *
     * @param \Railken\Lem\Contracts\EntityContract $entity
     * @param \Railken\Bag                          $parameters
     *
     * @return Collection
     */
    public function validate(EntityContract $entity, Bag $parameters)
    {
        $errors = new Collection();

        $value = $parameters->get($this->getRelationName());

        if ($this->required && !$entity->exists && !$parameters->exists($this->getRelationName())) {
            $errors->push($this->newException(Tokens::NOT_DEFINED, $parameters->get($this->getName())));
        }

        if ($this->unique && $parameters->exists($this->getRelationName()) && $this->isUnique($entity, $value)) {
            $errors->push($this->newException(Tokens::NOT_UNIQUE, $parameters->get($this->getName())));
        }

        if ($parameters->exists($this->getRelationName()) && ($value !== null || $this->required) && !$this->valid($entity, $value)) {
            $errors->push($this->newException(Tokens::NOT_VALID, $parameters->get($this->getName())));
        }

        return $errors;
    }

    /**
     * Is a value valid ?
     *
     * @param \Railken\Lem\Contracts\EntityContract $entity
     * @param mixed                                 $value
     *
     * @return bool
     */
    public function valid(EntityContract $entity, $value)
    {
        $class = $this->getRelationManager($entity)->getRepository()->getEntity();

        return $value instanceof $class;
    }

    /**
     * Update entity value.
     *
     * @param \Railken\Lem\Contracts\EntityContract $entity
     * @param \Railken\Bag                          $parameters
     *
     * @return Collection
     */
    public function update(EntityContract $entity, Bag $parameters)
    {
        $errors = new Collection();

        if (!$parameters->has($this->getRelationName())) {
            if ($parameters->get($this->getName())) {
                $m = $this->getRelationManager($entity);

                if (!$m) {
                    $errors->push($this->newException(Tokens::NOT_VALID, $parameters->get($this->getName())));
                }

                if ($errors->count() === 0) {
                    $value = $m->getRepository()->findOneBy([
                        $this->getRelationManager($entity)->newEntity()->getTable().".id" => $parameters->get($this->getName())
                    ]);
                    $parameters->set($this->getRelationName(), $value);
                }
            } elseif ($parameters->exists($this->getName())) {
                $parameters->set($this->getRelationName(), $parameters->get($this->getName()));
            }
        }

        if ($parameters->has($this->getRelationName())) {
            $val = $parameters->get($this->getRelationName());

            if (is_array($val) || ($val instanceof \stdClass)) {
                $m = $this->getRelationManager($entity);
{'fraction_non_alphanumeric': 0.14918032786885246, 'fraction_numerical': 0.00020491803278688525, 'mean_word_length': 2.145665485014502, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 135, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5309181', 'n_tokens_mistral': 2746, 'n_tokens_neox': 2663, 'n_words': 496}