Skip to content

Commit 4da3bf8

Browse files
committed
value starting with dash escape
1 parent 3b87744 commit 4da3bf8

File tree

2 files changed

+54
-51
lines changed

2 files changed

+54
-51
lines changed

src/DSL/QueryBuilder.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,13 @@ private static function _parseParams($key, $value): string
289289

290290
public static function _escape($value): string
291291
{
292-
$specialChars = ['"', '\\', '~', '^'];
292+
$specialChars = ['"', '\\', '~', '^', '/'];
293293
foreach ($specialChars as $char) {
294294
$value = str_replace($char, "\\".$char, $value);
295295
}
296+
if (str_starts_with($value, '-')) {
297+
$value = '\\'.$value;
298+
}
296299

297300
return $value;
298301
}

src/Eloquent/HybridRelations.php

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use PDPhilip\Elasticsearch\Relations\MorphTo;
1515
use PDPhilip\Elasticsearch\Relations\MorphOne;
1616
use PDPhilip\Elasticsearch\Eloquent\Model as ParentModel;
17-
17+
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
1818

1919
trait HybridRelations
2020
{
@@ -24,158 +24,158 @@ trait HybridRelations
2424
public function hasOne($related, $foreignKey = null, $localKey = null)
2525
{
2626
$foreignKey = $foreignKey ? : $this->getForeignKey();
27-
27+
2828
$instance = new $related;
29-
29+
3030
$localKey = $localKey ? : $this->getKeyName();
31-
31+
3232
return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey);
3333
}
34-
34+
3535
/**
3636
* @inheritDoc
3737
*/
3838
public function morphOne($related, $name, $type = null, $id = null, $localKey = null)
3939
{
4040
$instance = new $related;
41-
41+
4242
[$type, $id] = $this->getMorphs($name, $type, $id);
43-
43+
4444
$localKey = $localKey ? : $this->getKeyName();
45-
45+
4646
return new MorphOne($instance->newQuery(), $this, $type, $id, $localKey);
47-
47+
4848
}
49-
49+
5050
/**
5151
* @inheritDoc
5252
*/
5353
public function hasMany($related, $foreignKey = null, $localKey = null)
5454
{
5555
$foreignKey = $foreignKey ? : $this->getForeignKey();
56-
56+
5757
$instance = new $related;
58-
58+
5959
$localKey = $localKey ? : $this->getKeyName();
60-
60+
6161
return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey);
6262
}
63-
63+
6464
/**
6565
* @inheritDoc
6666
*/
6767
public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
6868
{
69-
69+
7070
$instance = new $related;
71-
71+
7272
[$type, $id] = $this->getMorphs($name, $type, $id);
73-
73+
7474
$table = $instance->getTable();
75-
75+
7676
$localKey = $localKey ? : $this->getKeyName();
77-
77+
7878
return new MorphMany($instance->newQuery(), $this, $type, $id, $localKey);
7979
}
80-
80+
8181
/**
8282
* @inheritDoc
8383
*/
8484
public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
8585
{
86-
86+
8787
if ($relation === null) {
8888
[$current, $caller] = debug_backtrace(false, 2);
89-
89+
9090
$relation = $caller['function'];
9191
}
92-
93-
92+
93+
9494
if ($foreignKey === null) {
9595
$foreignKey = Str::snake($relation).'_id';
9696
}
97-
97+
9898
$instance = new $related;
99-
99+
100100
$query = $instance->newQuery();
101-
101+
102102
$otherKey = $otherKey ? : $instance->getKeyName();
103-
103+
104104
return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation);
105105
}
106-
106+
107107
/**
108108
* @inheritDoc
109109
*/
110110
public function morphTo($name = null, $type = null, $id = null, $ownerKey = null)
111111
{
112112
if ($name === null) {
113113
[$current, $caller] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
114-
114+
115115
$name = Str::snake($caller['function']);
116116
}
117-
117+
118118
[$type, $id] = $this->getMorphs($name, $type, $id);
119-
119+
120120
if (($class = $this->$type) === null) {
121121
return new MorphTo(
122122
$this->newQuery(), $this, $id, $ownerKey, $type, $name
123123
);
124124
}
125-
125+
126126
$class = $this->getActualClassNameForMorph($class);
127-
127+
128128
$instance = new $class;
129-
129+
130130
$ownerKey = $ownerKey ?? $instance->getKeyName();
131-
131+
132132
return new MorphTo(
133133
$instance->newQuery(), $this, $id, $ownerKey, $type, $name
134134
);
135135
}
136-
136+
137137
/**
138138
* @inheritDoc
139139
*/
140140
public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null, $parentKey = null, $relatedKey = null, $relation = null)
141141
{
142-
142+
143143
if ($relation === null) {
144144
$relation = $this->guessBelongsToManyRelation();
145145
}
146-
147-
146+
147+
148148
if (!is_subclass_of($related, ParentModel::class)) {
149149
return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $parentKey, $relatedKey, $relation);
150150
}
151-
151+
152152
$foreignKey = $foreignKey ? : $this->getForeignKey().'s';
153153
$instance = new $related;
154154
$otherKey = $otherKey ? : $instance->getForeignKey().'s';
155-
155+
156156
if ($collection === null) {
157157
$collection = $instance->getTable();
158158
}
159-
159+
160160
$query = $instance->newQuery();
161-
161+
162162
return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $parentKey ? : $this->getKeyName(), $relatedKey ? : $instance->getKeyName(), $relation
163163
);
164164
}
165-
165+
166166
/**
167167
* @inheritDoc
168168
*/
169-
169+
170170
protected function guessBelongsToManyRelation()
171171
{
172172
if (method_exists($this, 'getBelongsToManyCaller')) {
173173
return $this->getBelongsToManyCaller();
174174
}
175-
175+
176176
return parent::guessBelongsToManyRelation();
177177
}
178-
178+
179179
/**
180180
* @inheritdoc
181181
*/
@@ -184,7 +184,7 @@ public function newEloquentBuilder($query)
184184
if (is_subclass_of($this, ParentModel::class)) {
185185
return new Builder($query);
186186
}
187-
188-
return new Builder($query);
187+
188+
return new EloquentBuilder($query);
189189
}
190190
}

0 commit comments

Comments
 (0)