Completed
Push — master ( 7c9586...c8ce39 )
by Oscar
01:44
created

CommonsTrait::getTables()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 2
nop 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace SimpleCrud\Scheme\Traits;
5
6
use PDO;
7
8
trait CommonsTrait
9
{
10
    private $scheme;
11
    private $pdo;
12
13
    public function __construct(PDO $pdo)
14
    {
15
        $this->pdo = $pdo;
16
    }
17
18
    /**
19
     * @see SchemeInterface
20
     */
21
    public function toArray(): array
22
    {
23
        $array = [];
24
25
        foreach ($this->getTables() as $table) {
26
            $array[$table] = $this->getTableFields($table);
27
        }
28
29
        return $array;
30
    }
31
32
    /**
33
     * @see SchemeInterface
34
     */
35
    public function getTables(): array
36
    {
37
        if (!is_array($this->scheme)) {
38
            $this->scheme = [];
39
40
            foreach ($this->loadTables() as $table) {
41
                $this->scheme[$table] = null;
42
            }
43
        }
44
45
        return array_keys($this->scheme);
46
    }
47
48
    /**
49
     * @see SchemeInterface
50
     */
51
    public function getTableFields(string $table): array
52
    {
53
        if (!is_array($this->scheme[$table])) {
54
            $this->scheme[$table] = $this->loadTableFields($table);
55
        }
56
57
        return $this->scheme[$table];
58
    }
59
60
    /**
61
     * Return all tables.
62
     */
63
    abstract protected function loadTables(): array;
64
65
    /**
66
     * Return the scheme of a table.
67
     */
68
    abstract protected function loadTableFields(string $table): array;
69
}
70