Completed
Branch feature/pre-split (1effb8)
by Anton
03:04
created

PostgresIndex::createInstance()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 2
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\Database\Drivers\Postgres\Schemas;
8
9
use Spiral\Database\Schemas\Prototypes\AbstractIndex;
10
11
class PostgresIndex extends AbstractIndex
12
{
13
    /**
14
     * @param string $table Table name.
15
     * @param array  $schema
16
     *
17
     * @return PostgresIndex
18
     */
19
    public static function createInstance(string $table, array $schema): self
20
    {
21
        $index = new self($table, $schema['indexname']);
22
        $index->type = strpos($schema['indexdef'], ' UNIQUE ') ? self::UNIQUE : self::NORMAL;
23
24
        if (preg_match('/\(([^)]+)\)/', $schema['indexdef'], $matches)) {
25
            $columns = explode(',', $matches[1]);
26
27
            foreach ($columns as $column) {
28
                //Postgres adds quotes to all columns with uppercase letters
29
                $index->columns[] = trim($column, ' "\'');
30
            }
31
        }
32
33
        return $index;
34
    }
35
}