forked from larastan/larastan
/
User.php
194 lines (165 loc) · 4.71 KB
/
User.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
<?php
namespace App;
use function get_class;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
use Illuminate\Database\Eloquent\Casts\AsCollection;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
use Tests\Application\HasManySyncable;
/**
* @property string $propertyDefinedOnlyInAnnotation
*
* @method Builder<static> scopeSomeScope(Builder $builder)
*
* @mixin \Eloquent
*/
class User extends Authenticatable
{
use HasFactory;
use Notifiable;
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/** @var array<string, string> */
protected $casts = [
'meta' => 'array',
'blocked' => 'boolean',
'email_verified_at' => 'date',
'allowed_ips' => 'encrypted:array',
'floatButRoundedDecimalString' => 'decimal:1',
'options' => AsArrayObject::class,
'properties' => AsCollection::class,
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function id(): int
{
return $this->id;
}
public function getAllCapsName(): string
{
return Str::upper($this->name);
}
public function scopeActive(Builder $query): Builder
{
return $query->where('active', 1);
}
/**
* @param Builder<User> $query
* @return Builder<User>
*/
public function scopeWhereActive(Builder $query): Builder
{
return $query->where('active', 1);
}
/** @phpstan-return BelongsTo<Group, User> */
public function group(): BelongsTo
{
return $this->belongsTo(Group::class)->withTrashed();
}
/** @phpstan-return HasMany<Account> */
public function accounts(): HasMany
{
return $this->hasMany(Account::class);
}
public function transactions(): HasManyThrough
{
return $this->hasManyThrough(Transaction::class, Account::class);
}
public function syncableRelation(): HasManySyncable
{
return $this->hasManySyncable(Account::class);
}
public function address(): MorphMany
{
return $this->morphMany(Address::class, 'addressable');
}
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class)
->withPivot('some_column')
->wherePivotIn('some_column', [1, 2, 3]);
}
public function children(): HasMany
{
return $this->hasMany(self::class, 'parent_id');
}
public function parent(): BelongsTo
{
return $this->belongsTo(get_class($this));
}
public function posts(): BelongsToMany
{
return $this->belongsToMany(Post::class);
}
public function hasManySyncable($related, $foreignKey = null, $localKey = null): HasManySyncable
{
$instance = $this->newRelatedInstance($related);
$foreignKey = $foreignKey ?: $this->getForeignKey();
$localKey = $localKey ?: $this->getKeyName();
return new HasManySyncable(
$instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey
);
}
public function getOnlyAvailableWithAccessorAttribute(): string
{
return 'foo';
}
public function isActive(): bool
{
return $this->active === 1;
}
public function setActive(): void
{
$this->active = 1;
}
/**
* @return Attribute<int, never>
*/
protected function newStyleAttribute(): Attribute
{
return Attribute::make(
fn ($value) => 5,
);
}
/**
* @return Attribute<int, string>
*/
protected function stringButInt(): Attribute
{
return Attribute::make(
fn ($value) => 5,
fn (string $value) => strtolower($value)
);
}
/** This will not take any effect because it's public and does not specify generic types at return type. */
public function email(): Attribute
{
return Attribute::make(
fn ($value) => 5,
fn (string $value) => strtolower($value)
);
}
}