|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cog\Laravel\Optimus\Traits; |
|
4
|
|
|
|
|
5
|
|
|
trait OptimusEncodedRouteKey |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Get the value of the model's route key. |
|
9
|
|
|
* |
|
10
|
|
|
* @return mixed |
|
11
|
|
|
*/ |
|
12
|
|
|
public function getRouteKey() |
|
13
|
|
|
{ |
|
14
|
|
|
$id = parent::getRouteKey(); |
|
15
|
|
|
|
|
16
|
|
|
return $this->getOptimus()->encode($id); |
|
|
|
|
|
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Retrieve the model for a bound value. |
|
21
|
|
|
* |
|
22
|
|
|
* @param mixed $value |
|
23
|
|
|
* @param string|null $field |
|
24
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
|
25
|
|
|
*/ |
|
26
|
|
|
public function resolveRouteBinding($value, $field = null) |
|
27
|
|
|
{ |
|
28
|
|
|
if ($field === null) { |
|
29
|
|
|
$field = $this->getRouteKeyName(); |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if (is_string($value) && ctype_digit($value)) { |
|
33
|
|
|
$value = (int) $value; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
if (is_int($value) && $field === $this->getRouteKeyName()) { |
|
37
|
|
|
$value = $this->getOptimus()->decode($value); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $this->where($field, $value)->first(); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Retrieve the model for a bound value. |
|
45
|
|
|
* |
|
46
|
|
|
* @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation $query |
|
47
|
|
|
* @param mixed $value |
|
48
|
|
|
* @param string|null $field |
|
49
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\Relation |
|
50
|
|
|
*/ |
|
51
|
|
|
public function resolveRouteBindingQuery($query, $value, $field = null) |
|
52
|
|
|
{ |
|
53
|
|
|
if ($field === null) { |
|
54
|
|
|
$field = $query->getRouteKeyName(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (is_string($value) && ctype_digit($value)) { |
|
58
|
|
|
$value = (int) $value; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (is_int($value) && $field === $this->getRouteKeyName()) { |
|
62
|
|
|
$value = $this->getOptimus()->decode($value); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $query->where($field, $value); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get the Optimus instance. |
|
70
|
|
|
* |
|
71
|
|
|
* @return \Cog\Laravel\Optimus\OptimusManager |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function getOptimus() |
|
74
|
|
|
{ |
|
75
|
|
|
$connection = null; |
|
76
|
|
|
|
|
77
|
|
|
if (property_exists($this, 'optimusConnection')) { |
|
78
|
|
|
$connection = $this->optimusConnection; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return app('optimus')->connection($connection); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|