Passed
Pull Request — master (#25)
by Prateek
03:11
created

TypeResolver::getType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 15
rs 9.9666
cc 2
nc 2
nop 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
/**
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
29
    /**
30
     * List of all types of data.
31
     *
32
     * @var array
33
     */
34
    public static $types = [
35
        'integer',
36
        'string',
37
        'boolean',
38
        'text',
39
        'date',
40
        'datetime'
41
    ];
42
43
    public static $fileTypes = [
44
        'image',
45
        'file'
46
    ];
47
48
    public static $specialTypes = [
49
        'parent',
50
        'related'
51
    ];
52
53
    public static $relatedMultiple = [
54
        'gallery',
55
        'multiple',
56
        'related'
57
    ];
58
59
    /**
60
     * Key to laragen type conversion array.
61
     *
62
     * @var array
63
     */
64
    protected static $keyToLaragenType = [
65
        'string' => StringType::class,
66
        'boolean' => BooleanType::class,
67
        'integer' => IntegerType::class,
68
        'text' => TextType::class,
69
        'datetime' => DateTimeType::class,
70
        'date' => DateType::class,
71
        'image' => ImageType::class,
72
        'file' => MixedFileType::class,
73
        'gallery' => GalleryType::class,
74
        'attachments' => AttachmentType::class,
75
        'parent' => ParentType::class,
76
        'options' => OptionType::class,
77
        'related' => RelatedType::class,
78
        'multiple' => MultipleDataType::class
79
    ];
80
81
    /**
82
     * Finds the data type of of given column and
83
     * returns an implementation of \Prateekkarki\Laragen\Models\Types\LaragenType
84
     *
85
     * @param string $moduleName
86
     * @param string $columnName
87
     * @param string $optionString
88
     * @return \Prateekkarki\Laragen\Models\Types\LaragenType
89
     */
90
    public static function getType($moduleName, $columnName, $optionString)
91
    {
92
        // Find the datatype from the option string
93
        if (is_array($optionString)) {
0 ignored issues
show
introduced by
The condition is_array($optionString) is always false.
Loading history...
94
            $dataType = 'multiple';
95
        } else {
96
            $optionArray = explode('|', $optionString);
97
            $typePieces = array_shift($optionArray);
98
            $types = explode(':', $typePieces);
99
            $dataType = $types[0];
100
        }
101
102
        // Create and return an instance of required type
103
        $class = self::$keyToLaragenType[$dataType];
104
        return new $class($moduleName, $columnName, $optionString);
105
    }
106
}
107