Completed
Push — master ( cd25ec...a129a9 )
by Rasmus
02:17
created

Table::getNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
namespace mindplay\sql\model;
4
5
use mindplay\sql\framework\Driver;
6
use mindplay\sql\framework\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
     * @return Column[] list of all available Columns
82
     */
83 1
    public function listColumns()
84
    {
85
        // create a whitelist of parent types, excluding the Table class itself:
86
87 1
        $type = get_class($this);
88
89 1
        $whitelist = [];
90
91 1
        while ($type && $type !== self::class) {
92 1
            $whitelist[$type] = true;
93
94 1
            $type = get_parent_class($type);
95
        }
96
97
        // reflect all available public methods:
98
99 1
        $reflection = new ReflectionClass($this);
100
101 1
        $methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC);
102
103 1
        $columns = [];
104
105 1
        foreach ($methods as $method) {
106 1
            if (isset($whitelist[$method->class]) && !$method->isStatic()) {
107 1
                $columns[] = $this->__get($method->name);
108
            }
109
        }
110
111 1
        return $columns;
112
    }
113
114
    /**
115
     * @param string      $name
116
     * @param string      $type Type class-name
117
     * @param string|null $alias
118
     * @param mixed       $default
119
     *
120
     * @return Column
121
     */
122 1
    protected function requiredColumn($name, $type, $alias = null, $default = null)
123
    {
124 1
        return new Column($this->driver, $this, $name, $this->types->getType($type), $alias, true, $default, false);
125
    }
126
127
    /**
128
     * @param string      $name
129
     * @param string      $type Type class-name
130
     * @param string|null $alias
131
     * @param mixed       $default
132
     *
133
     * @return Column
134
     */
135
    protected function optionalColumn($name, $type, $alias = null, $default = null)
136
    {
137
        return new Column($this->driver, $this, $name, $this->types->getType($type), $alias, false, $default, false);
138
    }
139
140
    /**
141
     * @param string      $name
142
     * @param string      $type Type class-name
143
     * @param string|null $alias
144
     *
145
     * @return Column
146
     */
147 1
    protected function autoColumn($name, $type, $alias = null)
148
    {
149 1
        return new Column($this->driver, $this, $name, $this->types->getType($type), $alias, false, null, true);
150
    }
151
152
    /**
153
     * @ignore
154
     *
155
     * @return string
156
     */
157 1
    public function __toString()
158
    {
159 1
        return $this->driver->quoteName($this->alias ?: $this->name);
160
    }
161
162
    /**
163
     * @ignore
164
     *
165
     * @param string $name
166
     *
167
     * @return Column
168
     */
169 1
    public function __get($name)
170
    {
171
        // TODO caching
172
173 1
        return $this->$name($this->alias ? "{$this->alias}_{$name}" : null);
174
    }
175
}
176