BaseDefinition::studlyId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Distilleries\Contentful\Commands\Generators\Definitions;
4
5
use Illuminate\Support\Str;
6
7
abstract class BaseDefinition implements DefinitionInterface
8
{
9
    /**
10
     * Model table.
11
     *
12
     * @var string
13
     */
14
    protected $table;
15
16
    /**
17
     * Field data.
18
     *
19
     * @var array
20
     */
21
    protected $field;
22
23
    /**
24
     * BaseDefinition constructor.
25
     *
26
     * @param  string  $table
27
     * @param  array  $field
28
     * @return void
29
     */
30
    public function __construct($table, $field)
31
    {
32
        $this->table = $table;
33
34
        $this->field = $field;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    abstract public function modelGetter();
41
42
    /**
43
     * Return normalized ID of current field.
44
     *
45
     * @return string
46
     */
47
    protected function id(): string
48
    {
49
        return $this->field['id'];
50
    }
51
52
    /**
53
     * Return studly case ID of current field.
54
     *
55
     * @return string
56
     */
57
    protected function studlyId(): string
58
    {
59
        return Str::studly($this->id());
60
    }
61
62
    /**
63
     * Return snake case ID of current field.
64
     *
65
     * @return string
66
     */
67
    protected function snakeId(): string
68
    {
69
        return Str::snake($this->id());
70
    }
71
72
    /**
73
     * Write stub to destination path with given string replacements.
74
     *
75
     * Return relative base path of destination path.
76
     *
77
     * @param  string  $stubPath
78
     * @param  array  $replacements
79
     * @return string
80
     */
81
    public static function getStub(string $stubPath, array $replacements = []): string
82
    {
83
        $content = file_get_contents($stubPath);
84
85
        foreach ($replacements as $key => $value) {
86
            $content = str_replace('{{' . Str::upper($key) . '}}', $value, $content);
87
        }
88
89
        return $content;
90
    }
91
}
92