-
Notifications
You must be signed in to change notification settings - Fork 4
/
AbstractDriver.php
136 lines (115 loc) · 3.84 KB
/
AbstractDriver.php
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace Pulsar\Driver;
use BackedEnum;
use DateTimeInterface;
use JAQB\Query\SelectQuery;
use Pulsar\Model;
use Pulsar\Property;
use Pulsar\Query;
use Pulsar\Relation\Pivot;
use Pulsar\Type;
use UnitEnum;
abstract class AbstractDriver implements DriverInterface
{
/**
* Marshals a value to storage.
*/
public function serializeValue(mixed $value, ?Property $property): mixed
{
// encode backed enums as their backing type
if ($value instanceof BackedEnum) {
return $value->value;
}
// encode datetime objects
if ($value instanceof DateTimeInterface) {
$format = $property?->date_format;
if (!$format) {
$format = $property?->type == Type::DATE ? 'Y-m-d' : 'Y-m-d H:i:s';
}
return $value->format($format);
}
// encode arrays/objects as JSON
if (is_array($value) || is_object($value)) {
return json_encode($value);
}
return $value;
}
/**
* Serializes an array of values.
*/
protected function serialize(array $values, Model $model): array
{
foreach ($values as $k => &$value) {
$value = $this->serializeValue($value, $model::definition()->get($k));
}
return $values;
}
/**
* Returns a prefixed select statement.
*/
protected function prefixSelect(string $columns, string $tablename): string
{
$prefixed = [];
foreach (explode(',', $columns) as $column) {
$prefixed[] = $this->prefixColumn($column, $tablename);
}
return implode(',', $prefixed);
}
/**
* Returns a prefixed where statement.
*/
protected function prefixWhere(array $where, string $tablename): array
{
$return = [];
foreach ($where as $key => $condition) {
// handles $where[property] = value
if (!is_numeric($key)) {
$return[$this->prefixColumn($key, $tablename)] = $condition;
// handles $where[] = [property, value, '=']
} elseif (is_array($condition)) {
$condition[0] = $this->prefixColumn($condition[0], $tablename);
$return[] = $condition;
// handles raw SQL - do nothing
} else {
$return[] = $condition;
}
}
return $return;
}
/**
* Returns a prefixed sort statement.
*/
protected function prefixSort(array $sort, string $tablename): array
{
foreach ($sort as &$condition) {
$condition[0] = $this->prefixColumn($condition[0], $tablename);
}
return $sort;
}
/**
* Prefix columns with tablename that contains only
* alphanumeric/underscores/*.
*/
protected function prefixColumn(string $column, string $tablename): string
{
if ('*' === $column || preg_match('/^[a-z0-9_]+$/i', $column)) {
return "$tablename.$column";
}
return $column;
}
/**
* Adds join conditions to a select query.
*/
protected function addJoins(Query $query, string $tablename, SelectQuery $dbQuery): void
{
foreach ($query->getJoins() as $join) {
[$foreignModelClass, $column, $foreignKey, $type] = $join;
$foreignModel = $foreignModelClass instanceof Pivot ? $foreignModelClass : new $foreignModelClass();
$foreignTablename = $foreignModel->getTablename();
$condition = $foreignModelClass instanceof Pivot
? $this->prefixColumn($column, $foreignTablename).'='.$this->prefixColumn($foreignKey, $tablename)
: $this->prefixColumn($column, $tablename).'='.$this->prefixColumn($foreignKey, $foreignTablename);
$dbQuery->join($foreignTablename, $condition, null, $type);
}
}
}