<commit_before><?php

namespace Code16\Sharp\Form\Eloquent;

use Closure;
use Code16\Sharp\Form\Eloquent\Exceptions\SharpFormEloquentUpdateException;
use Code16\Sharp\Form\Transformers\SharpAttributeValuator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;

trait WithSharpFormEloquentUpdater
{
    /**
     * @var array
     */
    protected $valuators = [];

    /**
     * @param string $attribute
     * @param string|Closure $valuator
     * @return $this
     */
    function setCustomValuator(string $attribute, $valuator)
    {
        $valuator = $valuator instanceof Closure
            ? $this->normalizeToSharpAttributeValuator($valuator)
            : app($valuator);

        $this->valuators[$attribute] = $valuator;

        return $this;
    }

    /**
     * Update an Eloquent Model with $data.
     *
     * @param Model $instance
     * @param array $data
     * @return bool
     */
    function save(Model $instance, array $data): bool
    {
        // TODO extract to EloquentModelUpdater + extends to hasMany, belongsToMany, ...
        $delayedAttributes = [];
        $relationships = [];

        foreach($data as $attribute => $value) {

            if($this->isRelationship($instance, $attribute)) {
                $relationships[$attribute] = [
                    "relation_type" => get_class($instance->$attribute()),
                    "value" => $this->formatValueAccordingToFieldType($value, $attribute, $instance)
                ];

                continue;
            }

            try {
                $value = $this->formatValueAccordingToFieldType($value, $attribute, $instance);

            } catch(ModelNotFoundException $ex) {
                 // We try to format a field which needs the instance to be persisted.
                // For example: the UploadFormatter needs a persisted instance if
                // its storagePath contains a parameter, like {id}. We delay the valuate.
                $delayedAttributes[$attribute] = $value;

                continue;
            }

            $this->valuateAttribute($instance, $attribute, $value);
        }

        // End of "normal" attributes.
        $instance->save();

        // Next, handle delayed attributes
        foreach($delayedAttributes as $attribute => $value) {
            $this->valuateAttribute(
                $instance, $attribute, $this->formatValueAccordingToFieldType($value, $attribute, $instance)
            );
        }
        $instance->save();

        // Finally, handle relationships.
        $this->saveRelationships($instance, $relationships);

        return true;
    }

    /**
     * Valuates the $attribute with $value<commit_msg>Tweaks<commit_after><?php

namespace Code16\Sharp\Form\Eloquent;

use Closure;
use Code16\Sharp\Form\Eloquent\Exceptions\SharpFormEloquentUpdateException;
use Code16\Sharp\Form\Transformers\SharpAttributeValuator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;

trait WithSharpFormEloquentUpdater
{
    /**
     * @var array
     */
    protected $valuators = [];

    /**
     * @param string $attribute
     * @param string|Closure $valuator
     * @return $this
     */
    function setCustomValuator(string $attribute, $valuator)
    {
        $valuator = $valuator instanceof Closure
            ? $this->normalizeToSharpAttributeValuator($valuator)
            : app($valuator);

        $this->valuators[$attribute] = $valuator;

        return $this;
    }

    /**
     * Update an Eloquent Model with $data.
     *
     * @param Model $instance
     * @param array $data
     * @return bool
     */
    function save(Model $instance, array $data): bool
    {
        $delayedAttributes = [];
        $relationships = [];

        foreach($data as $attribute => $value) {

            if($this->isRelationship($instance, $attribute)) {
                $relationships[$attribute] = [
                    "relation_type" => get_class($instance->$attribute()),
                    "value" => $this->formatValueAccordingToFieldType($value, $attribute, $instance)
                ];

                continue;
            }

            try {
                $value = $this->formatValueAccordingToFieldType($value, $attribute, $instance);

            } catch(ModelNotFoundException $ex) {
                // We try to format a field which needs the instance to be persisted.
                // For example: the UploadFormatter needs a persisted instance if
                // its storagePath contains a parameter, like {id}. We delay the valuate.
                $delayedAttributes[$attribute] = $value;

                continue;
            }

            $this->valuateAttribute($instance, $attribute, $value);
        }

        // End of "normal" attributes.
        $instance->save();

        // Next, handle delayed attributes
        foreach($delayedAttributes as $attribute => $value) {
            $this->valuateAttribute(
                $instance, $attribute, $this->formatValueAccordingToFieldType($value, $attribute, $instance)
            );
        }
        $instance->save();

        // Finally, handle relationships.
        $this->saveRelationships($instance, $relationships);

        return true;
    }

    /**
     * Valuates the $attribute with $value
{'fraction_non_alphanumeric': 0.11448815961215737, 'fraction_numerical': 0.002237553608055193, 'mean_word_length': 2.133177570093458, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '5930386', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1420, 'n_words': 317}