1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rentgen\Database; |
4
|
|
|
|
5
|
|
|
abstract class Column implements DatabaseObjectInterface |
6
|
|
|
{ |
7
|
|
|
protected $name; |
8
|
|
|
protected $description; |
9
|
|
|
protected $isNotNull = false; |
10
|
|
|
protected $default; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Constructor. |
14
|
|
|
* |
15
|
|
|
* @param string $name Column name. |
16
|
|
|
* @param array $options Optional options. |
17
|
|
|
*/ |
18
|
|
|
public function __construct($name, array $options = array()) |
19
|
|
|
{ |
20
|
|
|
$this->name = $name; |
21
|
|
|
|
22
|
|
|
if (array_key_exists('not_null', $options)) { |
23
|
|
|
$this->isNotNull = $options['not_null']; |
24
|
|
|
} |
25
|
|
|
if (array_key_exists('default', $options)) { |
26
|
|
|
$this->default = $options['default']; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get column name. |
32
|
|
|
* |
33
|
|
|
* @return string Column name. |
34
|
|
|
*/ |
35
|
|
|
public function getName() |
36
|
|
|
{ |
37
|
|
|
return $this->name; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get default value of column. |
42
|
|
|
* |
43
|
|
|
* @return mixed Default value of column. |
44
|
|
|
*/ |
45
|
|
|
public function getDefault() |
46
|
|
|
{ |
47
|
|
|
return $this->default; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Check if column not allows null value. |
52
|
|
|
* |
53
|
|
|
* @return boolean Column not allows null value. |
54
|
|
|
*/ |
55
|
|
|
public function isNotNull() |
56
|
|
|
{ |
57
|
|
|
return $this->isNotNull; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set the table instance. |
62
|
|
|
* |
63
|
|
|
* @param Table $table Table instance. |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function setTable(Table $table) |
68
|
|
|
{ |
69
|
|
|
$this->table = $table; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the table instance. |
74
|
|
|
* |
75
|
|
|
* @return Table |
76
|
|
|
*/ |
77
|
|
|
public function getTable() |
78
|
|
|
{ |
79
|
|
|
return $this->table; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get column type name. |
84
|
|
|
* |
85
|
|
|
* @return string Column type name. |
86
|
|
|
*/ |
87
|
|
|
abstract public function getType(); |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set column description. |
91
|
|
|
* |
92
|
|
|
* @param string $description Table description. |
93
|
|
|
*/ |
94
|
|
|
public function setDescription($description) |
95
|
|
|
{ |
96
|
|
|
$this->description = $description; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get column description. |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getDescription() |
105
|
|
|
{ |
106
|
|
|
return $this->description; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: