1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\Migrations\Operation\Column; |
13
|
|
|
|
14
|
|
|
use Cycle\Database\Schema\AbstractColumn; |
15
|
|
|
use Cycle\Database\Schema\AbstractTable; |
16
|
|
|
use Cycle\Migrations\Exception\Operation\ColumnException; |
17
|
|
|
use Cycle\Migrations\Operation\AbstractOperation; |
18
|
|
|
use Cycle\Migrations\Operation\Traits\OptionsTrait; |
19
|
|
|
|
20
|
|
|
abstract class Column extends AbstractOperation |
21
|
|
|
{ |
22
|
|
|
use OptionsTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Some options has set of aliases. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $aliases = [ |
30
|
|
|
'size' => ['length', 'limit'], |
31
|
|
|
'default' => ['defaultValue'], |
32
|
|
|
'null' => ['nullable'], |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** @var string */ |
36
|
|
|
protected $name; |
37
|
|
|
|
38
|
|
|
/** @var string */ |
39
|
|
|
protected $type; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $table |
43
|
|
|
* @param string $columns |
44
|
|
|
* @param string $type |
45
|
|
|
* @param array $options |
46
|
|
|
*/ |
47
|
|
|
public function __construct( |
48
|
|
|
string $table, |
49
|
|
|
string $columns, |
50
|
|
|
string $type = 'string', |
51
|
|
|
array $options = [] |
52
|
|
|
) { |
53
|
|
|
parent::__construct($table); |
54
|
|
|
$this->name = $columns; |
55
|
|
|
$this->type = $type; |
56
|
|
|
$this->options = $options; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param AbstractTable $schema |
61
|
|
|
* |
62
|
|
|
* @throws ColumnException |
63
|
|
|
* |
64
|
|
|
* @return AbstractColumn |
65
|
|
|
*/ |
66
|
|
|
protected function declareColumn(AbstractTable $schema): AbstractColumn |
67
|
|
|
{ |
68
|
|
|
$column = $schema->column($this->name); |
69
|
|
|
|
70
|
|
|
//Type configuring |
71
|
|
|
if (method_exists($column, $this->type)) { |
72
|
|
|
$arguments = []; |
73
|
|
|
|
74
|
|
|
$method = new \ReflectionMethod($column, $this->type); |
75
|
|
|
foreach ($method->getParameters() as $parameter) { |
76
|
|
|
if ($this->hasOption($parameter->getName())) { |
77
|
|
|
$arguments[] = $this->getOption($parameter->getName()); |
78
|
|
|
} elseif (!$parameter->isOptional()) { |
79
|
|
|
throw new ColumnException( |
80
|
|
|
"Option '{$parameter->getName()}' are required to define column with type '{$this->type}'" |
81
|
|
|
); |
82
|
|
|
} else { |
83
|
|
|
$arguments[] = $parameter->getDefaultValue(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
call_user_func_array([$column, $this->type], $arguments); |
88
|
|
|
} else { |
89
|
|
|
$column->type($this->type); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$column->nullable($this->getOption('nullable', false)); |
|
|
|
|
93
|
|
|
|
94
|
|
|
if ($this->hasOption('default')) { |
95
|
|
|
$column->defaultValue($this->getOption('default', null)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $column; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|