|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace donatj\MySqlSchema\Columns; |
|
4
|
|
|
|
|
5
|
|
|
use donatj\MySqlSchema\Columns\Interfaces\CharsetColumnInterface; |
|
6
|
|
|
use donatj\MySqlSchema\Columns\Interfaces\OptionalLengthInterface; |
|
7
|
|
|
use donatj\MySqlSchema\Columns\Interfaces\RequiredLengthInterface; |
|
8
|
|
|
use donatj\MySqlSchema\Columns\Interfaces\SignedInterface; |
|
9
|
|
|
use donatj\MySqlSchema\Columns\Numeric\AbstractIntegerColumn; |
|
10
|
|
|
use donatj\MySqlSchema\Table; |
|
11
|
|
|
use donatj\MySqlSchema\Traits\EscapeTrait; |
|
12
|
|
|
|
|
13
|
|
|
abstract class AbstractColumn { |
|
14
|
|
|
|
|
15
|
|
|
use EscapeTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var \donatj\MySqlSchema\Table[] |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $tables = [ ]; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $name; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $comment = ''; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var bool |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $nullable = false; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var mixed |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $default; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $name |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct( $name ) { |
|
42
|
|
|
$this->name = $name; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @access private |
|
47
|
|
|
* @param \donatj\MySqlSchema\Table $table |
|
48
|
|
|
*/ |
|
49
|
|
|
public function addTable( Table $table ) { |
|
50
|
|
|
$this->tables[spl_object_hash($table)] = $table; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return \donatj\MySqlSchema\Table[] |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getTables() { |
|
57
|
|
|
return array_values($this->tables); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getComment() { |
|
64
|
|
|
return $this->comment; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $comment |
|
69
|
|
|
*/ |
|
70
|
|
|
public function setComment( $comment ) { |
|
71
|
|
|
$this->comment = $comment; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return boolean |
|
76
|
|
|
*/ |
|
77
|
|
|
public function isNullable() { |
|
78
|
|
|
return $this->nullable; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param boolean $nullable |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setNullable( $nullable ) { |
|
85
|
|
|
$this->nullable = $nullable; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getName() { |
|
92
|
|
|
return $this->name; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param string $name |
|
97
|
|
|
*/ |
|
98
|
|
|
public function setName( $name ) { |
|
99
|
|
|
$this->name = $name; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param \donatj\MySqlSchema\Table $table |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
public function toString( Table $table ) { |
|
107
|
|
|
$type = $this->getTypeName(); |
|
108
|
|
|
|
|
109
|
|
|
$nullable = ''; |
|
110
|
|
|
if( !$this->nullable ) { |
|
111
|
|
|
$nullable = ' NOT NULL'; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$length = ''; |
|
115
|
|
|
if( $this instanceof RequiredLengthInterface || |
|
116
|
|
|
($this instanceof OptionalLengthInterface && $this->getLength()) |
|
|
|
|
|
|
117
|
|
|
) { |
|
118
|
|
|
$l = intval($this->getLength()); |
|
|
|
|
|
|
119
|
|
|
$length = "($l)"; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$signed = ''; |
|
123
|
|
|
if( $this instanceof SignedInterface ) { |
|
124
|
|
|
if( !$this->isSigned() ) { |
|
|
|
|
|
|
125
|
|
|
$signed = " unsigned"; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$default = ''; |
|
130
|
|
|
if( !is_null($this->default) ) { |
|
131
|
|
|
$default = ' DEFAULT ' . $this->mkString($this->default, "'");; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$charset = ''; |
|
135
|
|
|
$collation = ''; |
|
136
|
|
View Code Duplication |
if( $this instanceof CharsetColumnInterface ) { |
|
|
|
|
|
|
137
|
|
|
if( $this->getCharset() ) { |
|
|
|
|
|
|
138
|
|
|
$charset = ' CHARACTER SET ' . $this->getCharset(); |
|
|
|
|
|
|
139
|
|
|
if( $this->getCollation() ) { |
|
|
|
|
|
|
140
|
|
|
$collation = ' COLLATE ' . $this->getCollation(); |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$comment = ''; |
|
146
|
|
|
if( $this->comment ) { |
|
147
|
|
|
$comment = ' COMMENT ' . $this->mkString($this->comment, "'"); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$autoIncrement = ''; |
|
151
|
|
|
if( $this instanceof AbstractIntegerColumn ) { |
|
152
|
|
|
if( $table->isAutoIncrement($this) ) { |
|
|
|
|
|
|
153
|
|
|
$autoIncrement = ' AUTO_INCREMENT'; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$name = $this->mkString($this->name); |
|
158
|
|
|
|
|
159
|
|
|
return "{$name} {$type}{$length}{$signed}{$charset}{$collation}{$nullable}{$default}{$autoIncrement}{$comment}"; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @return string |
|
164
|
|
|
*/ |
|
165
|
|
|
abstract public function getTypeName(); |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @return mixed |
|
169
|
|
|
*/ |
|
170
|
|
|
public function getDefault() { |
|
171
|
|
|
return $this->default; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param mixed $default |
|
176
|
|
|
*/ |
|
177
|
|
|
public function setDefault( $default ) { |
|
178
|
|
|
$this->default = $default; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: