-
Notifications
You must be signed in to change notification settings - Fork 25
/
Model.php
200 lines (176 loc) · 4.65 KB
/
Model.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
namespace Esensi\Model;
use Esensi\Model\Contracts\EncryptingModelInterface;
use Esensi\Model\Contracts\HashingModelInterface;
use Esensi\Model\Contracts\JugglingModelInterface;
use Esensi\Model\Contracts\PurgingModelInterface;
use Esensi\Model\Contracts\RelatingModelInterface;
use Esensi\Model\Contracts\ValidatingModelInterface;
use Esensi\Model\Traits\EncryptingModelTrait;
use Esensi\Model\Traits\HashingModelTrait;
use Esensi\Model\Traits\JugglingModelTrait;
use Esensi\Model\Traits\PurgingModelTrait;
use Esensi\Model\Traits\RelatingModelTrait;
use Esensi\Model\Traits\ValidatingModelTrait;
use Illuminate\Contracts\Support\MessageProvider;
use Illuminate\Database\Eloquent\Model as Eloquent;
/**
* Base Model.
*
*/
abstract class Model extends Eloquent implements
EncryptingModelInterface,
HashingModelInterface,
JugglingModelInterface,
MessageProvider,
PurgingModelInterface,
RelatingModelInterface,
ValidatingModelInterface
{
/*
* Make model validate attributes.
*
* This comes first even though it is out of alphabetical
* order because it is important that it is booted before
* hashing and purging traits.
*
* @see Esensi\Model\Traits\ValidatingModelTrait
*/
use ValidatingModelTrait;
/*
* Make model encrypt attributes.
*
* @see Esensi\Model\Traits\EncryptingModelTrait
*/
use EncryptingModelTrait;
/*
* Make model hash attributes.
*
* @see Esensi\Model\Traits\HashingModelTrait
*/
use HashingModelTrait;
/*
* Make the model juggle attributes when setting and getting
*
* @see Esensi\Model\Contracts\JugglingModelInterface
*/
use JugglingModelTrait;
/*
* Make model purge attributes.
*
* @see Esensi\Model\Traits\PurgingModelTrait
*/
use PurgingModelTrait;
/*
* Make model use properties for model relationships.
*
* @see Esensi\Model\Traits\RelatingModelTrait
*/
use RelatingModelTrait;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [];
/**
* The default rules that the model will validate against.
*
* @var array
*/
protected $rules = [];
/**
* The rulesets that the model will validate against.
*
* @deprecated watson/validating 0.10.9
*
* @var array
*/
protected $rulesets = [];
/**
* The attributes to encrypt when set and
* decrypt when gotten.
*
* @var array
*/
protected $encryptable = [];
/**
* The attributes to hash before saving.
*
* @var array
*/
protected $hashable = [];
/**
* Attributes to cast to a different type.
*
* @var array
*/
protected $jugglable = [];
/**
* The attributes to purge before saving.
*
* @var array
*/
protected $purgeable = [];
/**
* Relationships that the model should set up.
*
* @var array
*/
protected $relationships = [];
/**
* Extra attributes to be added to pivot relationships.
*
* @var array
*/
protected $relationshipPivots = [];
/**
* Dynamically retrieve attributes.
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
// Resolve relationship dynamically
if ($relationship = $this->getDynamicRelationship($key)) {
return $relationship;
}
// Dynamically retrieve the encryptable attribute
$value = $this->getDynamicEncrypted($key);
if (is_null($value) || ! is_string($value)) {
// Fallback to the default Eloquent dynamic getter
$value = parent::__get($key);
}
// Dynamically juggle the attribute.
// This is always called so that even decrypted values
// can be casted after decrypting.
return $this->getDynamicJuggle($key, $value);
}
/**
* Dynamically set attributes.
*
* @param string $key
* @param mixed $value
*/
public function __set($key, $value)
{
// Dynamically set the encryptable attribute
if (! empty($value) && $this->setDynamicEncryptable($key, $value)) {
return;
}
// Fallback to the default Eloquent dynamic setter
parent::__set($key, $value);
// Dynamically juggle the attribute.
$this->setDynamicJuggle($key, $value);
}
/**
* Get the messages for the instance.
*
* @return Illuminate\Contracts\Support\MessageBag
*/
public function getMessageBag()
{
return $this->getErrors();
}
}