Blueprint::creation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php namespace jlourenco\support\Database;
2
3
use Illuminate\Database\Schema\Blueprint as IlluminateBlueprint;
4
5
/**
6
 * Extended version of Blueprint with
7
 * support of 'set' data type
8
 */
9
class Blueprint extends IlluminateBlueprint {
10
11
    /**
12
     * Create a new binary column on the table.
13
     *
14
     * @param  string  $column
15
     * @param  int  $length
16
     * @return \Illuminate\Support\Fluent
17
     */
18
    public function binary($column, $length = 255)
19
    {
20
        return $this->addColumn('binary', $column, compact('length'));
21
    }
22
23
    /**
24
     * Add creation and update user information to the table.
25
     *
26
     * @return void
27
     */
28
    public function creation()
29
    {
30
        $this->integer('created_by')->nullable()->unsigned();
31
        $this->integer('modified_by')->nullable()->unsigned();
32
        $this->integer('deleted_by')->nullable()->unsigned();
33
    }
34
35
    public function creationRelation()
36
    {
37
        $tab = config('jlourenco.support.relations.UsersTable');
38
        $col = config('jlourenco.support.relations.UsersColumn');
39
40
        $this->foreign('created_by')->references($col)->on($tab);
41
        $this->foreign('modified_by')->references($col)->on($tab);
42
        $this->foreign('deleted_by')->references($col)->on($tab);
43
    }
44
45
    /**
46
     * Create a new 'set' column on the table.
47
     *
48
     * @param  string  $column
49
     * @param  array   $allowed
50
     * @return \Illuminate\Support\Fluent
51
     */
52
    public function set($column, array $allowed)
53
    {
54
        return $this->addColumn('set', $column, compact('allowed'));
55
    }
56
57
    /**
58
     * Specify a unique index for the table.
59
     *
60
     * @param  string|array  $columns
61
     * @param  string        $name
62
     * @param  int           $length
63
     * @return \Illuminate\Support\Fluent
64
     */
65
    public function unique($columns, $name = null, $length = null)
66
    {
67
        return $this->indexCommand('unique', $columns, $name, $length);
68
    }
69
70
    /**
71
     * Specify an index for the table.
72
     *
73
     * @param  string|array  $columns
74
     * @param  string        $name
75
     * @param  int           $length
76
     * @return \Illuminate\Support\Fluent
77
     */
78
    public function index($columns, $name = null, $length = null)
79
    {
80
        return $this->indexCommand('index', $columns, $name, $length);
81
    }
82
83
    /**
84
     * Determine if the given table exists.
85
     *
86
     * @param  string $table
87
     *
88
     * @return bool
89
     */
90
    public function hasForeign($table, $foreign)
91
    {
92
        $sql = $this->grammar->compileHasForeign();
93
        $table = $this->connection->getTablePrefix() . $table;
94
        return count($this->connection->select($sql, [$table, $foreign])) > 0;
95
    }
96
97
    /**
98
     * Add a new index command to the blueprint.
99
     *
100
     * @param  string        $type
101
     * @param  string|array  $columns
102
     * @param  string        $index
103
     * @param  int           $length
104
     * @return \Illuminate\Support\Fluent
105
     */
106
    protected function indexCommand($type, $columns, $index, $length = null)
107
    {
108
        $columns = (array) $columns;
109
        // If no name was specified for this index, we will create one using a basic
110
        // convention of the table name, followed by the columns, followed by an
111
        // index type, such as primary or index, which makes the index unique.
112
        if (is_null($index))
113
            $index = $this->createIndexName($type, $columns);
114
        return $this->addCommand($type, compact('index', 'columns', 'length'));
115
    }
116
117
}