Passed
Pull Request — master (#23)
by Prateek
07:40
created

TypeResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 61
c 1
b 0
f 0
dl 0
loc 104
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getLaragenType() 0 2 1
A __construct() 0 11 3
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
class TypeResolver
20
{
21
    const TYPE_PARENT = 'parent';
22
    const TYPE_RELATED = 'related';
23
    const COLUMN_UNIQUE = 'unique';
24
    const COLUMN_REQUIRED = 'required';
25
26
    protected $specialType;
27
    protected $uniqueFlag;
28
    protected $requiredFlag;
29
    protected $size;
30
    protected $column;
31
32
    /**
33
     * List of all types of data.
34
     *
35
     * @var array
36
     */
37
    public static $types = [
38
        'integer',
39
        'string',
40
        'boolean',
41
        'text',
42
        'date',
43
        'datetime'
44
    ];
45
46
    public static $fileTypes = [   
47
        'image',
48
        'file'     
49
    ];
50
51
    public static $specialTypes = [
52
        'parent',
53
        'related'
54
    ];
55
56
    public static $relatedMultiple = [
57
        'gallery',
58
        'multiple',
59
        'related'
60
    ];
61
62
    /**
63
     * Array of data type options
64
     *
65
     * @var array
66
     */
67
    protected $optionArray;
68
69
    
70
    /**
71
     * Key to type conversion array.
72
     *
73
     * @var array
74
     */
75
    protected $keyToType = [
76
        'integer' =>'integer',
77
        'string' =>'string',
78
        'image' =>'string',
79
        'file' =>'string',
80
        'boolean' =>'boolean',
81
        'text' =>'text',
82
        'datetime' =>'datetime',
83
        'date' =>'date'
84
    ];
85
86
    /**
87
     * Key to laragen type conversion array.
88
     *
89
     * @var array
90
     */
91
    protected $keyToLaragenType = [
92
        'string' => StringType::class,
93
        'boolean' => BooleanType::class,
94
        'integer' => IntegerType::class,
95
        'text' => TextType::class,
96
        'datetime' => DateTimeType::class,
97
        'date' => DateType::class,
98
        'image' => ImageType::class,
99
        'file' => MixedFileType::class,
100
        'gallery' => GalleryType::class,
101
        'attachments' => AttachmentType::class,
102
        'parent' => ParentType::class,
103
        'options' => OptionType::class,
104
        'related' => RelatedType::class,
105
        'multiple' => MultipleDataType::class
106
    ];
107
108
    public function __construct($moduleName, $columnName, $optionString)
109
    { 
110
        if (is_array($optionString)) {
111
            $this->dataType = 'multiple';
0 ignored issues
show
Bug Best Practice introduced by
The property dataType does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
112
        } else {
113
            $this->optionArray = explode('|', $optionString);
114
            $typePieces = array_shift($this->optionArray);
115
            $type = explode(':', $typePieces);
116
            $this->dataType = is_array($type) ? $type[0] : $type;
0 ignored issues
show
introduced by
The condition is_array($type) is always true.
Loading history...
117
        }
118
        $this->laragenType = new $this->keyToLaragenType[$this->dataType]($moduleName, $columnName, $optionString);
0 ignored issues
show
Bug Best Practice introduced by
The property laragenType does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
119
    }
120
121
    public function getLaragenType() {
122
        return $this->laragenType;
123
    }
124
}
125