-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathCast.php
More file actions
77 lines (61 loc) · 1.69 KB
/
Copy pathCast.php
File metadata and controls
77 lines (61 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace ProcessMaker\Query;
use Illuminate\Support\Facades\DB;
use ProcessMaker\Query\SyntaxError;
class Cast extends BaseField
{
protected $field;
protected $type;
protected $types = [
'number' => [
'mysql' => 'decimal',
'sqlite' => 'integer',
],
'text' => [
'mysql' => 'char',
'sqlite' => 'text',
],
];
public function __construct($field, $type)
{
$this->field = $field;
$this->type = $this->mapType($type);
}
public function toArray()
{
return [
'Cast' => [
'field' => $this->field->toArray(),
'type' => $this->type,
],
];
}
private function getConnectionDriver()
{
$config = DB::connection()->getConfig();
return $config['driver'];
}
private function getSupportedTypes()
{
$list = [];
foreach ($this->types as $key => $value) {
$list[] = $key;
}
return $list;
}
private function mapType($type)
{
$driver = $this->getConnectionDriver();
if (array_key_exists($type, $this->types)) {
if (array_key_exists($driver, $this->types[$type])) {
return $this->types[$type][$driver];
}
}
$types = implode(', ', $this->getSupportedTypes());
throw new SyntaxError("Unsupported cast type. Casts must be of type: {$types}.", $this->getSupportedTypes(), $type, 0, 0, 0);
}
public function toEloquent($connection = null)
{
return DB::raw('CAST(' . $this->field->toEloquent($connection) . ' AS ' . $this->type . ')');
}
}