|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Support\Models; |
|
4
|
|
|
|
|
5
|
|
|
use DomainException; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
|
9
|
|
|
use Illuminate\Support\Arr; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @property int $id |
|
14
|
|
|
* @property \Illuminate\Support\Carbon $created_at |
|
15
|
|
|
* @property \Illuminate\Support\Carbon $updated_at |
|
16
|
|
|
*/ |
|
17
|
|
|
trait HasAttributes |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Determine if the model relation value has been modified. |
|
21
|
|
|
* |
|
22
|
|
|
* @param array|string|null $relations |
|
23
|
|
|
* @return bool |
|
24
|
|
|
* |
|
25
|
|
|
* @throws \InvalidArgumentException |
|
26
|
|
|
*/ |
|
27
|
|
|
public function isRelationValueDirty($relations = null): bool |
|
28
|
|
|
{ |
|
29
|
|
|
$attributes = array_map(function ($name) { |
|
30
|
|
|
$relation = $this->$name(); |
|
31
|
|
|
|
|
32
|
|
|
if (!$relation instanceof Relation || !method_exists($relation, 'getForeignKeyName')) { |
|
33
|
|
|
throw new InvalidArgumentException(sprintf( |
|
34
|
|
|
'The given relation name %s on model class %s must return a relationship instance and has getForeignKeyName() method.', |
|
35
|
|
|
$name, static::class |
|
36
|
|
|
)); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $relation->getForeignKeyName(); |
|
40
|
|
|
}, is_array($relations) ? $relations : func_get_args()); |
|
41
|
|
|
|
|
42
|
|
|
return $this->isDirty($attributes); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Determine whether the given model collection is valid with the given class name. |
|
47
|
|
|
* |
|
48
|
|
|
* @param \Illuminate\Database\Eloquent\Collection<\Illuminate\Database\Eloquent\Model> $models |
|
49
|
|
|
* @param string $className |
|
50
|
|
|
* @param bool $throwExceptionWhenNotValid |
|
51
|
|
|
* @return bool |
|
52
|
|
|
* |
|
53
|
|
|
* @throws \DomainException |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function isCollectionValid(Collection $models, string $className, bool $throwExceptionWhenNotValid = true): bool |
|
56
|
|
|
{ |
|
57
|
|
|
return $models->every(function (Model $model) use ($className, $throwExceptionWhenNotValid) { |
|
58
|
|
|
$isValid = $model instanceof $className; |
|
59
|
|
|
|
|
60
|
|
|
if (!$isValid && $throwExceptionWhenNotValid) { |
|
61
|
|
|
throw new DomainException(sprintf( |
|
62
|
|
|
'Invalid %s model type, %s given', $className, get_class($model) |
|
63
|
|
|
)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $isValid; |
|
67
|
|
|
}); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Return collection value from specified model relation. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $key |
|
74
|
|
|
* @param string $className |
|
75
|
|
|
* @return \Illuminate\Database\Eloquent\Collection<\Illuminate\Database\Eloquent\Model> |
|
76
|
|
|
* |
|
77
|
|
|
* @throws \DomainException |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getCollectionValue(string $key, string $className): Collection |
|
80
|
|
|
{ |
|
81
|
|
|
$this->isCollectionValid($models = $this->getRelationValue($key), $className); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
return $models; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get the model's raw values from $attributes. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string|null $key |
|
90
|
|
|
* @param mixed $default |
|
91
|
|
|
* @return mixed|array |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getRawAttribute($key = null, $default = null) |
|
94
|
|
|
{ |
|
95
|
|
|
return Arr::get($this->getAttributes(), $key, $default); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|