Completed
Push — master ( f707f5...a08928 )
by Rasmus
02:27
created

Table   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 169
wmc 19
lcom 1
cbo 3
ccs 39
cts 39
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getName() 0 4 1
A getAlias() 0 4 1
A getNode() 0 8 2
C listColumns() 0 34 7
A requiredColumn() 0 4 1
A optionalColumn() 0 4 1
A autoColumn() 0 4 1
A __toString() 0 4 2
A __get() 0 6 2
1
<?php
2
3
namespace mindplay\sql\model\schema;
4
5
use mindplay\sql\model\Driver;
6
use mindplay\sql\model\TypeProvider;
7
use ReflectionClass;
8
use ReflectionMethod;
9
10
/**
11
 * This is an abstract base-class for user-defined Table-types belonging to a Schema.
12
 */
13
abstract class Table
14
{
15
    /**
16
     * @var Driver
17
     */
18
    private $driver;
19
20
    /**
21
     * @var TypeProvider
22
     */
23
    private $types;
24
25
    /**
26
     * @var string
27
     */
28
    private $name;
29
30
    /**
31
     * @var string|null
32
     */
33
    private $alias;
34
35
    /**
36
     * Table constructor.
37
     *
38
     * @param Driver       $driver
39
     * @param TypeProvider $types
40
     * @param string       $name
41
     * @param string|null  $alias
42
     */
43 1
    public function __construct(Driver $driver, TypeProvider $types, $name, $alias)
44
    {
45 1
        $this->driver = $driver;
46 1
        $this->types = $types;
47 1
        $this->name = $name;
48 1
        $this->alias = $alias;
49 1
    }
50
51
    /**
52
     * @return string
53
     */
54 1
    public function getName()
55
    {
56 1
        return $this->name;
57
    }
58
59
    /**
60
     * @return string|null
61
     */
62 1
    public function getAlias()
63
    {
64 1
        return $this->alias;
65
    }
66
67
68
    /**
69
     * @return string table expression (e.g. "{table} AS {alias}" for use in the FROM clause of an SQL statement)
70
     */
71 1
    public function getNode()
72
    {
73 1
        $alias = $this->getAlias();
74
75 1
        return $alias
76 1
            ? $this->driver->quoteName($this->getName()) . ' AS ' . $this->driver->quoteName($alias)
77 1
            : $this->driver->quoteName($this->getName());
78
    }
79
80
    /**
81
     * @param string|null $prefix optional Column Alias prefix
82
     *
83
     * @return Column[] list of all available Columns
84
     */
85 1
    public function listColumns($prefix = null)
86
    {
87
        // create a whitelist of parent types, excluding the Table class itself:
88
89 1
        $type = get_class($this);
90
91 1
        $whitelist = [];
92
93 1
        while ($type && $type !== self::class) {
94 1
            $whitelist[$type] = true;
95
96 1
            $type = get_parent_class($type);
97
        }
98
99
        // reflect all available public methods:
100
101 1
        $reflection = new ReflectionClass($this);
102
103 1
        $methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC);
104
105 1
        $columns = [];
106
107 1
        foreach ($methods as $method) {
108 1
            if (isset($whitelist[$method->class]) && !$method->isStatic()) {
109 1
                $alias = $prefix
110 1
                    ? "{$prefix}_{$method->name}"
111 1
                    : null;
112
113 1
                $columns[] =  $method->invoke($this, $alias);
114
            }
115
        }
116
117 1
        return $columns;
118
    }
119
120
    /**
121
     * @param string      $name
122
     * @param string      $type Type class-name
123
     * @param string|null $alias
124
     * @param mixed       $default
125
     *
126
     * @return Column
127
     */
128 1
    protected function requiredColumn($name, $type, $alias = null, $default = null)
129
    {
130 1
        return new Column($this->driver, $this, $name, $this->types->getType($type), $alias, true, $default, false);
131
    }
132
133
    /**
134
     * @param string      $name
135
     * @param string      $type Type class-name
136
     * @param string|null $alias
137
     * @param mixed       $default
138
     *
139
     * @return Column
140
     */
141
    protected function optionalColumn($name, $type, $alias = null, $default = null)
142
    {
143
        return new Column($this->driver, $this, $name, $this->types->getType($type), $alias, false, $default, false);
144
    }
145
146
    /**
147
     * @param string      $name
148
     * @param string      $type Type class-name
149
     * @param string|null $alias
150
     *
151
     * @return Column
152
     */
153 1
    protected function autoColumn($name, $type, $alias = null)
154
    {
155 1
        return new Column($this->driver, $this, $name, $this->types->getType($type), $alias, false, null, true);
156
    }
157
158
    /**
159
     * @ignore
160
     *
161
     * @return string
162
     */
163 1
    public function __toString()
164
    {
165 1
        return $this->driver->quoteName($this->alias ?: $this->name);
166
    }
167
168
    /**
169
     * @ignore
170
     *
171
     * @param string $name
172
     *
173
     * @return Column
174
     */
175 1
    public function __get($name)
176
    {
177
        // TODO caching
178
179 1
        return $this->$name($this->alias ? "{$this->alias}_{$name}" : null);
180
    }
181
}
182