56 lines
1.5 KiB
PHP
56 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Catalog;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class IdentityDimension
|
|
{
|
|
public function assign(Model $subject, EditorialDimensionValue $value, string $origin = 'direkt'): void
|
|
{
|
|
if (! in_array($origin, ['direkt', 'abgeleitet'], true)) {
|
|
throw new DomainRuleException('Die Herkunft einer Zuordnung ist direkt oder abgeleitet.');
|
|
}
|
|
|
|
IdentityDimensionValue::query()->updateOrCreate(
|
|
[
|
|
'subject_type' => $subject->getMorphClass(),
|
|
'subject_id' => $subject->getKey(),
|
|
'editorial_dimension_value_id' => $value->id,
|
|
],
|
|
['origin' => $origin],
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $valueIds
|
|
*/
|
|
public function matches(Model $subject, array $valueIds): bool
|
|
{
|
|
if ($valueIds === []) {
|
|
return true;
|
|
}
|
|
|
|
$assigned = IdentityDimensionValue::query()
|
|
->where('subject_type', $subject->getMorphClass())
|
|
->where('subject_id', $subject->getKey())
|
|
->pluck('editorial_dimension_value_id')
|
|
->all();
|
|
|
|
$wanted = EditorialDimensionValue::query()->whereIn('id', $valueIds)->get()->groupBy('editorial_dimension_id');
|
|
|
|
if ($wanted->isEmpty()) {
|
|
return false;
|
|
}
|
|
|
|
foreach ($wanted as $group) {
|
|
$ids = $group->pluck('id')->all();
|
|
if (array_intersect($ids, $assigned) === []) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|