Collection.php 1.2 KB
<?php


namespace Lackoxygen\Customs\Utils;

use Illuminate\Support\Collection as BaseCollection;

class Collection extends BaseCollection
{
    /**
     * @param $method
     * @param $value
     */
    public function mSet($method, $value)
    {
        parent::offsetSet($this->cutKey($method, 3), $value);
    }

    /**
     * @param $method
     * @param $value
     */
    public function mPush($method, $value)
    {
        $key            = $this->cutKey($method, 4);
        $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->cutKey($method, 3), $default);
    }

    /**
     * @param string $key
     * @param int    $offset
     *
     * @return string
     */
    public function cutKey(string $key, int $offset = 0): string
    {
        return lcfirst(substr($key, $offset));
    }
}