TypeResolver   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 47
c 1
b 0
f 0
dl 0
loc 84
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getType() 0 15 2
1
<?php
2
namespace Prateekkarki\Laragen\Models;
3
4
use Prateekkarki\Laragen\Models\Types\General\StringType;
5
use Prateekkarki\Laragen\Models\Types\General\BooleanType;
6
use Prateekkarki\Laragen\Models\Types\General\IntegerType;
7
use Prateekkarki\Laragen\Models\Types\General\TextType;
8
use Prateekkarki\Laragen\Models\Types\General\DateTimeType;
9
use Prateekkarki\Laragen\Models\Types\General\DateType;
10
use Prateekkarki\Laragen\Models\Types\File\Single\ImageType;
11
use Prateekkarki\Laragen\Models\Types\File\Single\MixedFileType;
12
use Prateekkarki\Laragen\Models\Types\File\Multiple\AttachmentType;
13
use Prateekkarki\Laragen\Models\Types\File\Multiple\GalleryType;
14
use Prateekkarki\Laragen\Models\Types\Relational\Single\ParentType;
15
use Prateekkarki\Laragen\Models\Types\Relational\Single\OptionType;
16
use Prateekkarki\Laragen\Models\Types\Relational\Multiple\MultipleDataType;
17
use Prateekkarki\Laragen\Models\Types\Relational\Multiple\RelatedType;
18
19
/**
20
 * Contains static properties and methods usable in other classes
21
 */
22
class TypeResolver
23
{
24
    const TYPE_PARENT = 'parent';
25
    const TYPE_RELATED = 'related';
26
    const COLUMN_UNIQUE = 'unique';
27
    const COLUMN_REQUIRED = 'required';
28
    const COLUMN_DISPLAY = 'display';
29
30
    /**
31
     * List of all types of data.
32
     *
33
     * @var array
34
     */
35
    public static $types = [
36
        'integer',
37
        'string',
38
        'boolean',
39
        'text',
40
        'date',
41
        'datetime'
42
    ];
43
44
    public static $fileTypes = [
45
        'image',
46
        'file'
47
    ];
48
49
    public static $specialTypes = [
50
        'parent',
51
        'related'
52
    ];
53
54
    public static $relatedMultiple = [
55
        'gallery',
56
        'multiple',
57
        'related'
58
    ];
59
60
    /**
61
     * Key to laragen type conversion array.
62
     *
63
     * @var array
64
     */
65
    protected static $keyToLaragenType = [
66
        'string' => StringType::class,
67
        'boolean' => BooleanType::class,
68
        'integer' => IntegerType::class,
69
        'text' => TextType::class,
70
        'datetime' => DateTimeType::class,
71
        'date' => DateType::class,
72
        'image' => ImageType::class,
73
        'file' => MixedFileType::class,
74
        'gallery' => GalleryType::class,
75
        'attachments' => AttachmentType::class,
76
        'parent' => ParentType::class,
77
        'options' => OptionType::class,
78
        'related' => RelatedType::class,
79
        'multiple' => MultipleDataType::class
80
    ];
81
82
    /**
83
     * Finds the data type of of given column and
84
     * returns an implementation of \Prateekkarki\Laragen\Models\Types\LaragenType
85
     *
86
     * @param string $moduleName
87
     * @param string $columnName
88
     * @param string $optionString
89
     * @return \Prateekkarki\Laragen\Models\Types\LaragenType
90
     */
91
    public static function getType($moduleName, $columnName, $optionString)
92
    {
93
        // Find the datatype from the option string
94
        if (is_array($optionString)) {
0 ignored issues
show
introduced by
The condition is_array($optionString) is always false.
Loading history...
95
            $dataType = 'multiple';
96
        } else {
97
            $optionArray = explode('|', $optionString);
98
            $typePieces = array_shift($optionArray);
99
            $types = explode(':', $typePieces);
100
            $dataType = $types[0];
101
        }
102
103
        // Create and return an instance of required type
104
        $class = self::$keyToLaragenType[$dataType];
105
        return new $class($moduleName, $columnName, $optionString);
106
    }
107
}
108