-
Notifications
You must be signed in to change notification settings - Fork 170
/
Hashable.php
119 lines (105 loc) · 3.18 KB
/
Hashable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php namespace October\Rain\Database\Traits;
use Hash;
use Exception;
trait Hashable
{
/**
* @var array List of attribute names which should be hashed using the Bcrypt hashing algorithm.
*
* protected $hashable = [];
*/
/**
* @var array List of original attribute values before they were hashed.
*/
protected $originalHashableValues = [];
/**
* Boot the hashable trait for a model.
* @return void
*/
public static function bootHashable()
{
if (!property_exists(get_called_class(), 'hashable')) {
throw new Exception(sprintf(
'You must define a $hashable property in %s to use the Hashable trait.', get_called_class()
));
}
/*
* Hash required fields when necessary
*/
static::extend(function($model){
$model->bindEvent('model.beforeSetAttribute', function($key, $value) use ($model) {
$hashable = $model->getHashableAttributes();
if (in_array($key, $hashable) && !empty($value)) {
return $model->makeHashValue($key, $value);
}
});
});
}
/**
* Adds an attribute to the hashable attributes list
* @param array|string|null $attributes
* @return $this
*/
public function addHashable($attributes = null)
{
$attributes = is_array($attributes) ? $attributes : func_get_args();
$this->hashable = array_merge($this->hashable, $attributes);
return $this;
}
/**
* Hashes an attribute value and saves it in the original locker.
* @param string $key Attribute
* @param string $value Value to hash
* @return string Hashed value
*/
public function makeHashValue($key, $value)
{
$this->originalHashableValues[$key] = $value;
return Hash::make($value);
}
/**
* Checks if the supplied plain value matches the stored hash value.
* @param string $key Attribute to check
* @param string $value Value to check
* @return bool
*/
public function checkHashValue($key, $value)
{
return Hash::check($value, $this->{$key});
}
/**
* Returns a collection of fields that will be hashed.
* @return array
*/
public function getHashableAttributes()
{
return $this->hashable;
}
/**
* Returns the original values of any hashed attributes.
* @return array
*/
public function getOriginalHashValues()
{
return $this->originalHashableValues;
}
/**
* Returns the original values of any hashed attributes.
* @return mixed
*/
public function getOriginalHashValue($attribute)
{
return isset($this->originalHashableValues[$attribute])
? $this->originalHashableValues[$attribute]
: null;
}
/**
* @deprecated use self::addHashable()
* Remove this method if year >= 2018
*/
public function addHashableAttribute($attribute)
{
traceLog('The addHashableAttribute() method is deprecated, use addHashable() instead.');
return $this->addHashable($attribute);
}
}