|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ORM\Dbal; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Describes a column of a database table |
|
7
|
|
|
* |
|
8
|
|
|
* @package ORM\Dbal |
|
9
|
|
|
* @author Thomas Flori <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* @property string name |
|
12
|
|
|
* @property type Type |
|
13
|
|
|
* @property mixed default |
|
14
|
|
|
* @property bool nullable |
|
15
|
|
|
*/ |
|
16
|
|
|
class Column |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var string[] */ |
|
19
|
|
|
protected static $registeredTypes = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Register $type for describe |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $type The full qualified class name |
|
25
|
|
|
*/ |
|
26
|
7 |
|
public static function registerType($type) |
|
27
|
|
|
{ |
|
28
|
7 |
|
if (!in_array($type, static::$registeredTypes)) { |
|
29
|
7 |
|
array_unshift(static::$registeredTypes, $type); |
|
30
|
|
|
} |
|
31
|
7 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** @var array */ |
|
34
|
|
|
protected $columnDefinition; |
|
35
|
|
|
|
|
36
|
|
|
/** @var Dbal */ |
|
37
|
|
|
protected $dbal; |
|
38
|
|
|
|
|
39
|
|
|
/** @var TypeInterface */ |
|
40
|
|
|
protected $type; |
|
41
|
|
|
|
|
42
|
|
|
/** @var bool */ |
|
43
|
|
|
protected $hasDefault; |
|
44
|
|
|
|
|
45
|
|
|
/** @var bool */ |
|
46
|
|
|
protected $isNullable; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Column constructor. |
|
50
|
|
|
* |
|
51
|
|
|
* @param Dbal $dbal |
|
52
|
|
|
* @param array $columnDefinition |
|
53
|
|
|
*/ |
|
54
|
110 |
|
public function __construct(Dbal $dbal, array $columnDefinition) |
|
55
|
|
|
{ |
|
56
|
110 |
|
$this->dbal = $dbal; |
|
57
|
110 |
|
$this->columnDefinition = $columnDefinition; |
|
58
|
110 |
|
} |
|
59
|
|
|
|
|
60
|
5 |
|
public function validate($value) |
|
61
|
|
|
{ |
|
62
|
5 |
|
if ($value === null) { |
|
63
|
3 |
|
if ($this->nullable || $this->hasDefault()) { |
|
64
|
2 |
|
return true; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
return new Error\NotNullable($this); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
2 |
|
return $this->getType()->validate($value); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
110 |
|
public function __get($name) |
|
74
|
|
|
{ |
|
75
|
|
|
switch ($name) { |
|
76
|
110 |
|
case 'name': |
|
77
|
108 |
|
return $this->columnDefinition['column_name']; |
|
78
|
20 |
|
case 'type': |
|
79
|
2 |
|
return $this->getType(); |
|
80
|
19 |
|
case 'default': |
|
81
|
12 |
|
return $this->columnDefinition['column_default']; |
|
82
|
9 |
|
case 'nullable': |
|
83
|
9 |
|
return $this->columnDefinition['is_nullable'] === true || |
|
84
|
9 |
|
$this->columnDefinition['is_nullable'] === 'YES'; |
|
85
|
|
|
default: |
|
86
|
|
|
return isset($this->columnDefinition[$name]) ? $this->columnDefinition[$name] : null; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
12 |
|
public function hasDefault() |
|
91
|
|
|
{ |
|
92
|
12 |
|
return $this->default !== null; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return Type |
|
97
|
|
|
*/ |
|
98
|
84 |
|
public function getType() |
|
99
|
|
|
{ |
|
100
|
84 |
|
if (!$this->type) { |
|
101
|
84 |
|
$class = null; |
|
102
|
|
|
|
|
103
|
84 |
|
if (isset($this->columnDefinition['type']) && class_exists($this->columnDefinition['type'])) { |
|
104
|
77 |
|
$class = $this->columnDefinition['type']; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
84 |
|
if (!$class) { |
|
108
|
7 |
|
foreach (self::$registeredTypes as $c) { |
|
109
|
4 |
|
if (call_user_func([$c, 'fits'], $this->columnDefinition)) { |
|
110
|
4 |
|
$class = $c; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
7 |
|
$class = $class ?: Type\Text::class; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
84 |
|
$this->type = call_user_func([$class, 'factory'], $this->dbal, $this->columnDefinition); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
84 |
|
return $this->type; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|