|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Describe; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Describe |
|
7
|
|
|
* |
|
8
|
|
|
* Gets information of a table schema from a database. |
|
9
|
|
|
* |
|
10
|
|
|
* @package Describe |
|
11
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Describe |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $columns = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var \Rougin\Describe\Driver\DriverInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $driver; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param \Rougin\Describe\Driver\DriverInterface $driver |
|
27
|
|
|
*/ |
|
28
|
69 |
|
public function __construct(\Rougin\Describe\Driver\DriverInterface $driver) |
|
29
|
|
|
{ |
|
30
|
69 |
|
$this->driver = $driver; |
|
31
|
69 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Gets the primary key in the specified table. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $table |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
12 |
|
public function getPrimaryKey($table) |
|
40
|
|
|
{ |
|
41
|
12 |
|
$result = ''; |
|
42
|
|
|
|
|
43
|
12 |
|
if (empty($this->columns)) { |
|
44
|
12 |
|
$this->columns = $this->driver->getTable($table); |
|
45
|
12 |
|
} |
|
46
|
|
|
|
|
47
|
12 |
|
foreach ($this->columns as $column) { |
|
48
|
12 |
|
if ($column->isPrimaryKey()) { |
|
49
|
12 |
|
$result = $column->getField(); |
|
50
|
12 |
|
} |
|
51
|
12 |
|
} |
|
52
|
|
|
|
|
53
|
12 |
|
return $result; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Returns the result. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $table |
|
|
|
|
|
|
60
|
|
|
* @return array |
|
61
|
|
|
* @throws \Rougin\Describe\Exceptions\TableNameNotFoundException |
|
62
|
|
|
*/ |
|
63
|
45 |
|
public function getTable($tableName) |
|
64
|
|
|
{ |
|
65
|
45 |
|
$table = $this->driver->getTable($tableName); |
|
66
|
|
|
|
|
67
|
45 |
|
if (empty($table) || is_null($table)) { |
|
68
|
|
|
$message = '"' . $tableName . '" table not found in database!'; |
|
69
|
|
|
|
|
70
|
|
|
throw new Exceptions\TableNameNotFoundException($message); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
45 |
|
return $table; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Shows the list of tables. |
|
78
|
|
|
* |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
12 |
|
public function showTables() |
|
82
|
|
|
{ |
|
83
|
12 |
|
return $this->driver->showTables(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Calls methods from this class in underscore case. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $method |
|
90
|
|
|
* @param mixed $parameters |
|
91
|
|
|
* @return mixed |
|
92
|
|
|
*/ |
|
93
|
36 |
|
public function __call($method, $parameters) |
|
94
|
|
|
{ |
|
95
|
36 |
|
return MagicMethodHelper::call($this, $method, $parameters); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.