adrema/app/Lib/Editor/Condition.php

46 lines
1.1 KiB
PHP
Raw Normal View History

2024-07-12 00:16:50 +02:00
<?php
namespace App\Lib\Editor;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\DataCollection;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\LaravelData\Attributes\DataCollectionOf;
class Condition extends Data
{
/** @param DataCollection<int, Statement> $ifs */
public function __construct(
public ConditionMode $mode,
#[DataCollectionOf(Statement::class)]
public DataCollection $ifs,
) {
}
public static function fromMedia(Media $media): self
{
2024-09-21 22:46:38 +02:00
return $media->getCustomProperty('conditions') ? static::factory()->withoutMagicalCreation()->from($media->getCustomProperty('conditions')) : static::defaults();
2024-07-12 00:16:50 +02:00
}
2024-07-12 18:05:11 +02:00
public static function defaults(): self
2024-07-12 00:16:50 +02:00
{
2024-09-21 22:46:38 +02:00
return static::factory()->withoutMagicalCreation()->from(['mode' => 'any', 'ifs' => []]);
2024-07-12 00:16:50 +02:00
}
public function hasStatements(): bool
{
return count($this->ifs) > 0;
}
public function isAny(): bool
{
return $this->mode === ConditionMode::ANY;
}
public function isAll(): bool
{
return $this->mode === ConditionMode::ALL;
}
}