ResourceCollectorTrait::getResourceName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 2
crap 2
1
<?php
2
3
/**
4
 * Codeburner Framework.
5
 *
6
 * @author Alex Rohleder <[email protected]>
7
 * @copyright 2016 Alex Rohleder
8
 * @license http://opensource.org/licenses/MIT
9
 */
10
11
namespace Codeburner\Router\Collectors;
12
13
/**
14
 * Just a fix to phpstorm parser, as it intends that Resource is
15
 * a datatype of php7 and not a class in router package.
16
 */
17
18
use Codeburner\Router\Resource as RouteResource;
19
20
/**
21
 * Methods for enable the collector to be resourceful and make
22
 * easily to build apis routes.
23
 *
24
 * @author Alex Rohleder <[email protected]>
25
 */
26
27
trait ResourceCollectorTrait
28
{
29
30
    abstract public function set($method, $pattern, $action);
31
32
    /**
33
     * A map of all routes of resources.
34
     *
35
     * @var array
36
     */
37
38
    protected $map = [
39
        "index"  => ["get",    "/{name}"],
40
        "make"   => ["get",    "/{name}/make"],
41
        "create" => ["post",   "/{name}"],
42
        "show"   => ["get",    "/{name}/{id:int+}"],
43
        "edit"   => ["get",    "/{name}/{id:int+}/edit"],
44
        "update" => ["put",    "/{name}/{id:int+}"],
45
        "delete" => ["delete", "/{name}/{id:int+}"],
46
    ];
47
48
    /**
49
     * Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. 
50
     * Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, 
51
     * a resourceful route declares them in a single line of code.
52
     *
53
     * @param string $controller The controller name.
54
     * @param array  $options    Some options like, "as" to name the route pattern, "only" to
55
     *                           explicit say that only this routes will be registered, and
56
     *                           "except" that register all the routes except the indicates.
57
     * @return RouteResource
58
     */
59
60 9
    public function resource($controller, array $options = array())
61
    {
62 9
        $name       = isset($options["prefix"]) ? $options["prefix"] : "";
63 9
        $name      .= $this->getResourceName($controller, $options);
64 9
        $actions    = $this->getResourceActions($options);
65 9
        $resource = new RouteResource;
66
67 9
        foreach ($actions as $action => $map) {
68 9
            $resource->set($this->set($map[0], $this->getResourcePath($action, $map[1], $name, $options), [$controller, $action]));
69 9
        }
70
71 9
        return $resource;
72
    }
73
74
    /**
75
     * Collect several resources at same time.
76
     *
77
     * @param array $controllers Several controller names as parameters or an array with all controller names.
78
     * @return RouteResource
79
     */
80
81 1
    public function resources(array $controllers)
82
    {
83 1
        $resource = new RouteResource;
84 1
        foreach ($controllers as $controller)
85 1
            $resource->set($this->resource($controller));
86 1
        return $resource;
87
    }
88
89
    /**
90
     * @param string $controller
91
     * @param array $options
92
     *
93
     * @return mixed
94
     */
95
96 9
    protected function getResourceName($controller, array $options)
97
    {
98 9
        return isset($options["as"]) ? $options["as"] : str_replace("controller", "", strtolower($controller));
99
    }
100
101
    /**
102
     * @param  array $options
103
     * @return array
104
     */
105
106 9
    protected function getResourceActions(array $options)
107
    {
108 9
        return isset($options["only"])   ? array_intersect_key($this->map, array_flip((array) $options["only"])) :
109 9
              (isset($options["except"]) ? array_diff_key($this->map, array_flip((array) $options["except"]))    : $this->map);
110
    }
111
112
    /**
113
     * @param string $action
114
     * @param string $path
115
     * @param string $name
116
     * @param string[] $options
117
     *
118
     * @return string
119
     */
120
121 9
    protected function getResourcePath($action, $path, $name, array $options)
122
    {
123 9
        return str_replace("{name}", $name,
124 9
            $action === "make" && isset($options["translate"]["make"]) ? str_replace("make", $options["translate"]["make"], $path) :
125 9
           ($action === "edit" && isset($options["translate"]["edit"]) ? str_replace("edit", $options["translate"]["edit"], $path) : $path));
126
    }
127
128
}
129