|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Rinvex\Support\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use Vinkla\Hashids\Facades\Hashids; |
|
8
|
|
|
|
|
9
|
|
|
trait HashidsTrait |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Get the value of the model's route key. |
|
13
|
|
|
* |
|
14
|
|
|
* @throws \Exception |
|
15
|
|
|
* |
|
16
|
|
|
* @return mixed |
|
17
|
|
|
*/ |
|
18
|
|
|
public function getRouteKey() |
|
19
|
|
|
{ |
|
20
|
|
|
$accessarea = app()->bound('request.accessarea') ? app('request.accessarea') : null; |
|
21
|
|
|
|
|
22
|
|
|
$obscure = property_exists($this, 'obscure') && is_array($this->obscure) ? $this->obscure : config('cortex.foundation.obscure'); |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
return in_array($accessarea, $obscure['areas']) |
|
25
|
|
|
? Hashids::encode($this->getAttribute($this->getKeyName()), $obscure['rotate'] ? random_int(1, 999) : 1) |
|
|
|
|
|
|
26
|
|
|
: $this->getAttribute($this->getRouteKeyName()); |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Retrieve the model for a bound value. |
|
31
|
|
|
* |
|
32
|
|
|
* @param mixed $value |
|
33
|
|
|
* @param string|null $field |
|
34
|
|
|
* |
|
35
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
|
36
|
|
|
*/ |
|
37
|
|
|
public function resolveRouteBinding($value, $field = null) |
|
38
|
|
|
{ |
|
39
|
|
|
$accessarea = app()->bound('request.accessarea') ? app('request.accessarea') : null; |
|
40
|
|
|
$obscure = property_exists($this, 'obscure') && is_array($this->obscure) ? $this->obscure : config('cortex.foundation.obscure'); |
|
41
|
|
|
|
|
42
|
|
|
return in_array($accessarea, $obscure['areas']) |
|
43
|
|
|
? $this->where($field ?? $this->getKeyName(), optional(Hashids::decode($value))[0])->first() |
|
|
|
|
|
|
44
|
|
|
: $this->where($field ?? $this->getRouteKeyName(), $value)->first(); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Unhash given value of the model. |
|
49
|
|
|
* |
|
50
|
|
|
* @param mixed $value |
|
51
|
|
|
* |
|
52
|
|
|
* @return mixed |
|
53
|
|
|
*/ |
|
54
|
|
|
public function unhashId($value) |
|
55
|
|
|
{ |
|
56
|
|
|
$accessarea = app()->bound('request.accessarea') ? app('request.accessarea') : null; |
|
57
|
|
|
$obscure = property_exists($this, 'obscure') && is_array($this->obscure) ? $this->obscure : config('cortex.foundation.obscure'); |
|
58
|
|
|
|
|
59
|
|
|
return in_array($accessarea, $obscure['areas']) ? optional(Hashids::decode($value))[0] : $value; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: