<commit_before><?php

namespace StartupPalace\Maki;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\View;
use Illuminate\Support\HtmlString;
use Kblais\Uuid\Uuid;

/**
 * Section model
 * Table : sections
 */
class Section extends Model
{
    use Uuid;

    protected $fillable = [
        'type', 'parent_id',
    ];

    /**
     * Describe the `sections` relation
     * @return HasMany
     */
    public function sections() : HasMany
    {
        return $this->hasMany(self::class, 'parent_id');
    }

    /**
     * Describe the `parentSection` relation
     * @return BelongsTo
     */
    public function parentSection() : BelongsTo
    {
        return $this->belongsTo(self::class, 'parent_id');
    }

    /**
     * Describe the `fieldValues` relation
     * @return HasMany
     */
    public function fieldValues() : HasMany
    {
        return $this->hasMany(config('maki.fieldValueClass'));
    }

    /**
     * Get the section config depending on its type
     * @return array
     */
    protected function getTypeConfig() : array
    {
        return array_get(config('maki.sectionTypes'), $this->type);
    }

    public function render()
    {
        $view = $this->getTemplateName();

        return new HtmlString(
            View::make($view, [
                'section' => $this,
            ])->render()
        );
    }

    /**
     * Lists the fields of the section
     * @return array
     */
    public function getFieldsAttribute() : Collection
    {
        $existingFields = $this->fieldValues->keyBy('field');

        return collect($this->getTypeConfig()['fields'])
            ->map(function ($field) use ($existingFields) {
                if ($existingFields->has($field)) {
                    return $existingFields->get($field);
                }

                $fieldValueClass = config('maki.fieldValueClass');

                return new $fieldValueClass(compact('field'));
            })
            ->keyBy('field');
    }

    /**
     * Get the template full name
     * @return string
     */
    public function getTemplateName() : string
    {
        return config('maki.templatePath') . '.' . $this->getTypeConfig()['template'];
    }
}<commit_msg>Use toString and toHtml methods for sections<commit_after><?php

namespace StartupPalace\Maki;

use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\View;
use Illuminate\Support\HtmlString;
use Kblais\Uuid\Uuid;

/**
 * Section model
 * Table : sections
 */
class Section extends Model implements Htmlable
{
    use Uuid;

    protected $fillable = [
        'type', 'parent_id',
    ];

    /**
     * Describe the `sections` relation
     * @return HasMany
     */
    public function sections() : HasMany
    {
        return $this->hasMany(self::class, 'parent_id');
    }

    /**
     * Describe the `parentSection` relation
     * @return BelongsTo
     */
    public function parentSection() : BelongsTo
    {
        return $this->belongsTo(self::class, 'parent_id');
    }

    /**
     * Describe the `fieldValues` relation
     * @return HasMany
     */
    public function fieldValues() : HasMany
    {
        return $this->hasMany(config('maki.fieldValueClass'));
    }

    /**
     * Get the section config depending on its type
     * @return array
     */
    protected function getTypeConfig() : array
    {
        return array_get(config('maki.sectionTypes'), $this->type);
    }

    public function render()
    {
        $view = $this->getTemplateName();

        return new HtmlString(
            View::make($view, [
                'section' => $this,
            ])->render()
        );
    }

    /**
     * Lists the fields of the section
     * @return array
     */
    public function getFieldsAttribute() : Collection
    {
        $existingFields = $this->fieldValues->keyBy('field');

        return collect($this->getTypeConfig()['fields'])
            ->map(function ($field) use ($existingFields) {
                if ($existingFields->has($field)) {
                    return $existingFields->get($field);
                }

                $fieldValueClass = config('maki.fieldValueClass');

                return new $fieldValueClass(compact('field'));
            })
            ->keyBy('field');
    }

    /**
     * Get the template full name
     * @return string
     */
    public function getTemplateName() : string
    {
        return config('maki.templatePath') . '.' . $this->getTypeConfig()['template'];
    }

    public function toHtml()
    {
        return (string) $this->render();
    }

    public function __toString()
    {
        return $this->toHtml();
    }
}
{'fraction_non_alphanumeric': 0.13016570173687364, 'fraction_numerical': 0.0, 'mean_word_length': 2.2260141661300707, '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': '5766589', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1419, 'n_words': 305}