imanghafoori1/laravel-widgetize

View on GitHub
src/Utils/SlotRenderer.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace Imanghafoori\Widgets\Utils;

trait SlotRenderer
{
    /**
     * @var string
     */
    protected $slotName;

    /**
     * @var array<string, string>
     */
    protected $slots = [];

    /**
     * Start output buffer to get content of slot and set slot name.
     *
     * @param  string  $name
     */
    public function startSlot($name)
    {
        if (ob_start()) {
            $this->slotName = $name;
        }
    }

    /**
     * get slot content from widget block.
     *
     * @param  string  $data
     */
    public function renderSlot($data = '')
    {
        $this->slots[$this->slotName] = $data;
    }

    /**
     * check if widget has any slots.
     *
     * @return bool
     */
    public function hasSlots()
    {
        return ! empty($this->slots);
    }

    /**
     * get and clean current slots.
     *
     * @return array<string, string>
     */
    public function getSlots()
    {
        $slots = $this->slots;

        $this->slots = [];

        return $slots;
    }
}