<?php

namespace Sargilla\Flatlab4\Console\Routes;

use Illuminate\Support\Str;

/**
 * Class Controller.
 *
 * @package Sargilla\Flatlab4\Console\Routes
 */
trait Controller
{
    /**
     * Obtain controller.
     *
     * @param $controllername
     * @return string
     */
    protected function controller($controllername)
    {
        if (Str::contains($controllername, '/')) {
            $controllername = str_replace('/', '\\', $controllername);
        }

        if (Str::contains($controllername, '@')) {
            return ucfirst($controllername);
        }
        return ucfirst($controllername . '@index');
    }

    /**
     * Obtain controller.
     *
     * @param $controllername
     * @return string
     */
    protected function controllerWithoutMethod($controllername)
    {
        if (Str::contains($controller = $controllername, '@')) {
            return ucfirst(substr($controllername, 0, strpos($controllername, '@')));
        }
        return ucfirst($controllername);
    }

    /**
     * Get method from controller name.
     *
     * @param $controllername
     * @return string
     */
    protected function controllerMethod($controllername)
    {
        if (Str::contains($controller = $controllername, '@')) {
            return substr($controllername, strpos($controllername, '@')+1, strlen($controllername));
        }
        return 'index';
    }
}
