<commit_before><?php

namespace Code16\Sharp\Form\Eloquent\Transformers;

use Code16\Sharp\Utils\Transformers\SharpAttributeTransformer;
use Illuminate\Database\Eloquent\Relations\MorphMany;

class FormUploadModelTransformer implements SharpAttributeTransformer
{

    /**
     * Transform a model attribute to array (json-able).
     *
     * @param $instance
     * @param string $attribute
     * @return mixed
     */
    function apply($instance, string $attribute)
    {
        if(!$instance->$attribute) {
            return null;
        }

        if($instance->$attribute() instanceof MorphMany) {
            // We are handling a list of uploads
            return $instance->$attribute->map(function($upload) {
                $array = $this->transformUpload($upload);

                return [
                    "file" => array_only($array, ["name", "thumbnail", "size"]),
                ] + array_except($array, ["name", "thumbnail", "size"]);
            })->all();
        };

        return $this->transformUpload($instance->$attribute);
    }

    protected function transformUpload($upload)
    {
        return ($upload->file_name ? [
                "name" => $upload->file_name,
                "thumbnail" => $upload->thumbnail(null, 150),<commit_msg>fix file transformer to allow null value<commit_after><?php

namespace Code16\Sharp\Form\Eloquent\Transformers;

use Code16\Sharp\Utils\Transformers\SharpAttributeTransformer;
use Illuminate\Database\Eloquent\Relations\MorphMany;

class FormUploadModelTransformer implements SharpAttributeTransformer
{

    /**
     * Transform a model attribute to array (json-able).
     *
     * @param $instance
     * @param string $attribute
     * @return mixed
     */
    function apply($instance, string $attribute)
    {
        if(!$instance->$attribute) {
            return null;
        }

        if($instance->$attribute() instanceof MorphMany) {
            // We are handling a list of uploads
            return $instance->$attribute->map(function($upload) {
                $array = $this->transformUpload($upload);

                $file = array_only($array, ["name", "thumbnail", "size"]);

                return [
                    "file" => sizeof($file) ? $file : null,
                ] + array_except($array, ["name", "thumbnail", "size"]);
            })->all();
        };

        return $this->transformUpload($instance->$attribute);
    }

    protected function transformUpload($upload)
    {
        return ($upload->file_name ? [
                "name" => $upload->file_name,
                "thumbnail" => $upload->thumbnail(null, 150),
{'fraction_non_alphanumeric': 0.13457164518574677, 'fraction_numerical': 0.00530705079605762, 'mean_word_length': 2.3278688524590163, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '530767', 'n_tokens_mistral': 741, 'n_tokens_neox': 697, 'n_words': 147}