1 | <?php |
||||||
2 | |||||||
3 | declare(strict_types=1); |
||||||
4 | |||||||
5 | namespace LaravelFreelancerNL\Aranguent\Eloquent\Concerns; |
||||||
6 | |||||||
7 | use Illuminate\Database\Eloquent\Builder as IlluminateEloquentBuilder; |
||||||
8 | use Illuminate\Support\Str; |
||||||
9 | use LaravelFreelancerNL\Aranguent\Connection; |
||||||
10 | use LaravelFreelancerNL\Aranguent\Eloquent\Builder; |
||||||
11 | use LaravelFreelancerNL\Aranguent\Query\Builder as QueryBuilder; |
||||||
12 | |||||||
13 | trait IsAranguentModel |
||||||
14 | { |
||||||
15 | use HasAttributes; |
||||||
16 | use HasAranguentRelationships; |
||||||
17 | |||||||
18 | /** |
||||||
19 | * Insert the given attributes and set the ID on the model. |
||||||
20 | * |
||||||
21 | * @param array<mixed> $attributes |
||||||
22 | * @return void |
||||||
23 | */ |
||||||
24 | 31 | protected function insertAndSetId(IlluminateEloquentBuilder $query, $attributes) |
|||||
25 | { |
||||||
26 | assert($query instanceof Builder); |
||||||
27 | |||||||
28 | 31 | $keyName = $this->getKeyName(); |
|||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
29 | |||||||
30 | 31 | $id = (string) $query->insertGetId($attributes, $keyName); |
|||||
0 ignored issues
–
show
The method
insertGetId() does not exist on LaravelFreelancerNL\Aranguent\Eloquent\Builder . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
31 | |||||||
32 | 31 | $this->setAttribute($keyName, $id); |
|||||
0 ignored issues
–
show
The method
setAttribute() does not exist on LaravelFreelancerNL\Aran...ncerns\IsAranguentModel . Did you maybe mean setKeyAttribute() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
33 | 31 | if ($keyName === '_id') { |
|||||
34 | $matches = []; |
||||||
35 | preg_match('/\/(.*)$/', $id, $matches); |
||||||
36 | |||||||
37 | // We know the exact string format for $matches when the attribute is _id |
||||||
38 | /** @var array{0: string, 1: string} $matches */ |
||||||
39 | $this->setAttribute('id', $matches[1]); // @phpstan-ignore arrayUnpacking.stringOffset |
||||||
40 | } |
||||||
41 | 31 | if ($keyName === 'id' || $keyName === '_key') { |
|||||
42 | 31 | $this->updateIdWithKey($id); |
|||||
43 | } |
||||||
44 | } |
||||||
45 | |||||||
46 | /** |
||||||
47 | * @override |
||||||
48 | * Create a new Eloquent query builder for the model. |
||||||
49 | * |
||||||
50 | * @param QueryBuilder $query |
||||||
51 | * @return Builder |
||||||
52 | */ |
||||||
53 | 192 | public function newEloquentBuilder($query) |
|||||
54 | { |
||||||
55 | 192 | return new Builder($query); |
|||||
56 | } |
||||||
57 | |||||||
58 | /** |
||||||
59 | * Get a new query builder instance for the connection. |
||||||
60 | * |
||||||
61 | * @return \Illuminate\Database\Query\Builder |
||||||
62 | */ |
||||||
63 | 192 | protected function newBaseQueryBuilder() |
|||||
64 | { |
||||||
65 | 192 | return $this->getConnection()->query(); |
|||||
66 | } |
||||||
67 | |||||||
68 | /** |
||||||
69 | * Dynamically set attributes on the model. |
||||||
70 | * |
||||||
71 | * @param string $key |
||||||
72 | * @param mixed $value |
||||||
73 | * @return void |
||||||
74 | */ |
||||||
75 | 41 | public function __set($key, $value) |
|||||
76 | { |
||||||
77 | // Laravel's mutators don't differentiate between id and _id, so we catch ArangoDB's _id here. |
||||||
78 | 41 | if ($key === 'id') { |
|||||
79 | 1 | $this->updateIdWithKey($value); |
|||||
80 | } |
||||||
81 | |||||||
82 | 41 | if ($key === '_id') { |
|||||
83 | 1 | $this->attributes['id'] = explode('/', $value)[1]; |
|||||
0 ignored issues
–
show
|
|||||||
84 | } |
||||||
85 | |||||||
86 | 41 | $this->setAttribute($key, $value); |
|||||
87 | } |
||||||
88 | |||||||
89 | /** |
||||||
90 | * Map the id attribute commonly used in Laravel to the primary key for third-party compatibility. |
||||||
91 | * In ArangoDB '_key' is the equivalent of 'id' in sql databases. |
||||||
92 | * |
||||||
93 | * @param string $value |
||||||
94 | * @return void |
||||||
95 | */ |
||||||
96 | public function setKeyAttribute($value) |
||||||
97 | { |
||||||
98 | $this->attributes['_key'] = $value; |
||||||
0 ignored issues
–
show
|
|||||||
99 | |||||||
100 | $this->updateIdWithKey($value); |
||||||
101 | } |
||||||
102 | |||||||
103 | 32 | protected function updateIdWithKey(string $key): void |
|||||
104 | { |
||||||
105 | 32 | $this->attributes['_id'] = $this->getTable() . '/' . $key; |
|||||
0 ignored issues
–
show
It seems like
getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
106 | } |
||||||
107 | |||||||
108 | /** |
||||||
109 | * Qualify the given column name by the model's table. |
||||||
110 | * |
||||||
111 | * @param string $column |
||||||
112 | * @return string |
||||||
113 | */ |
||||||
114 | 151 | public function qualifyColumn($column) |
|||||
115 | { |
||||||
116 | 151 | $tableReferer = Str::singular($this->getTable()) . 'Doc'; |
|||||
117 | |||||||
118 | 151 | if (Str::startsWith($column, $tableReferer . '.')) { |
|||||
119 | return $column; |
||||||
120 | } |
||||||
121 | |||||||
122 | 151 | return $tableReferer . '.' . $column; |
|||||
123 | } |
||||||
124 | |||||||
125 | /** |
||||||
126 | * Get the default foreign key name for the model. |
||||||
127 | * |
||||||
128 | * @return string |
||||||
129 | */ |
||||||
130 | 13 | public function getForeignKey() |
|||||
131 | { |
||||||
132 | 13 | $keyName = $this->getKeyName(); |
|||||
133 | |||||||
134 | 13 | if ($keyName[0] != '_') { |
|||||
135 | 13 | $keyName = '_' . $keyName; |
|||||
136 | } |
||||||
137 | |||||||
138 | 13 | return Str::snake(class_basename($this)) . $keyName; |
|||||
139 | } |
||||||
140 | |||||||
141 | /** |
||||||
142 | * Get the database connection for the model. |
||||||
143 | * |
||||||
144 | * @return Connection |
||||||
145 | */ |
||||||
146 | 192 | public function getConnection() |
|||||
147 | { |
||||||
148 | 192 | $connection = static::resolveConnection($this->getConnectionName()); |
|||||
0 ignored issues
–
show
The method
getConnectionName() does not exist on LaravelFreelancerNL\Aran...ncerns\IsAranguentModel . Did you maybe mean getConnection() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
149 | |||||||
150 | assert($connection instanceof Connection); |
||||||
151 | |||||||
152 | 192 | return $connection; |
|||||
153 | } |
||||||
154 | } |
||||||
155 |