Passed
Push — master ( 341749...d97e6d )
by Stephen
13:50
created

ResolveModelName::getClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
4
namespace Sfneal\Models\Actions;
5
6
7
use Sfneal\Actions\AbstractAction;
8
use Sfneal\Helpers\Laravel\LaravelHelpers;
9
use Sfneal\Helpers\Strings\StringHelpers;
10
use Sfneal\Models\AbstractModel;
11
12
class ResolveModelName extends AbstractAction
13
{
14
    /**
15
     * @var AbstractModel
16
     */
17
    private $model;
18
19
    /**
20
     * @var bool
21
     */
22
    private $short;
23
24
    /**
25
     * ResolveModelName constructor.
26
     *
27
     * @param AbstractModel $model
28
     * @param bool $short
29
     */
30
    public function __construct(AbstractModel $model, bool $short = true)
31
    {
32
        $this->model = $model;
33
        $this->short = $short;
34
    }
35
36
    /**
37
     * Retrieve the Model class's short name (without namespace).
38
     *
39
     * @return mixed
40
     */
41
    public function execute()
42
    {
43
        // Split string on upper case characters
44
        return implode(' ', (new StringHelpers($this->getClassName()))->camelCaseSplit());
45
    }
46
47
    /**
48
     * Retrieve the name from the Model's class name or the table name.
49
     *
50
     * @return string
51
     */
52
    private function getClassName(): string
53
    {
54
        return LaravelHelpers::getClassName($this->model, $this->short, $this->getTableCamelCase());
55
    }
56
57
    /**
58
     * Convert table name to a CamelCase string for consistency with Model names.
59
     *
60
     * @return string
61
     */
62
    private function getTableCamelCase(): string
63
    {
64
        return implode(
65
            '',
66
            array_map(
67
                function (string $piece) {
68
                    return ucfirst($piece);
69
                },
70
                explode('_', $this->model->getTable())
71
            )
72
        );
73
    }
74
}
75