Skip to content

Commit a157287

Browse files
committed
Handle column type methods using __call magic method
1 parent e14ddb0 commit a157287

File tree

1 file changed

+21
-194
lines changed

1 file changed

+21
-194
lines changed

src/Bosnadev/Database/Schema/Grammars/PostgresGrammar.php

Lines changed: 21 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@
88

99
/**
1010
* Class PostgresGrammar
11+
*
12+
* Available Column Type supported dynamically:
13+
* hstore, uuid, jsonb, point, line,
14+
* path, box, polygon, circle,
15+
* money, int4range, int8range, numrange,
16+
* tsrange, tstzrange, daterange, tsvector
17+
*
1118
* @package Bosnadev\Database\Schema\Grammars
1219
*/
1320
class PostgresGrammar extends \Illuminate\Database\Schema\Grammars\PostgresGrammar
1421
{
1522
/**
1623
* Check if the type is uuid, use internal guid
17-
*
24+
*
1825
* @param string $type
1926
* @return \Doctrine\DBAL\Types\Type
2027
*/
@@ -28,47 +35,30 @@ protected function getDoctrineColumnType($type)
2835
}
2936

3037
/**
31-
* Create the column definition for a character type.
38+
* Handle dynamic method calls.
3239
*
33-
* @param Fluent $column
34-
* @return string
40+
* @param string $method
41+
* @param array $parameters
42+
* @return mixed
3543
*/
36-
protected function typeCharacter(Fluent $column)
44+
public function __call($method, $parameters)
3745
{
38-
return "character({$column->length})";
39-
}
46+
if(substr($method, 0, 4) === 'type') {
47+
$type = substr($method, 4, strlen($method) - 4);
4048

41-
/**
42-
* Create the column definition for a hstore type.
43-
*
44-
* @param Fluent $column
45-
* @return string
46-
*/
47-
protected function typeHstore(Fluent $column)
48-
{
49-
return "hstore";
50-
}
51-
52-
/**
53-
* Create the column definition for a uuid type.
54-
*
55-
* @param Fluent $column
56-
* @return string
57-
*/
58-
protected function typeUuid(Fluent $column)
59-
{
60-
return "uuid";
49+
return lcfirst($type);
50+
}
6151
}
6252

6353
/**
64-
* Create the column definition for a jsonb type.
54+
* Create the column definition for a character type.
6555
*
6656
* @param Fluent $column
6757
* @return string
6858
*/
69-
protected function typeJsonb(Fluent $column)
59+
protected function typeCharacter(Fluent $column)
7060
{
71-
return "jsonb";
61+
return "character({$column->length})";
7262
}
7363

7464
/**
@@ -81,6 +71,7 @@ protected function typeIpAddress(Fluent $column)
8171
{
8272
return 'inet';
8373
}
74+
8475
/**
8576
* Create the column definition for a CIDR notation-style netmask.
8677
*
@@ -103,28 +94,6 @@ protected function typeMacAddress(Fluent $column)
10394
return 'macaddr';
10495
}
10596

106-
/**
107-
* Create the column definition for a 2D geometric point (x, y), x and y are floating-point numbers.
108-
*
109-
* @param \Illuminate\Support\Fluent $column
110-
* @return string
111-
*/
112-
protected function typePoint(Fluent $column)
113-
{
114-
return 'point';
115-
}
116-
117-
/**
118-
* Create the column definition for a line represented as a standard linear equation Ax + By + C = 0.
119-
*
120-
* @param \Illuminate\Support\Fluent $column
121-
* @return string
122-
*/
123-
protected function typeLine(Fluent $column)
124-
{
125-
return 'line';
126-
}
127-
12897
/**
12998
* Create the column definition for a line segment represented by two points (x1, y1), (x2, y2).
13099
*
@@ -136,148 +105,6 @@ protected function typeLineSegment(Fluent $column)
136105
return 'lseg';
137106
}
138107

139-
/**
140-
* Create the column definition for a path represented as a list of points (x1, y1), (x2, y2), ..., (xn, yn).
141-
*
142-
* @param \Illuminate\Support\Fluent $column
143-
* @return string
144-
*/
145-
protected function typePath(Fluent $column)
146-
{
147-
return 'path';
148-
}
149-
150-
/**
151-
* Create the column definition for a box represented by opposite corners of the box as points (x1, y1), (x2, y2).
152-
*
153-
* @param \Illuminate\Support\Fluent $column
154-
* @return string
155-
*/
156-
protected function typeBox(Fluent $column)
157-
{
158-
return 'box';
159-
}
160-
161-
/**
162-
* Create the column definition for a polygon represented by a list of points (vertices of the polygon).
163-
*
164-
* @param \Illuminate\Support\Fluent $column
165-
* @return string
166-
*/
167-
protected function typePolygon(Fluent $column)
168-
{
169-
return 'polygon';
170-
}
171-
172-
/**
173-
* Create the column definition for a circle represented by a center point and a radius <(x, y), r>
174-
*
175-
* @param \Illuminate\Support\Fluent $column
176-
* @return string
177-
*/
178-
protected function typeCircle(Fluent $column)
179-
{
180-
return 'circle';
181-
}
182-
183-
/**
184-
* Create the column definition for storing amounts of currency with a fixed fractional precision.
185-
*
186-
* This will store values in the range of: -92233720368547758.08 to +92233720368547758.07. (92 quadrillion).
187-
* Output is locale-sensitive, see lc_monetary setting of PostgreSQL instance/s.
188-
*
189-
* @param \Illuminate\Support\Fluent $column
190-
* @return string
191-
*/
192-
protected function typeMoney(Fluent $column)
193-
{
194-
return 'money';
195-
}
196-
197-
/**
198-
* Create the column definition for an int4range type.
199-
*
200-
* @param Fluent $column
201-
*
202-
* @return string
203-
*/
204-
protected function typeInt4range(Fluent $column)
205-
{
206-
return "int4range";
207-
}
208-
209-
/**
210-
* Create the column definition for an int8range type.
211-
*
212-
* @param Fluent $column
213-
*
214-
* @return string
215-
*/
216-
protected function typeInt8range(Fluent $column)
217-
{
218-
return "int8range";
219-
}
220-
221-
/**
222-
* Create the column definition for an numrange type.
223-
*
224-
* @param Fluent $column
225-
*
226-
* @return string
227-
*/
228-
protected function typeNumrange(Fluent $column)
229-
{
230-
return "numrange";
231-
}
232-
233-
/**
234-
* Create the column definition for an tsrange type.
235-
*
236-
* @param Fluent $column
237-
*
238-
* @return string
239-
*/
240-
protected function typeTsrange(Fluent $column)
241-
{
242-
return "tsrange";
243-
}
244-
245-
/**
246-
* Create the column definition for an tstzrange type.
247-
*
248-
* @param Fluent $column
249-
*
250-
* @return string
251-
*/
252-
protected function typeTstzrange(Fluent $column)
253-
{
254-
return "tstzrange";
255-
}
256-
257-
/**
258-
* Create the column definition for an daterange type.
259-
*
260-
* @param Fluent $column
261-
*
262-
* @return string
263-
*/
264-
protected function typeDaterange(Fluent $column)
265-
{
266-
return "daterange";
267-
}
268-
269-
/**
270-
* Create the column definition for a Text Search Vector type.
271-
*
272-
* @param Fluent $column
273-
*
274-
* @return string
275-
*/
276-
protected function typeTsvector(Fluent $column)
277-
{
278-
return "tsvector";
279-
}
280-
281108
/**
282109
* @param mixed $value
283110
* @return mixed|string

0 commit comments

Comments
 (0)