Yeelight /
cloud2
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Yeelight\Base\Models; |
||||
| 4 | |||||
| 5 | use Carbon\Carbon; |
||||
| 6 | use Illuminate\Database\Eloquent\Model; |
||||
| 7 | use Prettus\Repository\Presenter\ModelFractalPresenter; |
||||
| 8 | use Yeelight\Models\Foundation\BaseUser; |
||||
| 9 | use Yeelight\Models\Interfaces\BaseModelEventsInterface; |
||||
| 10 | use Yeelight\Traits\BaseModelEvents; |
||||
| 11 | |||||
| 12 | /** |
||||
| 13 | * Class BaseModel |
||||
| 14 | * |
||||
| 15 | * @category Yeelight |
||||
| 16 | * |
||||
| 17 | * @package Yeelight\Base\Models |
||||
| 18 | * |
||||
| 19 | * @author Sheldon Lee <[email protected]> |
||||
| 20 | * |
||||
| 21 | * @license https://opensource.org/licenses/MIT MIT |
||||
| 22 | * |
||||
| 23 | * @link https://www.yeelight.com |
||||
| 24 | * |
||||
| 25 | * @property-read mixed $id |
||||
| 26 | * |
||||
| 27 | * @mixin \Eloquent |
||||
| 28 | */ |
||||
| 29 | class BaseModel extends Model implements BaseModelEventsInterface |
||||
| 30 | { |
||||
| 31 | use BaseModelEvents; |
||||
| 32 | |||||
| 33 | /** |
||||
| 34 | * Indicates if the model should be auto set user_id. |
||||
| 35 | * |
||||
| 36 | * @var bool |
||||
| 37 | */ |
||||
| 38 | protected $autoUserId = true; |
||||
| 39 | |||||
| 40 | /** |
||||
| 41 | * Indicates if the model should be recorded ips. |
||||
| 42 | * |
||||
| 43 | * @var bool |
||||
| 44 | */ |
||||
| 45 | protected $ips = true; |
||||
| 46 | |||||
| 47 | /** |
||||
| 48 | * Indicates if the model should be recorded users. |
||||
| 49 | * |
||||
| 50 | * @var bool |
||||
| 51 | */ |
||||
| 52 | protected $update_users = true; |
||||
| 53 | |||||
| 54 | /** |
||||
| 55 | * Indicates timestamp is always saved in UTC timezone. |
||||
| 56 | * |
||||
| 57 | * @var bool |
||||
| 58 | */ |
||||
| 59 | protected $timestampAlwaysUtc = false; |
||||
| 60 | |||||
| 61 | /** |
||||
| 62 | * Indicates timestamp is always get in user timezone. |
||||
| 63 | * |
||||
| 64 | * @var bool |
||||
| 65 | */ |
||||
| 66 | protected $getWithUserTimezone = true; |
||||
| 67 | |||||
| 68 | /** |
||||
| 69 | * Get the auth instance. |
||||
| 70 | * |
||||
| 71 | * @return \Dingo\Api\Auth\Auth |
||||
| 72 | */ |
||||
| 73 | protected function apiAuth() |
||||
| 74 | { |
||||
| 75 | return app('Dingo\Api\Auth\Auth'); |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | /** |
||||
| 79 | * Get ID from the model primary key. |
||||
| 80 | * |
||||
| 81 | * @return mixed |
||||
| 82 | */ |
||||
| 83 | public function getIdAttribute() |
||||
| 84 | { |
||||
| 85 | return $this->attributes[$this->getKeyName()]; |
||||
| 86 | } |
||||
| 87 | |||||
| 88 | /** |
||||
| 89 | * Get current auth user. |
||||
| 90 | * |
||||
| 91 | * @return BaseUser|null |
||||
| 92 | */ |
||||
| 93 | public function getAuthUser() |
||||
| 94 | { |
||||
| 95 | $user = null; |
||||
| 96 | if ($this->apiAuth()->check()) { // API |
||||
| 97 | $user = $this->apiAuth()->user(); |
||||
| 98 | } elseif (\Auth::guard(config('yeelight.backend.route.prefix'))->check()) { |
||||
| 99 | // Backend |
||||
| 100 | $user = \Auth::guard(config('yeelight.backend.route.prefix'))->user(); |
||||
| 101 | } elseif (\Auth::check()) { |
||||
| 102 | // Normal |
||||
| 103 | $user = \Auth::user(); |
||||
| 104 | } |
||||
| 105 | |||||
| 106 | return $user; |
||||
| 107 | } |
||||
| 108 | |||||
| 109 | /** |
||||
| 110 | * Get current auth user_id. |
||||
| 111 | * |
||||
| 112 | * @return mixed|null |
||||
| 113 | */ |
||||
| 114 | public function getAuthUserId() |
||||
| 115 | { |
||||
| 116 | $user_id = null; |
||||
| 117 | $user = $this->getAuthUser(); |
||||
| 118 | if ($user) { |
||||
| 119 | $user_id = isset($user->user_id) ? $user->user_id : $user->id; |
||||
| 120 | } |
||||
| 121 | |||||
| 122 | return $user_id; |
||||
| 123 | } |
||||
| 124 | |||||
| 125 | /** |
||||
| 126 | * Get current model's user_id. |
||||
| 127 | * |
||||
| 128 | * @return mixed|null |
||||
| 129 | */ |
||||
| 130 | public function getUserId() |
||||
| 131 | { |
||||
| 132 | return $this->user_id; |
||||
| 133 | } |
||||
| 134 | |||||
| 135 | /** |
||||
| 136 | * Update the creation and update ips. |
||||
| 137 | * |
||||
| 138 | * @return void |
||||
| 139 | */ |
||||
| 140 | protected function updateIps() |
||||
| 141 | { |
||||
| 142 | $ips = get_client_ip(); |
||||
| 143 | |||||
| 144 | if (!$this->isDirty('updated_ip')) { |
||||
| 145 | $this->updated_ip = $ips; |
||||
|
0 ignored issues
–
show
|
|||||
| 146 | } |
||||
| 147 | |||||
| 148 | if (!$this->exists && !$this->isDirty('created_ip')) { |
||||
| 149 | $this->created_ip = $ips; |
||||
|
0 ignored issues
–
show
|
|||||
| 150 | } |
||||
| 151 | } |
||||
| 152 | |||||
| 153 | /** |
||||
| 154 | * Update the creation and update by users. |
||||
| 155 | * |
||||
| 156 | * @return void |
||||
| 157 | */ |
||||
| 158 | protected function updateUsers() |
||||
| 159 | { |
||||
| 160 | $user_id = $this->getAuthUserId(); |
||||
| 161 | if (!($user_id > 0)) { |
||||
| 162 | return; |
||||
| 163 | } |
||||
| 164 | |||||
| 165 | if (!$this->isDirty('updated_by')) { |
||||
| 166 | $this->updated_by = $user_id; |
||||
|
0 ignored issues
–
show
|
|||||
| 167 | } |
||||
| 168 | |||||
| 169 | if (!$this->exists && !$this->isDirty('created_by')) { |
||||
| 170 | $this->created_by = $user_id; |
||||
|
0 ignored issues
–
show
|
|||||
| 171 | } |
||||
| 172 | } |
||||
| 173 | |||||
| 174 | /** |
||||
| 175 | * 判断当前登陆者 |
||||
| 176 | * |
||||
| 177 | * @return bool |
||||
| 178 | */ |
||||
| 179 | public function isAuthUserOwner() |
||||
| 180 | { |
||||
| 181 | return $this->getAuthUserId() == $this->getUserId(); |
||||
| 182 | } |
||||
| 183 | |||||
| 184 | /** |
||||
| 185 | * 获取当前UTC时间 |
||||
| 186 | * |
||||
| 187 | * @return Carbon |
||||
| 188 | */ |
||||
| 189 | public function getNowUTCTime() |
||||
| 190 | { |
||||
| 191 | return Carbon::now('UTC'); |
||||
| 192 | } |
||||
| 193 | |||||
| 194 | /** |
||||
| 195 | * 获取当前Auth用户UTC时间 |
||||
| 196 | * |
||||
| 197 | * @return Carbon |
||||
| 198 | */ |
||||
| 199 | public function getNowAuthUserTime() |
||||
| 200 | { |
||||
| 201 | return Carbon::now($this->getAuthUserDateTimezone()); |
||||
|
0 ignored issues
–
show
It seems like
$this->getAuthUserDateTimezone() can also be of type Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloquent\Relations\Relation; however, parameter $tz of Carbon\Carbon::now() does only seem to accept null|DateTimeZone|string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 202 | } |
||||
| 203 | |||||
| 204 | /** |
||||
| 205 | * 获取当前用户 |
||||
| 206 | * |
||||
| 207 | * @return BaseUser|null |
||||
| 208 | */ |
||||
| 209 | public function getUser() |
||||
| 210 | { |
||||
| 211 | return $this->user; |
||||
|
0 ignored issues
–
show
|
|||||
| 212 | } |
||||
| 213 | |||||
| 214 | /** |
||||
| 215 | * 获取关联用户 |
||||
| 216 | * |
||||
| 217 | * @return BaseUser|null |
||||
| 218 | */ |
||||
| 219 | public function getRelatedUser() |
||||
| 220 | { |
||||
| 221 | return $this->related_user; |
||||
|
0 ignored issues
–
show
|
|||||
| 222 | } |
||||
| 223 | |||||
| 224 | /** |
||||
| 225 | * Set Model Presenter. |
||||
| 226 | * |
||||
| 227 | * @return $this |
||||
| 228 | * @throws \Exception |
||||
| 229 | */ |
||||
| 230 | public function setModelPresenter() |
||||
| 231 | { |
||||
| 232 | $this->setPresenter(new ModelFractalPresenter()); |
||||
| 233 | |||||
| 234 | return $this; |
||||
| 235 | } |
||||
| 236 | |||||
| 237 | /** |
||||
| 238 | * Return a timezone for all Datetime objects. |
||||
| 239 | * |
||||
| 240 | * @return mixed |
||||
| 241 | */ |
||||
| 242 | public function getAuthUserDateTimezone() |
||||
| 243 | { |
||||
| 244 | $user = $this->getAuthUser(); |
||||
| 245 | if ($user && !empty($user->timezone)) { |
||||
|
0 ignored issues
–
show
The property
timezone does not exist on Yeelight\Models\Foundation\BaseUser. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||||
| 246 | return $user->timezone; |
||||
| 247 | } else { |
||||
| 248 | return app_timezone(); |
||||
| 249 | } |
||||
| 250 | } |
||||
| 251 | |||||
| 252 | /** |
||||
| 253 | * Set a given attribute on the model. |
||||
| 254 | * |
||||
| 255 | * @param string $key The Attribute Name |
||||
| 256 | * @param mixed $value The Attribute Value |
||||
| 257 | * |
||||
| 258 | * @return $this |
||||
| 259 | */ |
||||
| 260 | public function setAttribute($key, $value) |
||||
| 261 | { |
||||
| 262 | if ($this->timestampAlwaysUtc) { |
||||
| 263 | // set to UTC only if Carbon |
||||
| 264 | if ($value instanceof Carbon) { |
||||
| 265 | $value->setTimezone('UTC'); |
||||
| 266 | } |
||||
| 267 | } |
||||
| 268 | |||||
| 269 | return parent::setAttribute($key, $value); |
||||
| 270 | } |
||||
| 271 | |||||
| 272 | /** |
||||
| 273 | * Get a plain attribute (not a relationship). |
||||
| 274 | * |
||||
| 275 | * @param string $key The Attribute Name |
||||
| 276 | * |
||||
| 277 | * @return mixed |
||||
| 278 | */ |
||||
| 279 | public function getAttributeValue($key) |
||||
| 280 | { |
||||
| 281 | $value = parent::getAttributeValue($key); |
||||
| 282 | |||||
| 283 | if ($value instanceof Carbon && $this->getWithUserTimezone) { |
||||
| 284 | $value->setTimezone($this->getAuthUserDateTimezone()); |
||||
|
0 ignored issues
–
show
It seems like
$this->getAuthUserDateTimezone() can also be of type Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloquent\Relations\Relation; however, parameter $value of Carbon\Carbon::setTimezone() does only seem to accept DateTimeZone|string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 285 | } |
||||
| 286 | |||||
| 287 | return $value; |
||||
| 288 | } |
||||
| 289 | |||||
| 290 | /** |
||||
| 291 | * Prepare a date for array / JSON serialization. |
||||
| 292 | * |
||||
| 293 | * @param \DateTimeInterface $date DateTime Interface |
||||
| 294 | * |
||||
| 295 | * @return string |
||||
| 296 | */ |
||||
| 297 | protected function serializeDate(\DateTimeInterface $date) |
||||
| 298 | { |
||||
| 299 | if ($date instanceof Carbon && $this->getWithUserTimezone) { |
||||
| 300 | $date->setTimezone($this->getAuthUserDateTimezone()); |
||||
|
0 ignored issues
–
show
It seems like
$this->getAuthUserDateTimezone() can also be of type Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloquent\Relations\Relation; however, parameter $value of Carbon\Carbon::setTimezone() does only seem to accept DateTimeZone|string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 301 | } |
||||
| 302 | |||||
| 303 | return $date->format($this->getDateFormat()); |
||||
| 304 | } |
||||
| 305 | } |
||||
| 306 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.