Completed
Push — 4.x ( da046c...d9a6cf )
by Kit Loong
08:01
created

GeometryField::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: liow.kitloong
5
 * Date: 2020/04/07
6
 */
7
8
namespace KitLoong\MigrationsGenerator\Generators;
9
10
use KitLoong\MigrationsGenerator\MigrationGeneratorSetting;
11
use KitLoong\MigrationsGenerator\MigrationMethod\PgSQLGeography;
12
use KitLoong\MigrationsGenerator\Repositories\PgSQLRepository;
13
14
class GeometryField
15
{
16
    private $pgsqlRepository;
17
18
    public function __construct(PgSQLRepository $pgSQLRepository)
19
    {
20
        $this->pgsqlRepository = $pgSQLRepository;
21
    }
22
23
    public function makeField(string $tableName, array $field)
24
    {
25
        /** @var MigrationGeneratorSetting $setting */
26
        $setting = app(MigrationGeneratorSetting::class);
27
28
        switch ($setting->getPlatform()) {
29
            case Platform::POSTGRESQL:
30
                $columnType = $this->pgsqlRepository->getTypeByColumnName($tableName, $field['field']);
31
                if ($columnType !== null) {
32
                    $type = strtolower($columnType);
33
                    $type = preg_replace('/\s+/', '', $type);
34
35
                    if (isset(PgSQLGeography::MAP[$type])) {
36
                        $field['type'] = PgSQLGeography::MAP[$type];
37
                    }
38
                }
39
                break;
40
            default:
41
        }
42
        return $field;
43
    }
44
}
45