1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFreelancerNL\Aranguent\Eloquent\Concerns; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use LaravelFreelancerNL\Aranguent\Eloquent\Builder; |
10
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Builder as QueryBuilder; |
11
|
|
|
use LaravelFreelancerNL\FluentAQL\QueryBuilder as ArangoQueryBuilder; |
12
|
|
|
|
13
|
|
|
trait IsAranguentModel |
14
|
|
|
{ |
15
|
|
|
use HasAranguentRelationships; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Get the route key for the model. |
19
|
|
|
* |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
|
|
public function getRouteKeyName(): string |
23
|
|
|
{ |
24
|
|
|
return '_key'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Insert the given attributes and set the ID on the model. |
29
|
|
|
* |
30
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
31
|
|
|
* @param array $attributes |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
7 |
|
protected function insertAndSetId(\Illuminate\Database\Eloquent\Builder $query, $attributes) |
35
|
|
|
{ |
36
|
7 |
|
$id = $query->insertGetId($attributes, $keyName = $this->getKeyName()); |
|
|
|
|
37
|
|
|
|
38
|
7 |
|
$this->setAttribute($keyName, $id); |
|
|
|
|
39
|
7 |
|
if ($keyName === '_id') { |
40
|
7 |
|
$matches = []; |
41
|
7 |
|
preg_match('/\/(.*)$/', $id, $matches); |
|
|
|
|
42
|
|
|
|
43
|
7 |
|
$this->setAttribute('_key', $matches[1]); |
44
|
|
|
} |
45
|
7 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @override |
49
|
|
|
* Create a new Eloquent query builder for the model. |
50
|
|
|
* |
51
|
|
|
* @param QueryBuilder $query |
52
|
|
|
* |
53
|
|
|
* @return Builder |
54
|
|
|
*/ |
55
|
103 |
|
public function newEloquentBuilder($query) |
56
|
|
|
{ |
57
|
103 |
|
return new Builder($query); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get a new query builder instance for the connection. |
62
|
|
|
* |
63
|
|
|
* @return \Illuminate\Database\Query\Builder |
64
|
|
|
*/ |
65
|
103 |
|
protected function newBaseQueryBuilder() |
66
|
|
|
{ |
67
|
103 |
|
return $this->getConnection()->query(); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Dynamically retrieve attributes on the model. |
72
|
|
|
* |
73
|
|
|
* @param string $key |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
46 |
|
public function __get($key) |
77
|
|
|
{ |
78
|
|
|
// Laravel's accessors don't differentiate between id and _id, so we catch ArangoDB's _id here. |
79
|
46 |
|
if ($key === 'id') { |
80
|
2 |
|
return $this->attributes['_id']; |
81
|
|
|
} |
82
|
45 |
|
return $this->getAttribute($key); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Dynamically set attributes on the model. |
87
|
|
|
* |
88
|
|
|
* @param string $key |
89
|
|
|
* @param mixed $value |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
17 |
|
public function __set($key, $value) |
93
|
|
|
{ |
94
|
|
|
// Laravel's mutators don't differentiate between id and _id, so we catch ArangoDB's _id here. |
95
|
17 |
|
if ($key === 'id') { |
96
|
1 |
|
$this->attributes['_id'] = $value; |
|
|
|
|
97
|
1 |
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
16 |
|
$this->setAttribute($key, $value); |
101
|
16 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Map the id attribute commonly used in Laravel to the primary key for third-party compatibility. |
105
|
|
|
* In ArangoDB '_key' is the equivalent of 'id' in sql databases. |
106
|
|
|
* |
107
|
|
|
* @param string $value |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
8 |
|
public function setKeyAttribute($value) |
111
|
|
|
{ |
112
|
8 |
|
$this->attributes['_key'] = $value; |
|
|
|
|
113
|
|
|
|
114
|
8 |
|
$this->updateIdWithKey($value); |
115
|
8 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $key |
119
|
|
|
*/ |
120
|
8 |
|
protected function updateIdWithKey(string $key) |
121
|
|
|
{ |
122
|
8 |
|
if (! isset($this->attributes['_id'])) { |
123
|
7 |
|
return; |
124
|
|
|
} |
125
|
|
|
|
126
|
8 |
|
$id = preg_replace("/[a-zA-Z0-9_-]+\/\K.+/i", $key, $this->attributes['_id']); |
127
|
8 |
|
$this->attributes['_id'] = $id; |
|
|
|
|
128
|
8 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Qualify the given column name by the model's table. |
132
|
|
|
* |
133
|
|
|
* @param string $column |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
59 |
|
public function qualifyColumn($column) |
138
|
|
|
{ |
139
|
59 |
|
$tableReferer = Str::singular($this->getTable()) . 'Doc'; |
|
|
|
|
140
|
|
|
|
141
|
59 |
|
if (Str::startsWith($column, $tableReferer . '.')) { |
142
|
|
|
return $column; |
143
|
|
|
} |
144
|
|
|
|
145
|
59 |
|
return $tableReferer . '.' . $column; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the default foreign key name for the model. |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
8 |
|
public function getForeignKey() |
154
|
|
|
{ |
155
|
8 |
|
$keyName = $this->getKeyName(); |
156
|
|
|
|
157
|
8 |
|
if ($keyName[0] != '_') { |
158
|
|
|
$keyName = '_' . $keyName; |
159
|
|
|
} |
160
|
|
|
|
161
|
8 |
|
return Str::snake(class_basename($this)) . $keyName; |
162
|
|
|
} |
163
|
|
|
|
164
|
2 |
|
protected function fromAqb(ArangoQueryBuilder|Closure $aqb): Collection |
165
|
|
|
{ |
166
|
2 |
|
if ($aqb instanceof Closure) { |
167
|
|
|
/** @phpstan-ignore-next-line */ |
168
|
1 |
|
$aqb = $aqb(DB::aqb()); |
169
|
|
|
} |
170
|
2 |
|
$connection = $this->getConnection(); |
171
|
2 |
|
$results = $connection->execute($aqb->get()); |
172
|
2 |
|
return $this->hydrate($results); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|