Completed
Pull Request — master (#11)
by Andrea Marco
02:56
created

PropertiesListing::listForEloquent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Rexlabs\Laravel\Smokescreen\Console;
4
5
use Illuminate\Support\Facades\Schema;
6
7
/**
8
 * The properties listing.
9
 *
10
 */
11
class PropertiesListing
12
{
13
    /**
14
     * The map of property types.
15
     *
16
     * @var array
17
     */
18
    protected $typesMap = [
19
        'guid' => 'string',
20
        'boolean' => 'boolean',
21
        'datetime' => 'datetime',
22
        'string' => 'string',
23
        'json' => 'array',
24
        'integer' => 'integer',
25
        'date' => 'date',
26
        'smallint' => 'integer',
27
        'text' => 'string',
28
        'decimal' => 'float',
29
        'bigint' => 'integer',
30
    ];
31
32
    /**
33
     * List the properties of the given Eloquent model.
34
     *
35
     * @param string $class
36
     * @return array
37
     */
38
    public function listForEloquent(string $class) : array
39
    {
40
        $list = [];
41
        $table = (new $class)->getTable();
42
        $columns = Schema::getColumnListing($table);
43
44
        foreach ($columns as $column) {
45
            $type = Schema::getColumnType($table, $column);
46
            $list[$column] = $this->typesMap[$type] ?? null;
47
        }
48
49
        return $list;
50
    }
51
}
52