1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rougin\Describe; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Inflector\Inflector; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Describe |
9
|
|
|
* |
10
|
|
|
* Gets information of a table schema from a database. |
11
|
|
|
* NOTE: To be removed in v2.0.0. Use Table class instead. |
12
|
|
|
* |
13
|
|
|
* @package Describe |
14
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Describe |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \Rougin\Describe\Driver\DriverInterface |
20
|
|
|
*/ |
21
|
|
|
protected $driver; |
22
|
|
|
|
23
|
84 |
|
/** |
24
|
|
|
* @param \Rougin\Describe\Driver\DriverInterface $driver |
25
|
84 |
|
*/ |
26
|
84 |
|
public function __construct(Driver\DriverInterface $driver) |
27
|
|
|
{ |
28
|
|
|
$this->driver = $driver; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Returns an array of Column instances from a table. |
33
|
|
|
* |
34
|
|
|
* @param string $table |
35
|
69 |
|
* @return \Rougin\Describe\Column[] |
36
|
|
|
*/ |
37
|
69 |
|
public function columns($table) |
38
|
|
|
{ |
39
|
69 |
|
return $this->driver->columns($table); |
40
|
6 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
63 |
|
* Returns an array of Column instances from a table. |
44
|
|
|
* NOTE: To be removed in v2.0.0. Use columns() instead. |
45
|
|
|
* |
46
|
|
|
* @param string $table |
47
|
|
|
* @return \Rougin\Describe\Column[] |
48
|
|
|
*/ |
49
|
|
|
public function getColumns($table) |
50
|
|
|
{ |
51
|
|
|
return $this->driver->getColumns($table); |
52
|
|
|
} |
53
|
15 |
|
|
54
|
|
|
/** |
55
|
15 |
|
* Returns the primary key of a table. |
56
|
15 |
|
* NOTE: To be removed in v2.0.0. Use primary() instead. |
57
|
|
|
* |
58
|
15 |
|
* @param string $table |
59
|
15 |
|
* @param boolean $object |
60
|
15 |
|
* @return \Rougin\Describe\Column|string |
61
|
15 |
|
*/ |
62
|
15 |
|
public function getPrimaryKey($table, $object = false) |
63
|
|
|
{ |
64
|
15 |
|
return $this->primary($table, $object); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns an array of columns from a table. |
69
|
|
|
* NOTE: To be removed in v2.0.0. Use getColumns() instead. |
70
|
|
|
* |
71
|
|
|
* @param string $table |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function getTable($table) |
75
|
54 |
|
{ |
76
|
|
|
return $this->driver->getTable($table); |
77
|
54 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns an array of table names. |
81
|
|
|
* NOTE: To be removed in v2.0.0. Use tables() instead. |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
15 |
|
public function getTableNames() |
86
|
|
|
{ |
87
|
15 |
|
return $this->driver->getTableNames(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the primary key of a table. |
92
|
|
|
* |
93
|
|
|
* @param string $table |
94
|
|
|
* @param boolean $object |
95
|
|
|
* @return \Rougin\Describe\Column|string |
96
|
15 |
|
*/ |
97
|
|
|
public function primary($table, $object = false) |
98
|
15 |
|
{ |
99
|
|
|
$table = new Table($table, $this->driver); |
100
|
|
|
|
101
|
|
|
($result = $table->primary()) === null && $result = ''; |
102
|
|
|
|
103
|
|
|
return $object ? $result : $result->getField(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns an array of table names. |
108
|
3 |
|
* NOTE: To be removed in v2.0.0. Use getTableNames() instead. |
109
|
|
|
* |
110
|
3 |
|
* @return array |
111
|
3 |
|
*/ |
112
|
|
|
public function showTables() |
113
|
3 |
|
{ |
114
|
3 |
|
return $this->driver->showTables(); |
115
|
3 |
|
} |
116
|
|
|
|
117
|
3 |
|
/** |
118
|
|
|
* Returns an array of Table instances. |
119
|
|
|
* |
120
|
|
|
* @return \Rougin\Describe\Table[] |
121
|
|
|
*/ |
122
|
|
|
public function tables() |
123
|
|
|
{ |
124
|
|
|
return $this->driver->tables(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Calls methods from this class in underscore case. |
129
|
|
|
* NOTE: To be removed in v2.0.0. All new methods are now in one word. |
130
|
|
|
* |
131
|
|
|
* @param string $method |
132
|
|
|
* @param mixed $parameters |
133
|
|
|
* @return mixed |
134
|
|
|
*/ |
135
|
|
|
public function __call($method, $parameters) |
136
|
|
|
{ |
137
|
|
|
$method = Inflector::camelize($method); |
138
|
|
|
|
139
|
|
|
$instance = array($this, $method); |
140
|
|
|
|
141
|
|
|
return call_user_func_array($instance, $parameters); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|