Collection.php 1.4 KB
<?php


namespace Lackoxygen\GzCbec\Utils;

use Illuminate\Support\Collection as BaseCollection;
use Lackoxygen\GzCbec\Traits\XML;

class Collection extends BaseCollection
{
    use XML;

    /**
     * @param      $method
     * @param      $value
     * @param bool $lcFirst
     */
    public function mSet($method, $value, bool $lcFirst = true)
    {
        parent::offsetSet($this->subKey($method, 3, $lcFirst), $value);
    }

    /**
     * @param $method
     * @param $value
     */
    public function mPush($method, $value)
    {
        $key            = $this->subKey($method, 4, false);
        $listCollection = $this->get($key);
        if (!$listCollection instanceof BaseCollection) {
            $listCollection = BaseCollection::make();
        }
        $listCollection->push($value);
        $this->offsetSet($key, $listCollection);
    }

    /**
     * @param      $method
     * @param null $default
     *
     * @return mixed
     */
    public function mGet($method, $default = null)
    {
        return parent::get($this->subKey($method, 3), $default);
    }

    /**
     * @param string $key
     * @param int    $offset
     * @param bool   $lcFirst
     *
     * @return string
     */
    public function subKey(string $key, int $offset = 0, bool $lcFirst = true): string
    {
        $afterKey = substr($key, $offset);

        if ($lcFirst) {
            return lcfirst($afterKey);
        }

        return ucfirst($afterKey);
    }
}