Completed
Push — master ( 2f5afb...f40faa )
by Filipe
02:44
created

CrudCommonMethods::getEntityNameSingular()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Controller;
11
12
use Psr\Http\Message\ServerRequestInterface;
13
use Slick\Common\Utils\Text;
14
use Slick\Http\PhpEnvironment\Request;
15
use Slick\Mvc\ControllerInterface;
16
17
/**
18
 * CRUD Common Methods
19
 * 
20
 * @package Slick\Mvc\Controller
21
 * @author  Filipe Silva <[email protected]>
22
 */
23
trait CrudCommonMethods
24
{
25
    
26
    /**
27
     * Get the plural name of the entity
28
     *
29
     * @return string
30
     */
31 4
    protected function getEntityNamePlural()
32
    {
33 4
        $name = $this->getEntityNameSingular();
34 4
        $nameParts = Text::camelCaseToSeparator($name, '#');
35 4
        $nameParts = explode('#', $nameParts);
36 4
        $lastPart = array_pop($nameParts);
37 4
        $lastPart = ucfirst(Text::plural(strtolower($lastPart)));
38 4
        array_push($nameParts, $lastPart);
39 4
        return lcfirst(implode('', $nameParts));
40
    }
41
42
    /**
43
     * Get entity singular name used on controller actions
44
     * 
45
     * @return string
46
     */
47 4
    protected function getEntityNameSingular()
48
    {
49 4
        $names = explode('\\', $this->getEntityClassName());
50 4
        $name = end($names);
51 4
        return lcfirst($name);
52
    }
53
54
    /**
55
     * Gets updated HTTP request
56
     *
57
     * @return ServerRequestInterface|Request
58
     */
59
    abstract public function getRequest();
60
61
    /**
62
     * Sets a value to be used by render
63
     *
64
     * The key argument can be an associative array with values to be set
65
     * or a string naming the passed value. If an array is given then the
66
     * value will be ignored.
67
     *
68
     * Those values must be set in the request attributes so they can be used
69
     * latter by any other middle ware in the stack.
70
     *
71
     * @param string|array $key
72
     * @param mixed        $value
73
     *
74
     * @return ControllerInterface
75
     */
76
    abstract public function set($key, $value = null);
77
78
    /**
79
     * Gets the entity FQ class name
80
     *
81
     * @return string
82
     */
83
    abstract public function getEntityClassName();
84
85
    /**
86
     * Redirects the flow to another route/path
87
     *
88
     * @param string $path the route or path to redirect to
89
     *
90
     * @return ControllerInterface|self|$this
91
     */
92
    abstract public function redirect($path);
93
}