1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ianrizky\Illuminate\Database\Eloquent\Concerns; |
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 InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
trait HasAttributes |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Determine if the model relation value has been modified. |
15
|
|
|
* |
16
|
|
|
* @param array|string|null $relations |
17
|
|
|
* @return bool |
18
|
|
|
* |
19
|
|
|
* @throws \InvalidArgumentException |
20
|
|
|
*/ |
21
|
|
|
public function isRelationValueDirty($relations = null): bool |
22
|
|
|
{ |
23
|
|
|
$attributes = array_map(function ($name) { |
24
|
|
|
$relation = $this->$name(); |
25
|
|
|
|
26
|
|
|
if (!$relation instanceof Relation || !method_exists($relation, 'getForeignKeyName')) { |
27
|
|
|
throw new InvalidArgumentException(sprintf( |
28
|
|
|
'The given relation name %s on model class %s must return a relationship instance and has getForeignKeyName() method.', |
29
|
|
|
$name, static::class |
30
|
|
|
)); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $relation->getForeignKeyName(); |
34
|
|
|
}, is_array($relations) ? $relations : func_get_args()); |
35
|
|
|
|
36
|
|
|
return $this->isDirty($attributes); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Determine whether the given model collection is valid with the given class name. |
41
|
|
|
* |
42
|
|
|
* @param \Illuminate\Database\Eloquent\Collection<\Illuminate\Database\Eloquent\Model> $models |
43
|
|
|
* @param string $className |
44
|
|
|
* @param bool $throwExceptionWhenNotValid |
45
|
|
|
* @return bool |
46
|
|
|
* |
47
|
|
|
* @throws \DomainException |
48
|
|
|
*/ |
49
|
|
|
protected function isCollectionValid(Collection $models, string $className, bool $throwExceptionWhenNotValid = true): bool |
50
|
|
|
{ |
51
|
|
|
return $models->every(function (Model $model) use ($className, $throwExceptionWhenNotValid) { |
52
|
|
|
$isValid = $model instanceof $className; |
53
|
|
|
|
54
|
|
|
if (!$isValid && $throwExceptionWhenNotValid) { |
55
|
|
|
throw new DomainException(sprintf( |
56
|
|
|
'Invalid %s model type, %s given', $className, get_class($model) |
57
|
|
|
)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $isValid; |
61
|
|
|
}); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Return collection value from specified model relation. |
66
|
|
|
* |
67
|
|
|
* @param string $key |
68
|
|
|
* @param string $className |
69
|
|
|
* @return \Illuminate\Database\Eloquent\Collection<\Illuminate\Database\Eloquent\Model> |
70
|
|
|
* |
71
|
|
|
* @throws \DomainException |
72
|
|
|
*/ |
73
|
|
|
public function getCollectionValue(string $key, string $className): Collection |
74
|
|
|
{ |
75
|
|
|
$this->isCollectionValid($models = $this->getRelationValue($key), $className); |
|
|
|
|
76
|
|
|
|
77
|
|
|
return $models; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|