Decorator::tableUsedInFilename()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: liow.kitloong
5
 * Date: 2020/03/29
6
 */
7
8
namespace KitLoong\MigrationsGenerator\Generators;
9
10
use KitLoong\MigrationsGenerator\MigrationsGeneratorSetting;
11
12
class Decorator
13
{
14
    /**
15
     * Escape content with ' and wrap content with '
16
     *
17
     * @param  string  $args
18
     * @param  string  $quotes
19
     * @return string
20
     */
21 6
    public function columnDefaultToString(string $args, string $quotes = '\''): string
22
    {
23 6
        $args = addslashes($args);
24
        // To replace from ' to \\\'
25 6
        $args = str_replace($quotes, '\\\\\\\\'.$quotes, $args);
26
27 6
        return $quotes.$args.$quotes;
28
    }
29
30
    /**
31
     * Get Decorator
32
     * @param  string  $function
33
     * @param  array  $args
34
     * @return string
35
     */
36 21
    public function decorate(string $function, array $args): string
37
    {
38 21
        if (!empty($args)) {
39 18
            return $function.'('.implode(', ', $args).')';
40
        } else {
41 6
            return $function;
42
        }
43
    }
44
45 57
    public function addSlash(string $string): string
46
    {
47 57
        return addcslashes($string, "\\'");
48
    }
49
50
    public function tableWithoutPrefix(string $table): string
51
    {
52
        /** @var MigrationsGeneratorSetting $setting */
53
        $setting = app(MigrationsGeneratorSetting::class);
54
55
        return substr($table, strlen($setting->getConnection()->getTablePrefix()));
56
    }
57
58
    public function tableUsedInFilename(string $table): string
59
    {
60
        $tableNameEscaped = preg_replace('/[^a-zA-Z0-9_]/', '_', $table);
61
        return $this->tableWithoutPrefix($tableNameEscaped);
62
    }
63
}
64