Passed
Push — master ( 78a114...026f9b )
by Aleksandr
02:02
created

Breadcrumbs::actionToBreadcrumbMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
4
namespace carono\yii2crud;
5
6
7
use yii\helpers\Inflector;
8
9
class Breadcrumbs
10
{
11
    public static $crumbsNamespace = 'app\breadcrumbs';
12
13
    /**
14
     * @param $action
15
     * @return string
16
     */
17
    protected static function actionToBreadcrumbClass($action)
18
    {
19
        $arr = explode('/', Inflector::camel2id($action->controller->getUniqueId()));
20
        $arr[\count($arr) - 1] = Inflector::camelize(ucfirst($arr[\count($arr) - 1]));
21
        return static::$crumbsNamespace . '\\' . implode('\\', $arr) . 'Crumbs';
22
    }
23
24
    protected static function actionToBreadcrumbMethod($action)
25
    {
26
        return 'crumb' . Inflector::camelize($action->id);
27
    }
28
29
    /**
30
     * @param \yii\base\Action $action
31
     * @param array $params
32
     * @return array|mixed
33
     */
34
    public static function formCrumbs($action, $params)
35
    {
36
        $class = static::actionToBreadcrumbClass($action);
37
        $method = static::actionToBreadcrumbMethod($action);
38
39
        if (class_exists($class) && method_exists($class, $method)) {
40
            $reflectionMethod = new \ReflectionMethod($class, $method);
41
            $data = [];
42
            foreach ($reflectionMethod->getParameters() as $p) {
43
                $data[] = $params[$p->getName()] ?? null;
44
            }
45
            return array_filter(\call_user_func_array([$class, $method], $data));
46
        }
47
48
        return [];
49
    }
50
}