Repository navigation
Expand file tree
/
Copy pathMetadataFeature.php
More file actions
78 lines (63 loc) · 2.06 KB
/
Copy pathMetadataFeature.php
File metadata and controls
78 lines (63 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace PhpDb\TableGateway\Feature;
use PhpDb\Metadata\MetadataInterface;
use PhpDb\Metadata\Object\TableObject;
use PhpDb\Sql\TableIdentifier;
use PhpDb\TableGateway\Exception;
use function count;
use function current;
use function is_array;
class MetadataFeature extends AbstractFeature
{
/**
* Constructor
*/
public function __construct(
protected MetadataInterface $metadata
) {
$this->sharedData['metadata'] = [
'primaryKey' => null,
'columns' => [],
];
}
public function postInitialize()
{
// localize variable for brevity
$t = $this->tableGateway;
$m = $this->metadata;
$tableGatewayTable = is_array($t->table) ? current($t->table) : $t->table;
if ($tableGatewayTable instanceof TableIdentifier) {
$table = $tableGatewayTable->getTable();
$schema = $tableGatewayTable->getSchema();
} else {
$table = $tableGatewayTable;
$schema = null;
}
// get column named
$columns = $m->getColumnNames($table, $schema);
$t->columns = $columns;
// set locally
$this->sharedData['metadata']['columns'] = $columns;
// process primary key only if table is a table; there are no PK constraints on views
if (! $m->getTable($table, $schema) instanceof TableObject) {
return;
}
$pkc = null;
foreach ($m->getConstraints($table, $schema) as $constraint) {
if ($constraint->getType() === 'PRIMARY KEY') {
$pkc = $constraint;
break;
}
}
if ($pkc === null) {
throw new Exception\RuntimeException('A primary key for this column could not be found in the metadata.');
}
$pkcColumns = $pkc->getColumns();
if (count($pkcColumns) === 1) {
$primaryKey = $pkcColumns[0];
} else {
$primaryKey = $pkcColumns;
}
$this->sharedData['metadata']['primaryKey'] = $primaryKey;
}
}