|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Records\Traits\TableStructure; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Trait TableStructureRecordsTrait |
|
7
|
|
|
* @package Nip\Records\Traits\TableStructure |
|
8
|
|
|
*/ |
|
9
|
|
|
trait TableStructureRecordsTrait |
|
10
|
|
|
{ |
|
11
|
|
|
protected $tableStructure = null; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var null|array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $fields = null; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @return null |
|
20
|
|
|
*/ |
|
21
|
4 |
|
public function getFields() |
|
22
|
|
|
{ |
|
23
|
4 |
|
$this->checkFieldsIsInitiated(); |
|
24
|
|
|
|
|
25
|
4 |
|
return $this->fields; |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param array|null $fields |
|
30
|
|
|
*/ |
|
31
|
2 |
|
public function setFields(?array $fields): void |
|
32
|
|
|
{ |
|
33
|
2 |
|
$this->fields = $fields; |
|
34
|
2 |
|
} |
|
35
|
|
|
|
|
36
|
4 |
|
protected function checkFieldsIsInitiated() |
|
37
|
|
|
{ |
|
38
|
4 |
|
if ($this->fields === null) { |
|
39
|
2 |
|
$this->initFields(); |
|
40
|
|
|
} |
|
41
|
4 |
|
} |
|
42
|
|
|
|
|
43
|
2 |
|
public function initFields() |
|
44
|
|
|
{ |
|
45
|
2 |
|
$structure = $this->getTableStructure(); |
|
46
|
2 |
|
$this->setFields(array_keys($structure['fields'])); |
|
47
|
2 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $name |
|
51
|
|
|
* @return bool |
|
52
|
|
|
*/ |
|
53
|
|
|
public function hasField(string $name) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->checkFieldsIsInitiated(); |
|
56
|
|
|
return isset($this->fields[$name]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return mixed |
|
61
|
|
|
*/ |
|
62
|
5 |
|
public function getTableStructure() |
|
63
|
|
|
{ |
|
64
|
5 |
|
if ($this->tableStructure == null) { |
|
65
|
2 |
|
$this->initTableStructure(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
5 |
|
return $this->tableStructure; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param null $tableStructure |
|
|
|
|
|
|
73
|
|
|
*/ |
|
74
|
7 |
|
public function setTableStructure($tableStructure) |
|
75
|
|
|
{ |
|
76
|
7 |
|
$this->tableStructure = $tableStructure; |
|
77
|
7 |
|
} |
|
78
|
|
|
|
|
79
|
2 |
|
protected function initTableStructure() |
|
80
|
|
|
{ |
|
81
|
2 |
|
$this->setTableStructure($this->generateTableStructure()); |
|
82
|
2 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function generateTableStructure() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->getDB()->getMetadata()->describeTable($this->getTable()); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|