Files
laraundfelix/app/Domain/Catalog/IdentityDimension.php
T
LarsandCursor 7b325f0d66
CI Feature / test (push) Successful in 53s
Deploy Test / test (push) Successful in 48s
Deploy Test / deploy (push) Successful in 22s
Lasse Präsentationsmedien ohne Inhaltsfassung zu.
Bilder, Filme und Hörstücke lassen sich zuordnen und für die Website freigeben, ohne eine fachliche Fassung anzulegen. Unterrichtsfilme behalten die Teaserfassung an ihrer bereitgestellten Fassung.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-19 08:20:35 +01:00

87 lines
2.6 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;
}
/**
* Ersetzt nur die Werte der Dimensionen, die dieses Fachobjekt anbietet.
* Nicht angebotene Zuordnungen, etwa gespiegelte Säulen am Inhaltselement, bleiben.
*
* @param list<int> $valueIds
*/
public function replaceOffered(Model $subject, array $valueIds): void
{
$offeredIds = EditorialDimension::offeredValueIds($subject);
$ids = array_values(array_intersect(
array_values(array_unique(array_map(intval(...), $valueIds))),
$offeredIds,
));
$query = IdentityDimensionValue::query()
->where('subject_type', $subject->getMorphClass())
->where('subject_id', $subject->getKey())
->where('origin', 'direkt')
->whereIn('editorial_dimension_value_id', $offeredIds === [] ? [0] : $offeredIds);
if ($ids !== []) {
$query->whereNotIn('editorial_dimension_value_id', $ids);
}
$query->delete();
foreach (EditorialDimensionValue::query()->whereIn('id', $ids)->get() as $value) {
$this->assign($subject, $value, 'direkt');
}
}
}