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

PropertiesListing   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A listForEloquent() 0 12 2
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